我正在尝试为Arduino编写一个代码,它会打开LED 1秒钟,然后将其关闭5秒钟然后再打开1秒钟等等,我需要在不使用的情况下执行此操作delay()函数。
我找到了以下代码
if( (currentMils - prevMils_for_2) > interval_for_2 )
{
prevMils_for_2 = currentMils;
if(state_for_2 == LOW)
state_for_2 = HIGH;
else
state_for_2 = LOW;
digitalWrite(2, state_for_2);
}
使LED在没有使用延迟()的情况下闪烁但我无法弄清楚如果开关时间不同,我该如何应用这种技术。
答案 0 :(得分:0)
您可以更改 interval_for_2 值:
if((currentMils - prevMils_for_2) > interval_for_2 ){
prevMils_for_2 = currentMils;
if(state_for_2 == LOW){
state_for_2 = HIGH;
interval_for_2 = 1000;// duration for high
}
else{
state_for_2 = LOW;
interval_for_2 = 2000;// duration for low
}
digitalWrite(2, state_for_2);
}