// https://wokwi.com/projects/371695771876162561
// for https://forum.arduino.cc/t/for-loop-w-o-delay/1152722/36
// and https://forum.arduino.cc/t/millis-instead-of-delay-and-loop-instead-of-for-loop/1110044
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(12, INPUT_PULLUP);
delay(10); // let Wokwi switch settle after initialization
}
void loop() {
bool doBlocking = digitalRead(12)==HIGH; // choose between blocking or non-blocking code
const unsigned long quickInterval = 250; //
const unsigned long slowInterval = 1000 +quickInterval;
if (doBlocking == true) { // use delay() and for(;;)
for (int i = 0 ; i < 10 ; ++i) {
toggleLed();
Serial.print(i);
Serial.print(" ");
delay(quickInterval);
}
Serial.println("-- blocking");
delay(slowInterval);
} else { // unblocked loop() by:
// * replacing delays with millis() edge-detectors/state machines
// * replacing for(;;) loop with a counter state machine
// state variables to persist values past the end of loop()'s scope
static int i = 0; // digit to print
static unsigned long lastDigitMs = -quickInterval; // backdate by -quickInterval for immediate operation
static unsigned long lastLineMs = 0; // state variable for edge-detecting slow delay
unsigned long now = millis(); // remember the loop time
// edge detecting state machine {i,now-las}
if (i < 10 && now - lastDigitMs >= quickInterval) { // edge-detect interval change
lastDigitMs = now; // measure digit delay from last digit print
toggleLed();
Serial.print(i);
Serial.print(" ");
++i;
lastLineMs = now; // measure line ending delay from last print time
}
// edge-detecting state machine
if (i >= 10 && now - lastLineMs >= slowInterval) {
Serial.println( "-- nonblocking");
i = 0; // reset the count
}
}
}
void toggleLed(void){
digitalWrite(LED_BUILTIN,digitalRead(LED_BUILTIN)==HIGH?LOW:HIGH);
}