Hey guys, I am trying to make a circuit that loops infinitely once a button is pressed but if the button is pressed again, I would like it to stop. For the infinite loop, I am using a while loop, I tried initially doing the same method as seen at the start of
while (digitalRead(button) == HIGH){
}
This works to start the loop but the loop itself it doesn't stop it, then I tried the following:
while (1==1){ //while loop that stops everything until user presses the button to start
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <=20000) // do this loop for up to 1000mS = 1S, 20x60x1000=1200
{
digitalWrite(redLED, LOW);
delay(500);
digitalWrite(redLED, HIGH);
delay(500);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <=5000) // do this loop for up to 1000mS = 1S
{
digitalWrite(redLED, HIGH);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(redLED, LOW);
delay(50);
if (digitalRead(button)==LOW){
break;
}
}
but this doesn't work either. I believe it could be because the circuit is not actively listening to the button, it is going through the circuit so please if anyone could suggest a solution to have like the circuit also constantly listening at the signals sent by the button.
int redLED = 3;
int blueLED = 4;
int button = 6;
int buzzer = 5;
long starttime = 0;
long endtime = 0;
void setup() {
Serial.begin(9600); //remove when end
pinMode(A0, INPUT); //potentiometer
pinMode(buzzer, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(button, INPUT_PULLUP);
digitalWrite(redLED, LOW); //initialize all these as off, LEDs as well as we need to see in the void loop which one will be turne on
digitalWrite(blueLED, LOW);
digitalWrite(buzzer, LOW);
}
void loop() {
//depending on if manual or auto, have either one or the other LED on
while (digitalRead(button) == HIGH){ //while loop that stops everything until user presses the button to start
}
digitalWrite(buzzer, HIGH);
delay(50); //delay required between while loops or the LOW state that breaks each loop will affect both and will not start the light flashing loop
digitalWrite(buzzer, LOW);
while (1==1){ //while loop that stops everything until user presses the button to start
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <=20000) // do this loop for up to 1000mS = 1S, 20x60x1000=1200
{
digitalWrite(redLED, LOW);
delay(500);
digitalWrite(redLED, HIGH);
delay(500);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <=5000) // do this loop for up to 1000mS = 1S
{
digitalWrite(redLED, HIGH);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(redLED, LOW);
delay(50);
if (digitalRead(button)==LOW){
break;
}
}
}
Your sketch is moderated as follows so that redLED blinks for 20 sec at 1 sec interval once you press the button. Now, read pos #4@anon95813201 and add necessary code so that when the button is agian pressed within this 20 sec loop, the blinking will stop and the control will come out of while() loop.
int redLED = 3;
int blueLED = 4;
int button = 6;
int buzzer = 5;
//unsigned long startTime = 0;
//unsigned long endtTme = 0;
unsigned long pr1Millis = millis();
unsigned long pr2Millis = millis();
byte ledState = HIGH;
void setup() {
Serial.begin(9600); //remove when end
pinMode(A0, INPUT); //potentiometer
pinMode(buzzer, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(button, INPUT_PULLUP);
digitalWrite(redLED, LOW); //initialize all these as off, LEDs as well as we need to see in the void loop which one will be turne on
digitalWrite(blueLED, LOW);
digitalWrite(buzzer, LOW);
}
void loop()
{
//depending on if manual or auto, have either one or the other LED on
while (digitalRead(button) == HIGH)
{ //while loop that stops everything until user presses the button to start
; //wait
}
/* digitalWrite(buzzer, HIGH);
delay(50); //delay required between while loops or the LOW state that breaks each loop will affect both and will not start the light flashing loop
digitalWrite(buzzer, LOW);
while (1 == 1) { //while loop that stops everything until user presses the button to start
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
startTime = millis();
endTime = starttime;
while ((millis()) - starttime) <= 20000) // 20 sec test time
{
*/
do //blinking redLED for 20 sec at 1 sec interval
{
digitalWrite(redLED, ledState);
if (millis() - pr1Millis >= 1000)
{
ledState = !ledState;
pr1Millis = millis();
}
} while (millis() - pr2Millis <= 20000);
digitalWrite(redLED, LOW);
/*
digitalWrite(redLED, LOW);
delay(500);
digitalWrite(redLED, HIGH);
delay(500);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <= 5000) // do this loop for up to 1000mS = 1S
{
digitalWrite(redLED, HIGH);
endtime = millis();
}
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(redLED, LOW);
delay(50);
if (digitalRead(button) == LOW) {
break;
}
*/
}
Try to sample the closing condition of button within the 20 sec loop of the sketch of post #7 (you have marked solution) for coming out of the while() loop. Try to put in the following code and post to show what you ave done.
if(digitalRead(button) == HIGH) // Edit: If ===> if
{
break;
}
The above should go in the while() loop of the following code.
do //blinking redLED for 20 sec at 1 sec interval
{
digitalWrite(redLED, ledState);
if (millis() - pr1Millis >= 1000)
{
ledState = !ledState;
pr1Millis = millis();
}
} while (millis() - pr2Millis <= 20000);
digitalWrite(redLED, LOW);