I had a simple blink sketch. My intention was to control number of blink by a global maxLoop variable. I also defined a global loopCount in order to remember how many times it blinked. The strange thing about this sketch was that it will continue blink regardless my condition check. The even stranger part was that if I uncommented either the Serial.println() or delay() line before the if condition, the sketch then worked as expected.
Could anyone run this program and see if it behaves the same? Thanks!
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int maxLoop = 3;
int loopCount =0;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
++loopCount;
//Serial.println("Loop() has run " + String(loopCount) + " times");
//delay(1000);
if(loopCount < maxLoop){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
} // wait for a second
}