How to pause a loop and then Continue a Loop?
Depends on what else you want to do.
Need more context.
Are you wanting to say pause the program on a button press and resume it once pressed again?
digitalWrite(buzzer, LOW);
digitalWrite(b, HIGH);
digitalWrite(a, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(onee, LOW);
delay(1000);//8
if (digitalRead(cancel) == LOW)
{
Serial.println ("CANCEL");
digitalWrite(buzzer, LOW);
delay(forever);
}
digitalWrite(buzzer, HIGH);
digitalWrite(b, HIGH);
digitalWrite(a, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
delay(1000);//7
if (digitalRead(cancel) == LOW)
{
Serial.println ("CANCEL");
digitalWrite(buzzer, LOW);
delay(forever);
}
Yeah, so basically I had a cancel button? How would I make a resume? instead of "Delay" Forever... ??
The demo Several Things at a Time illustrates how to use millis() to manage timing and allow the Arduino to do sev.....
...R
You can create a "do nothing" [u]while() loop[/u] and then do something to make the while() condition false to break out of the loop.
Or with another kind of loop, you can use break; with an if-statement to exit the loop.
(The only way to break-out of a delay() is to reset the processor, or wait for the delay to end.)
It's important to remember that a loop (ie, a for or while loop) is different from the loop() function.
The loop() function is a "special" function, which is called repeatedly (the Arduino IDE combines your sketch with some initialization code which ends with:
while(1){loop();}
void delayForever() {
while(true) {
//do nothing
}
}
There you go.
If you had a "resume" button and you wired it so pressing the button causes a pin to go LOW then ...
void delayUntilResume(){
while(digitalRead(RESUME_PIN)) {
//do nothing until the resume button is touched
}
}
if (digitalRead(button1) == LOW) if (digitalRead(button2) == LOW)
{
if (digitalRead(cancel) == LOW)
{
Serial.println ("CANCEL");
digitalWrite(buzzer, LOW);
delay(forever
);
}
Serial.print("\nBomb Activiated\n");
delay(500);//10
if (digitalRead(cancel) == LOW)
{
Serial.println ("CANCEL");
digitalWrite(buzzer, LOW);
delay(forever);
}
digitalWrite(buzzer, HIGH);
digitalWrite(b, HIGH);
digitalWrite(a, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(onee, HIGH);
delay(1000);//9
Would i have to change any of these If statements in order to add while loops?
if (digitalRead(button1) == LOW) if (digitalRead(button2) == LOW)
Did you mean if (digitalRead(button1) == LOW && digitalRead(button2) == LOW)
?
@ScottT, did you happen to notice that everyone else has been placing their code between code tags in the above replies?
It makes the code much easier to read, and to copy/paste into an IDE. And keeps everyone happy.
You can do this using the </> button in the "Reply" dialog window.
(That button's not visible by default in the "Quick Reply" dialog, but that can be changed in your settings.)
delay(forever);
What is "forever"? There is no value/variable for infinity or forever in C/C++. ...You can delay() for something like 45 days... Whatever number of milliseconds a type unsigned long can hold. But, I doubt you really want to freeze your program for 45 days, or for forever.
You can delay "forever" by making an infinite loop... A loop that repeats forever unless the processor is reset. That's usually a BUG, since your software appears to "freeze up" or "get stuck" while it's looping forever.
Anyway... Like I said, your program can't do anything until the time delay runs-out... So if you delay forever (or for a very long time), you'll "never" get to the instructions following the delay and your program will appear to freeze-up for a very long time.
Scott,
You haven't got a loop, or you haven't shown that part of your code... You may have LOTS of errors and debugging may be nearly impossible....
I suggest you start simple... For example, start with the Blink Example, or the Blink Without Delay Example, and add a button and some code to stop the blinking when the button is pushed (or vice-versa).
And, take baby steps... Add the button and a couple of lines of code to read the button and send the results to the serial monitor.
When you're sure you can read the button, add your if-statement, or conditional loop statement, etc, to change what happens when you push the button.
Once you've got those basics working, add one or two lines of code at a time, test-compiling and test-running as you go, until your program does what you want.
Sorry guys, I'm new to Arduino...
Basically What i wanted to happen was If both Button 1 & Button 2 were pressed, it would start the loop and when the "Cancel" Button was pressed It would pause the Loop and wait IF Button 1 and 2 were pressed, it would continue. Unfortunately I don't know how to do that thus making a delay(Forever) which is int forever = 9999999 ..
I'll try to read While Do lessons and redo my code; if anyone could help me with a format on what to do with Do While or anything I would appreciate it :c
Thanks guys, sorry again for my amateur arduino forum posting & project :C
ScottT:
Unfortunately I don't know how to do that thus making a delay(Forever)
See Reply #4
...R