接线图:
代码:
unsigned long currentMillis =0;
unsigned long previousMillis_LED1 = 0;
unsigned long LED1_delay=0;
unsigned long previousMillis_LED2 = 0;
unsigned long LED2_delay=0;
#define LED1 3
#define LED2 9
#define P1 A2 //电位器1连接脚位,控制LED1灯延迟时间
#define P2 A1 //电位器2连接脚位,控制LED2灯延迟时间
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(P1, INPUT);
pinMode(P2, INPUT);
}
void loop() {
currentMillis= millis(); //开发板启动时到现在的毫秒数。
LED1_delay=0;
LED2_delay=0;
for(int i=0;i<5;i++){
LED1_delay=LED1_delay+map(analogRead(P1),0,1023,100,1000);//映射到100-1000
LED2_delay=LED2_delay+map(analogRead(P2),0,1023,100,1000);
}
if (currentMillis-previousMillis_LED1> LED1_delay){
//如果当前的‘启动毫秒数’比上次记录的‘启动毫秒数’大于设定的延迟时间
previousMillis_LED1=currentMillis;
//将当前‘启动毫秒数’存入上次毫秒数变量中,以便与下一次的时间比较。
digitalWrite(LED1,!digitalRead(LED1));
//颠倒led的显示状态
}
if (currentMillis-previousMillis_LED2> LED2_delay){
previousMillis_LED2=currentMillis;
digitalWrite(LED2,!digitalRead(LED2));
}
}