【模仿制作】闪烁的小灯-练习04
本帖最后由 明月照大江 于 2017-3-4 08:36 编辑作为创客新人,在DFROBOT社区看到大家的作品十分喜欢,非常感兴趣可惜我不会做。我决定从入门入门级,开始学习,并把每个学习的制晒一晒。希望能不断进步! 学习陈老师课件实例,闪烁的广告灯。如下:
代码如下:
int Led1 = 1;
int Led2 = 2;
int Led3 = 3;
int Led4 = 4;
int Led5 = 5;
void loop() {
style_1();
flash();
style_2();
flash();
style_3();
flash();
}
void style_1(void) {
unsigned char j;
for(j=1;j<=5;j++){
digitalWrite(j,HIGH);
delay(200);
}
for(j=5;j>=1;j--){
digitalWrite(j,LOW);
delay(200);
}
}
void flash(void) {
unsigned char j,k;
for(k=0;k<=1;k++) {
for(j=1;j<=5;j++) {
digitalWrite(j,HIGH);
delay(200);
}
for(j=1;j<=5;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);//延时400ms
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<=5;i++)
pinMode(i,OUTPUT);
}
页:
[1]