/*
============================
Selfmade For-Loop II
============================
This is an example of a "selfmade" for-loop
which is usually required where a a non-blocking
loop is required e.g. to blink an LED
This is an example of
a non-blocking millis() function to blink a LED
but counting a variable in a for loop with delay()
The LED will not blink in the correct timing as the for-loop incl.
the delay() blocks loop()
First Edition 2023-04-02
ec2021
Simulation: https://wokwi.com/projects/360902704451617793
Discussion: https://forum.arduino.cc/t/millis-instead-of-delay-and-loop-instead-of-for-loop/1110044/3
Changed 2023-07-26
*/
constexpr byte LEDPin = 13;
void setup() {
Serial.begin(115200);
pinMode(LEDPin, OUTPUT);
}
unsigned long lastChange = 0; // Variable to store the time in ms when ledState was changed
unsigned long delayTime = 300; // Delay in [ms] when then next change shall take place
unsigned long delayCount = 500;
int iMax = 9;
void loop() {
if (millis()-lastChange >= delayTime){
lastChange = millis();
boolean ledState = digitalRead(LEDPin);
digitalWrite(LEDPin, !ledState);
}
for (int i = 0; i <= iMax;i++){
Serial.print(i);
Serial.print("\t"); // prints tabs
delay(delayCount);
if (i == iMax) Serial.println();
}
}