本帖最后由 jpolo 于 2019-3-27 12:34 编辑
菜鸟刚学习了几天,第一次发帖,试通过板载的14个引脚和外接两个按钮实现流星灯左右转换,
有不懂的地方,求大神指点啊......
测试视频:
下面是代码:
int right = A1;
int left = A0;
bool buttonState = true;
void setup() {
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop() {
if (digitalRead(left) == 1) {
delay(70);
if (digitalRead(left) == 1) {
buttonState = !buttonState;
}
if (buttonState == false) {
for (int i = 0; i<= 13; i = i + (1)) { //向左流动....................................................
pinMode(i, OUTPUT);
digitalWrite(i+4,HIGH);
delay(8);
digitalWrite(i+3,HIGH);
delay(6);
digitalWrite(i+2,HIGH);
delay(4);
digitalWrite(i+1,HIGH);
delay(2);
digitalWrite(i,HIGH);
delay(1);
}
for (int f = 0; f <= 13; f = f + (1)) {
pinMode(f, OUTPUT);
digitalWrite(f+4,HIGH);
delay(8);
digitalWrite(f+3,HIGH);
delay(6);
digitalWrite(f+2,HIGH);
delay(4);
digitalWrite(f+1,HIGH);
delay(2);
digitalWrite(f,HIGH);
delay(1);
digitalWrite(f,LOW);
delay(1);
digitalWrite(f+1,LOW);
delay(2);
digitalWrite(f+2,LOW);
delay(4);
digitalWrite(f+3,LOW);
delay(6);
digitalWrite(f+4,LOW);
delay(8);
//这里如何可以让上面的程序执行三次?....................................................
}
} else {
//这里好像加什么代码都没反应?为嘛?....................................................
}
}
if (digitalRead(right) == 1) {
delay(70);
if (digitalRead(right) == 1) {
buttonState = !buttonState;
}
if (buttonState == false) {
for (int i = 13; i>= 0; i = i - (1)) { //向右流动....................................................
pinMode(i, OUTPUT);
digitalWrite(i+4,HIGH);
delay(8);
digitalWrite(i+3,HIGH);
delay(6);
digitalWrite(i+2,HIGH);
delay(4);
digitalWrite(i+1,HIGH);
delay(2);
digitalWrite(i,HIGH);
delay(1);
}
for (int f = 13; f >= 0; f = f - (1)) {
pinMode(f, OUTPUT);
digitalWrite(f-4,HIGH);
delay(8);
digitalWrite(f-3,HIGH);
delay(6);
digitalWrite(f-2,HIGH);
delay(4);
digitalWrite(f-1,HIGH);
delay(2);
digitalWrite(f,HIGH);
delay(1);
digitalWrite(f,LOW);
delay(1);
digitalWrite(f-1,LOW);
delay(2);
digitalWrite(f-2,LOW);
delay(4);
digitalWrite(f-3,LOW);
delay(6);
digitalWrite(f-4,LOW);
delay(8);
//这里如何可以让上面的程序执行三次?....................................................
}
}
else {
// //这里好像加什么代码都没反应?为嘛?....................................................
}
}
}
|