If while statement is not met(buttonPin) after 10sec reset loop, What to do?

Hello,

I am self teaching myself and new to arduino I am having problems trying to figure out how to put a loop reset in, if a while statement is not true. Pretty much button1 is pressed (loop starts)then stops and waits for the (while statement) if its true(button1 pressed again) then continue loop. If its false for 10sec then reset the loop. brain is fried any help would be a blessing.

Thanks,

attach is the code i am working on.

codehelp.txt (1.09 KB)

here is the code..

const int buttonPin = 2;
const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int ledPin4 = 8;

int buttonState = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);     //set the switch pin as input
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {

    digitalWrite(ledPin1, HIGH);
    delay(5000);
    digitalWrite(ledPin1, LOW);
    {
   while (digitalRead(buttonPin) == HIGH) {
  // Do nothing
}


     //need a condition or reset <---help here
    
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    delay(5000);
    digitalWrite(ledPin2, LOW);
    delay(5000);
    digitalWrite(ledPin4, HIGH);
    delay(5000);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    delay(50);
  }



    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }

}

You need to make sure you don't use delay() because your code will be stuck waiting there and can't tell if your 10 seconds have expired.

No sure I get what you are explaining...

wait for button get pressed = start the loop
then what ?
what happens if I release the button ?
what happens if I don't release the button ?
what happens if the button bounces ?

What do you mean by "a loop reset"? If you want the 'loop()' function to start over at the top, use "return;". The 'loop()' function is special because as soon as it returns, it will be called again.

I added some comments to make it more clear what is happening in your sketch:

void loop()
{
  // If the button is down
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW)
  {
    // Turn on LED 1:  *OOO
    digitalWrite(ledPin1, HIGH);
    // for 5 seconds
    delay(5000);
    // and then turn it off: OOOO
    digitalWrite(ledPin1, LOW);


    // Wait until the button is down
    while (digitalRead(buttonPin) == HIGH)
    {
      // Do nothing
    }


    //  need a condition or reset <---help here  // WHAT 'CONDITION OR RESET'?!?


    // then turn on LEDs 2 and 3: O**O
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    // wait five seconds
    delay(5000);
    // Turn LED 2 off: OO*O
    digitalWrite(ledPin2, LOW);
    // wait another five seconds
    delay(5000);
    // Turn LED 4 on: OO**
    digitalWrite(ledPin4, HIGH);
    // wait ANOTHER five seconds
    delay(5000);
    // Turn LEDs 3 and 4 off: OOOO
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    // wait 0.05 seconds
    delay(50);


    // And, even though they are all off, tur them off again: OOOO
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
}

Johnwasser,

by loop reset i mean to go back to the beginning of the void loop and wait for and input. so a button input will start the void loop (turn LED 1 on then off). after that is my problem, now it will wait for a button input, but i need something to tell it to go back to the beginning of the void loop after 10 seconds if no button is press. If there is a button input it will continue with the rest of the script. Sorry I was not clear and thanks for the help.

// Wait until the button is down
while (digitalRead(buttonPin) == HIGH)
{
// Do nothing
}

// a rest timer set a 10 seconds if no input/ if there is an input continue with script and finish // WHAT 'CONDITION OR RESET'?!?

read the tutorial pinned at the top of the forum on how to use millis() - that will likely help you solve your problem

J-m-L,

Thanks for the reply, so this is how i have

*wait for button get pressed = start the loop
{
// If the button is down then released
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
// Turn on LED 1: *OOO
digitalWrite(ledPin1, HIGH);
// for 5 seconds
delay(5000);
// and then turn it off: OOOO
digitalWrite(ledPin1, LOW);

*// Wait until the button is down then released

while (digitalRead(buttonPin) == HIGH)
{
// Do nothing

*Here I need a timer rest or something that will go back to the beginning of the void loop if there is no button input after 10 seconds. If there is a button input, it will continue with the rest of the script and complete it.

// then turn on LEDs 2 and 3: OO
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
// wait five seconds
delay(5000);
// Turn LED 2 off: OO*O
digitalWrite(ledPin2, LOW);
// wait another five seconds
delay(5000);
// Turn LED 4 on: OO

digitalWrite(ledPin4, HIGH);
// wait ANOTHER five seconds
delay(5000);
// Turn LEDs 3 and 4 off: OOOO
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
// wait 0.05 seconds
delay(50);

// And, even though they are all off, tur them off again: OOOO
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}

The buttons should be locked out after begin press the first time, until i look for the button press again to complete the script. if no input at that time go back to beginning of script.

Thanks again.

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


*Here I need a timer rest or something that will go back to the beginning of the void loop if there is no button input after 10 seconds. If there is a button input, it will continue with the rest of the script and complete it.

just measure the wait time and do a "return;" to exit the loop() function. as the loop() loops, it will come back at the top. see how to use millis() in the tutorial I pointed out for that.

My recommendation though is that you need to get rid of ALL the delays() in your code.

also you will need to handle button "being pressed" versus "staying pressed" (or use a button library to make it easier)

    // Wait until the button is down (or for ten seconds)
    unsigned long startTime = millis();
    while (digitalRead(buttonPin) == HIGH)
    {
      if (millis() - startTime >= 10000)
        return;  // No input for ten seconds.  Re-start loop().
    }

The problem with your delays is that you will never know if the button has been released in between or not...

Johnwa553r,

you are arduino god!!!! thanks for all the help! works great.