This code runs first loop then second loop but never goes back to first loop
void loop()
{
do
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
count++;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, ledState1);
}
} while (count < 50);
count =0;
do
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
count++;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);
Serial.print(count); // print as an ASCII-encoded decimal - same as "DEC"
}
} while (count < 50);
}
Can't figure why it stays in last loop.
thanks burt
The inner loop exits when count is greater than 50. Your outer loop requires counter to be <50 to run. Therefore it will not re enter outer loop. You set count to zero once you have entered outer loop.
It enters outer loop at the beginning as the program defaults to counter =0 when it first starts. It does the outer then inner, then loop around the outer, never going back in.
weedpharma:
The inner loop exits when count is greater than 50. Your outer loop requires counter to be <50 to run. Therefore it will not re enter outer loop. You set count to zero once you have entered outer loop.
It enters outer loop at the beginning as the program defaults to counter =0 when it first starts. It does the outer then inner, then loop around the outer, never going back in.
Weedpharma
What? What inner and outer loop? There are two do-while loops, one after the other.
How do you know it is running the second loop at all?
What is being printed to serial?
It starts blinking the second led and never goes back to the first led.
And i added serial.print (count) which show it counting the second loop over and over agin just it starting al 1 to 50 not 0.
count =0; //This resets the count for the second do loop
do
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
count++;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);
Serial.print(count); // print as an ASCII-encoded decimal - same as "DEC"
}
} while (count < 50);
Theres 3 loops two do loops a the main loop I thought it should run top down and go back to top of the main loop
I figured seeing i run first do while loop to 50 then second it should all start over again.
Thanks Burt
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis1 > interval)
{
// save the last time you blinked the LED
previousMillis1 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, ledState1);
}
if (currentMillis - previousMillis2 > interval)
{
// save the last time you blinked the LED
previousMillis2 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
digitalWrite(ledPin2, ledState2);
}
}
I'm not really seeing what count is supposed to do.
UKHeliBob:
You do not reset count back to zero after the second do/while loop so it never runs the first one again.
Count is reset after the first do loop with count = 0;
I'm not really seeing what count is supposed to do.
Well if it run like most top down code id run the first "do while count < 450 "
And the second " do while count <350" which I think did work but I messed something up. When
I changed the time. I didn't want to wait five minutes to see it run.
digitalWrite(ledPin2, ledState2);
Serial.print(count); // print as an ASCII-encoded decimal - same as "DEC"
}
} while (count < 50);
count=0;
}
It now happily runs both back to back and starts over.
Thanks all
Count is reset after the first do loop with count = 0;
Yes, but not after the second do/while so it start the first one again with a value of 50 which immediately satisfies the do/while condition so it exits the loop, gets set to zero and falls through to the second one.
UKHeliBob:
Yes, but not after the second do/while so it start the first one again with a value of 50 which immediately satisfies the do/while condition so it exits the loop, gets set to zero and falls through to the second one.