I'm trying to write a blink without delay code that will wait 20 seconds, then blink 5 times, then wait another 20 seconds - rinse and repeat.
Here's the minimal code I wrote:
if (currentTime % 20000 == 0) {
for (int x = 0; x <= 5; x++) {
if (currentTime % 500 == 0) {
if (solenoid == LOW) {
solenoid = HIGH;
} else {
solenoid = LOW;
}
digitalWrite(solenoidpin, solenoid);
}
}
}
}
All this seems to do is set solenoid to HIGH and keep it there. If I get rid of the for loop and the second if statement, I can get it to switch between low and high every 20 seconds, but adding the if and for loops seems to kill it. Any thoughts on how to make this work?
for
loop 6 times? IfcurrentTime % 500 == 0
is true, it will flip the solenoid state 6 times (i.e., not change it).micros()
value, you will only "trigger" if you happen to approach eachif
statement on exactly the microsecond boundary. Depending on your other code, you may find that the Arduino doesn't get around to yourif
statements on every sequential millisecond, and so you may miss some triggers. Better to do the subtract-and-compare operation that the BlinkWithoutDelay actually uses. That way, yourif
will trigger, even if you happened to miss the time by one millisecond.for
loop and the secondif
are pointless. In thefor
loop the variablecurrentTime
doesn't change and the secondif
is always true because 20000 is divisible by 500.