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.
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);
}
}
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'?!?
*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.
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().
}