I have written a program to flash two LEDs. The void loop runs once and then doesn't run again. Does this have to do with the fact that I'm using while loops? If so how do I get this to run the while loops over again continuously?
int redLED=13;
int blueLED=3;
int delayRED=500;
int delayBLUE=800;
int delayLONG=2000;
int cntRED=0;
int cntBLUE=0;
void setup() {
// put your setup code here, to run once:
pinMode (redLED,OUTPUT); // Set PIN 13 to output which in turn sets LED to output.
pinMode (blueLED,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while (cntRED < 5)
{
Serial.println(cntRED);
digitalWrite (redLED,1);
delay (delayRED);
digitalWrite (redLED,0);
delay (delayRED);
cntRED=cntRED+1;
}
while (cntBLUE < 10)
{
Serial.println(cntBLUE);
digitalWrite (blueLED,1);
delay (delayBLUE);
digitalWrite (blueLED,0);
delay (delayBLUE);
cntBLUE=cntBLUE+1;
}
}
Hint: the keyword "void", when used before a function name, indicates that the function does not return a value.
The loop function runs continuously, but after cntRED and cntBLUE are incremented past the conditions set in the if statements, nothing else will happen.
Reset the values of cntRED and cntBLUE to see more action. Better, don't use while, because the loop function loops (it is called repeatedly from the hidden main program).
I can suggest you to write as a comment the condition that would make each while loop end, as follows:
This works as a reminder of a condition known to be true right after the loop ends. This is the opposite to the while loop condition, which must be true for the while to execute.
As mentioned above, if nothing outside the while loop changes cntRED to be less than 5 it does not execute again.
—-
And this is an incorrect assumption:
Using Serial.print() as a debugging tool may reveal what your program is actually doing.