Trying to make a button enable a loop of code or disable it.

I have been trying to make a button basically enable the code or disable it like an on and off switch. I am making a laser alarm and I want to be able to activate or deactivate it depending on the button press. I have gotten close but I only get it to one the code once, versus a loop (the photo cell needs to keep reading the light, not just once) So when the red light is on(armed) I need to run the photo cell loop to make sure its scanning for light. When I press the button again and green light comes on, I want to disable that loop. I have tried using the while loop, switches, and different things but I can't seem to get it working.

Any help?! Thanks
(The code does not have my attempts because they didn't work)

const int yellow = 2;
const int green = 3;
const int red = 4;

const int buttonPin = 7;

int laser;



int redCurrent;
int redPrevious = LOW;
int redState = HIGH;

int greenCurrent;
int greenPrevious = LOW;
int greenState = LOW;

void setup() {
  Serial.begin(9600);
  
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);

  pinMode(buttonPin, INPUT_PULLUP);
  


}

void loop() { 

  
  redCurrent = digitalRead(buttonPin);
  if (redCurrent == HIGH && redPrevious == LOW)
  {
    if (redState == HIGH) 
    {
    redState = LOW;
    greenState = HIGH;
    }
    else 
    {
  redState = HIGH;
  greenState = LOW;
}


{
  while (redCurrent == LOW);
  delay(50);
}

  }

  digitalWrite(red, redState);
  digitalWrite(green, greenState);
  redPrevious = redCurrent;
  greenPrevious = greenCurrent;
  
  }

Thank you for using code tags !


If you want to convert a momentary switch to ON/OFF control (in software), toggle the state of a variable each time the switch changes from example, HIGH to LOW.

Use this variable to enable/disable code as needed.

BTW add comments to your sketches :wink:


Always show us a good schematic of your circuit.

You don’t need a while, or any other loop other than the loop() function itself, for this...

void loop() { 

  
  redCurrent = digitalRead(buttonPin);
  if (redCurrent != redPrevious)
  {
    if (redCurrent = LOW)
    {
       redState = !redState; //toggle state on press
       digitalWrite(red, redState); //update led state
    }
    delay(50); //debounce
    redPrevious = redCurrent;
  }

   if (redState)
   {
      //switch is currently toggled ON
      //do your check for your photo light here
      //do NOT loop
   }
   else
   {
      //switch is currently toggled OFF
    }
}

Not quite sure what you were trying here...

{
  while (redCurrent == LOW);
  delay(50);
}

If redCurrent == LOW that while will loop forever because there is no way redCurrent will ever be set to anything other than LOW again.
Also the semicolon at the end of the while ends the loop and you have a set of braces { } In apparently the wrong place.

This is the correct syntax for a while that actually repeats something...

  while (redCurrent == LOW) {
     delay(50);
     // add some code which might potentially somehow set redCurrent != LOW at some point
     // e.g. redCurrent = digitalRead(buttonPin);
   }

larryd:
Thank you for using code tags !


If you want to convert a momentary switch to ON/OFF control (in software), toggle the state of a variable each time the switch changes from example, HIGH to LOW.

Use this variable to enable/disable code as needed.

BTW add comments to your sketches :wink:


Always show us a good schematic of your circuit.

I have tried doing that already but somehow it was not working with the way the code was setup

“I have tried doing that already but somehow it was not working with the way the code was setup”

Well you are the one who set up the code :wink:

Show us the attempt.

Schematic ?

pcbbc:
You don’t need a while for this...

void loop() { 

redCurrent = digitalRead(buttonPin);
 if (redCurrent != redPrevious)
 {
   if (redCurrent = LOW)
   {
      redState = !redState; //toggle state on press
      digitalWrite(red, redState); //upset led state
   }
   delay(50); //debounce
   redPrevious = redCurrent;
 }

if (redState)
  {
     //switch is currently toggled ON
  }
  else
  {
     //switch is currently toggled OFF
   }
}




Not quite sure what you were trying here...


{
 while (redCurrent == LOW);
 delay(50);
}

I believe I need the while loop to keep running the loop that I want when it's active. Using if statements, the code only runs once. At least that's what happened when I did that before

I did have the while there only as a de bouncer but I had it formatted wrong apparently

Bomberguy111:
I believe I need the while loop to keep running the loop that I want when it's active.

You’re wrong. loop() will automatically loop, and redState will remember the toggled state of the switch.
There’s no need for additional looping. Just check redState and decide if the system is armed or disarmed.

Using if statements, the code only runs once. At least that's what happened when I did that before

I doubt this is the same code that you tried before. And you haven’t shown us the code you tried before so we can’t validate that.

pcbbc:
You’re wrong. loop() will automatically loop, and redState will remember the toggled state of the switch.
There’s no need for additional looping. Just check redState and decide if the system is armed or disarmed.

I need to have the loop there to keep checking of the laser beam has been broken or not

larryd:
“I have tried doing that already but somehow it was not working with the way the code was setup”

Well you are the one who set up the code :wink:

Show us the attempt.

Schematic ?

I don't have a schematic but basically right now its just 2 leds and a button. Here is my "attempt"

const int yellow = 2;
const int green = 3;
const int red = 4;

const int buttonPin = 7;

int laser;

int AlarmStatus;

int redCurrent;
int redPrevious = LOW;
int redState = HIGH;

int greenCurrent;
int greenPrevious = LOW;
int greenState = LOW;

void setup() {
  Serial.begin(9600);
  
  pinMode(yellow, OUTPUT); //Yellow LED
  pinMode(green, OUTPUT); //Green LED
  pinMode(red, OUTPUT); //Red LED

  pinMode(buttonPin, INPUT_PULLUP); //Button input
}

void loop() { 

  
  redCurrent = digitalRead(buttonPin);
  if (redCurrent == HIGH && redPrevious == LOW) //Checking button state
  {
    if (redState == HIGH) 
    {
    redState = LOW;  //Deactive
    greenState = HIGH;
    AlarmStatus = 0;
    }
    else 
    {
  redState = HIGH; //Active
  greenState = LOW;
  AlarmStatus = 1;
}
  while (AlarmStatus == 1) { //ATTEMPT. Trying to make this loop run only when its active.
  Serial.println("Active");
  
  }
  digitalWrite(red, redState);
  digitalWrite(green, greenState);
  redPrevious = redCurrent;
  greenPrevious = greenCurrent;
  
  }
  }
[code]

Bomberguy111:
I don't have a schematic

So, make one. I often use scrap paper and pen.