Void loop not running nested while loops after first run

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).

1 Like

Also. Theres no need to use a while loop if your just doing an increment

Use a for loop. c = 0. Do the loop while c is less than 5. Increment c by 1 at the end of the loop.

for (int c = 0; c < 5; c++)
{
  //do stuff here
}

expanding on @jremington post.

Think about it, when cntRED reaches 5 it will be 5 in the next loop so it will never run again.

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.

printing to the serial monitor will show you what is REALLY happening.

What values variables have. Which lines of code get executed and which not.

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() {
  Serial.println("top of 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;
  }
  Serial.println("while (cntRED < 5) done");
  Serial.print("cntRED=");
  Serial.println(cntRED);



  while (cntBLUE < 10)
  {
    Serial.println(cntBLUE);
    digitalWrite (blueLED, 1);
    delay (delayBLUE);
    digitalWrite (blueLED, 0);
    delay (delayBLUE);
    cntBLUE = cntBLUE + 1;
  }
  Serial.println("while cntBLUE < 10 done");
  Serial.print("cntBLUE=");
  Serial.println(cntBLUE);

  Serial.println("bottom of loop");
}

This should work because void loop() turns the crank.
Take the cntRED >=5 && part out and see red and blue alternate.

void loop() {
  // put your main code here, to run repeatedly:
if (cntRED < 5)
{
  Serial.println(cntRED);
  digitalWrite (redLED,1);
  delay (delayRED);
  digitalWrite (redLED,0);
  delay (delayRED);
  cntRED++;  // adds 1 to cntRED, called an increment operation
}

if (cntRED >= 5 && cntBLUE < 10)
{
  Serial.println(cntBLUE);
  digitalWrite (blueLED,1);
  delay (delayBLUE);
  digitalWrite (blueLED,0);
  delay (delayBLUE);
  cntBLUE++;
}

}

where does cntRED get reset?

and when/where should it?

To me, the OP hasn't got that far yet, problem in the way.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.