Start or stop loop on button press

Hello everyone,

I am just starting with arduino so I am doing this wrong please let me know.

My question is is there a way to start or stop a loop when you press a button.

I am now using this to print “hi” to the console if the button has not been pressed, but if the button has been pressed. It will no longer say “hi” in the console.

But maybe you guys ( or girls) can help my make the code so when I press the button it stops. But when you press the buton again it will start. If you need any more information just let me know.

This is the code that I am using right now:

//Button Toggle LED
int test = 0;
int ledPin = 13;
int buttonPin = 3;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)
void setup() {
  pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);//initialize Serial connection
}

void loop() {
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW) { //if button has just been pressed
    delay(1);//crude form of button debouncing
    //toggle the state of the LED
    if (ledState == HIGH) {
      digitalWrite(ledPin, HIGH);
      test = 0;
      Serial.print(test);
      ledState = LOW;
    } else {
      digitalWrite(ledPin, LOW);
      test = 1;
      Serial.print(test);
      ledState = HIGH;
    }
  }
  lastState = currentState;
  while (test < 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print("hi");
    Serial.print('\n');
    if (test == 0) {
      break;
    }
  }
}

led_switch_Working.ino (1.05 KB)

Nothing inside that while loop at the end modifies test. So if test was less than one to get into the loop, nothing in that while loop will ever make it not less than 1. So your code Will be stuck in that while loop forever.

I think you wanted an if and not a while there.

When I use the code it works fine. But if I change while to if the code wil still works?

Luukth:
When I use the code it works fine. But if I change while to if the code wil still works?

What do you mean works fine? I thought you wanted to be able to press the button again to stop it.

You can't do that if the code is stuck in that while loop. It will just print hi over and over again until test becomes 1 Or greater and that will never ever happen since nothing in that while loop ever changes the value of test.

Delta_G:
What do you mean works fine? I thought you wanted to be able to press the button again to stop it.

You can't do that if the code is stuck in that while loop. It will just print hi over and over again until test becomes 1 Or greater and that will never ever happen since nothing in that while loop ever changes the value of test.

Yes I know it is strange but for some reason it worked. Do you maybe have a link to a tutorial where I can find a good code example?

No, the problem is your definition of worked. Sure it worked. It did exactly what it was programmed to do.

Once you pushed the button it started printing "hi" over and over again. If that is what you want, then what are you asking about.

In your original post however you said that you wanted to be able to push the button again and stop it. It doesn't do that now does it? So in that sense it doesn't work.

So my question to you is, what do you want it to do? What is your definition of "it works"?

Delta_G:
No, the problem is your definition of worked. Sure it worked. It did exactly what it was programmed to do.

Once you pushed the button it started printing "hi" over and over again. If that is what you want, then what are you asking about.

In your original post however you said that you wanted to be able to push the button again and stop it. It doesn't do that now does it? So in that sense it doesn't work.

So my question to you is, what do you want it to do? What is your definition of "it works"?

Alright I will try to do my best to explain it.

When arduino startsup it will print hi over and over again in the serial monitor. When I press the button it will stop that. So it will no longer print hi in the serial monitor. But when I press the button again it should start printing hi in the serial monitor again.

So when I understand how that works I can make it the code more advanced. The hi message is just only to know and see what is happening.

while (test < 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print("hi");
    Serial.print('\n');
    if (test == 0) {
      break;
    }
  }

OK, I get why that works. You’ve got that break statement to get you out of the loop if test is 0. Well, you know it is 0 because otherwise you wouldn’t have gone into the loop. Therefore, this loop can only run once at a time.

So it is exactly equivalent to this:

if (test < 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print("hi");
    Serial.print('\n');
  }

I don’t know why you tried to write it as a while loop.

What I don’t understand is how you are setting this test variable. If the button is pressed, you check the state of the LED for some reason to decide how to set test. What does the state of the led have to do with it? If you want to change test each time the button is pressed, then do that.

Pseudocode:

if (the button was just pressed)
{
     if test is 0 make it 1
     else make test 0
}
if (test is 0){
    do your print stuff and control your LED
}

Delta_G:

while (test < 1) {

digitalWrite(ledPin, HIGH);
    Serial.print(“hi”);
    Serial.print(’\n’);
    if (test == 0) {
      break;
    }
  }




OK, I get why that works. You've got that break statement to get you out of the loop if test is 0. Well, you know it is 0 because otherwise you wouldn't have gone into the loop. Therefore, this loop can only run once at a time. 

So it is exactly equivalent to this:



if (test < 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print(“hi”);
    Serial.print(’\n’);
  }




I don't know why you tried to write it as a while loop.


What I don't understand is how you are setting this test variable. If the button is pressed, you check the state of the LED for some reason to decide how to set test. What does the state of the led have to do with it? If you want to change test each time the button is pressed, then do that. 

Pseudocode:



if (the button was just pressed)
{
    if test is 0 make it 1
    else make test 0
}
if (test is 0){
    do your print stuff and control your LED
}

Thnx for that quick reply. I will take a look at it tomorrow cause it is 00:17 in the netherlands right now. I will let you know if it worked.

Delta_G:

while (test < 1) {

digitalWrite(ledPin, HIGH);
    Serial.print(“hi”);
    Serial.print(’\n’);
    if (test == 0) {
      break;
    }
  }




OK, I get why that works. You've got that break statement to get you out of the loop if test is 0. Well, you know it is 0 because otherwise you wouldn't have gone into the loop. Therefore, this loop can only run once at a time. 

So it is exactly equivalent to this:



if (test < 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print(“hi”);
    Serial.print(’\n’);
  }




I don't know why you tried to write it as a while loop.


What I don't understand is how you are setting this test variable. If the button is pressed, you check the state of the LED for some reason to decide how to set test. What does the state of the led have to do with it? If you want to change test each time the button is pressed, then do that. 

Pseudocode:



if (the button was just pressed)
{
    if test is 0 make it 1
    else make test 0
}
if (test is 0){
    do your print stuff and control your LED
}

I got it working with this code

//Button Toggle LED
int test = 0;
int ledPin = 13;
int buttonPin = 3;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
boolean ledState = LOW;//storage for the current state of the LED (off/on)
void setup() {
  pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);//initialize Serial connection
}

void loop() {
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW) {
    delay(1);
    if (test == 0) {
      test = 1;
      Serial.print(test);
    }
    else {
      test = 0;
      Serial.print(test);
    }
    if (test == 0) {
    }
  }
  lastState = currentState;
  if (test == 0) {
    digitalWrite(ledPin, HIGH);
    Serial.print("hi");
    Serial.print('\n');
  }
    if (test == 1) {
    digitalWrite(ledPin, HIGH);
    Serial.print("doei");
    Serial.print('\n');
  }
}
    if (test == 0) {
    }

?