以下的代码存在一个bug 当拔掉连接的数据线时,led灯不会立马熄灭
要求三个功能:
1.红灯常亮
2.绿灯常亮
3.蓝灯常亮
4。三个灯(红绿蓝的顺序)循环闪烁,2s
如何使用delay函数配合计数器i++ 实现定时功能
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
TM1637Display display(CLK, DIO);
void setup() {
// put your setup code here, to run once:
pinMode(7,OUTPUT);//B
pinMode(8,OUTPUT);//R
pinMode(9,OUTPUT);//G
pinMode(10,INPUT);//10-1
pinMode(16,INPUT);//16-2
pinMode(14,INPUT);//14-3
pinMode(15,INPUT);//15-4
}
void loop() {
// put your main code here, to run repeatedly:
int i = 0;
if(digitalRead(10)==HIGH){
i=1;
};
if(digitalRead(16)==HIGH){
i=2;
};
if(digitalRead(14)==HIGH){
i=3;
};
if(digitalRead(15)==HIGH){
i=4;
};
display.setBrightness(7, true);
display.clear();
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
switch(i){
case 1:
display.clear();
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
display.showNumberDec(1, false); // Expect: ___1
break;
case 2:
display.clear();
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
display.showNumberDec(2, false); // Expect: ___2
break;
case 3:
display.clear();
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
display.showNumberDec(3, false); // Expect: ___3
break;
case 4:
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
digitalWrite(9,LOW);
display.showNumberDec(1, false); // Expect: ___1
delay(TEST_DELAY);
display.clear();
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
display.showNumberDec(2, false); // Expect: ___2
delay(TEST_DELAY);
display.clear();
digitalWrite(9,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
display.showNumberDec(3, false); // Expect: ___3
delay(TEST_DELAY);
break;
default:
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
display.clear();
break;
}
}
```