Instead of:
// Act if the latter time (ms) has now passed on this particular counter,
if (timeout(&count1, 500UL )) {
if (led1State == LOW) {
led1State = HIGH;
}
else {
led1State = LOW;
}
digitalWrite(led1Pin, led1State);
}
I'd use:
if (timeout(&count1, 500UL )) { //has the timing period ended?
led1State = !led1State;//Invert Led1State, HIGH becomes LOW and LOW becomes HIGH.
digitalWrite(led1Pin, led1State);//turn LED on or off, as defined by led1State.
}
I'd also add an explanation that, although this code only blinks LEDs, the principle can be use to time anything.