arduino制作简易跑马灯

arduino制作简易跑马灯

第一天摸arduino,打算把这里当成一个记录学习过程的备忘录 不定期更新一下

由于还不太会用面包板所以接线很笨拙。。

接线如图

arduino制作简易跑马灯_第1张图片因为发现一列只有五个孔但我有六盏灯 所以还用了一根线串起来
(我觉得肯定有更省线的做法但找了一圈都没太看懂 求教)

代码如下:

int startPin = 2;
int endPin = 7;             //因为我用的是2-7端口
int index = 0;
void setup() {
  // put your setup code here, to run once:
   for(int i=startPin;i<=endPin;i++){
    pinMode(i,OUTPUT);
   }
}

void loop() {
  // put your main code here, to run repeatedly:
    for(int i=startPin;i<=endPin;i++){
      digitalWrite(i,LOW);
    }
    digitalWrite(startPin + index,HIGH);
    index = (index+1) % (endPin - startPin+1);
    delay(100);
}

更新一下代码 比原来nb了一点:

int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 5;
int ledPin6 = 6;

void style_1(void){
  unsigned char j;
  for(j=1;j<=6;j++){
    digitalWrite(j,HIGH);
    delay(200);
  }
  for(j=1;j<=6;j++){
    digitalWrite(j,LOW);
    delay(200);
  }
}

void flash(void){
  unsigned char j,k;
  for(k=0;k<=1;k++){
    for(j=1;j<=6;j++)
        digitalWrite(j,HIGH);
    delay(200);
    for(j=1;j<=6;j++)
    digitalWrite(j,LOW);
    delay(200);
        
    }
  }

void style_2(void){
  unsigned char j,k;
  k=1;
  for(j=3;j>=1;j--){
    digitalWrite(j,HIGH);
    digitalWrite(j+k,HIGH);
    delay(400);
    k+=2;
  }
  k=5;
  for(j=1;j<=3;j++){
    digitalWrite(j,LOW);
    digitalWrite(j+k,LOW);
    delay(400);
    k-=2;
  }
}

void style_3(void){
  unsigned char j,k;
  k=5;
  for(j=1;j<=3;j++){
    digitalWrite(j,HIGH);
    digitalWrite(j+k,HIGH);
    delay(400);
    digitalWrite(j,LOW);
    digitalWrite(j+k,LOW);
    k-=2;
  }
  k=3;
  for(j=2;j>=1;j--){
    digitalWrite(j,HIGH);
    digitalWrite(j+k,HIGH);
    delay(400);
    digitalWrite(j,LOW);
    digitalWrite(j+k,LOW);
    k+=2;
  }
}

void setup(){
  unsigned char i;
  for(i=1;i<=6;i++){
    pinMode(i,OUTPUT);
  }
}

void loop(){
  style_1();
  flash();
  style_2();
  flash();
  style_3();
  flash();
}

你可能感兴趣的:(arduino学习日常)