How to use infinite loop and LED not working to my condition

hello there, currently im working on a project for my university. Im working on smart emergency traffic lights where whenever an ambulance is rushing to the hospital, with a push of a button, the traffic lights it passes through will be green. Im having problem with making it into an infinite loop and changing the lights back to normal. For now im working on a small scale where let say button 1 controls the red light, button 2 = yellow and button 3 = green.
my problem is when i pressed button 1, the red lights is turned on but when i press it again, it wont turn off.

here is my code

const int buttonPin1 = 12;     // the number of the pushbutton pin
const int buttonPin2 = 1;
const int buttonPin3 = 11;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  
  pinMode(5, OUTPUT); //TF1 Red

  pinMode(6, OUTPUT); //TF1 Orange

  pinMode(7, OUTPUT); //TF1 Green
  

}

void loop() {
  
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  while (1) {
  if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW) {
    digitalWrite(5, HIGH); // TURN On TF1 RED

    digitalWrite(6, LOW); // TURN OFF TF1 ORANGE

    digitalWrite(7, LOW); // TURN OFF TF1 GREEN
  } 
  else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW) {
    digitalWrite(5, LOW); // TURN OFF TF1 RED

    digitalWrite(6, HIGH); // TURN OFF TF1 ORANGE

    digitalWrite(7, LOW); // TURN OFF TF1 GREEN
  }
  else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH) {
    digitalWrite(5, LOW); // TURN OFF TF1 RED

    digitalWrite(6, LOW); // TURN OFF TF1 ORANGE

    digitalWrite(7, HIGH); // TURN OFF TF1 GREEN
  }
  else {
    digitalWrite(5, LOW); // TURN OFF TF1 RED

    digitalWrite(6, LOW); // TURN OFF TF1 ORANGE

    digitalWrite(7, LOW); // TURN OFF TF1 GREEN
  }
}
}

oh btw i inserted the "while (1)" so that it can be in an infinite loop but it doesnt work. Any form of help is appreciated :slight_smile:

  buttonState1 = digitalRead(buttonPin1);

buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  while (1) {

So how can these variables ever change once you're inside the while(1) loop?

Don't use while(1). The loop() function does this for you.

MorganS:
So how can these variables ever change once you're inside the while(1) loop?

Don't use while(1). The loop() function does this for you.

oh i see. but how to make it into an infinite loop where after, lets say i press button 1 which will turn on the red lights and then press it again to turn it off. After it is turned off, when i press another button, another led will light up. Is that possible because i have tried almost everything but it still remains off after the first loop

Stop thinking in terms of infinite loops. The loop() function is already called in an infinite loop. That is the ONLY infinite loop you need.

On any given pass through loop(), it may, or may not, be time to do something.

There may be a number of somethings that need doing, at various times, so it is not a simple matter of checking to see if it is time to do one thing.

If it is time to do a thing, do it, and decide whether that requires doing the same thing again later, or requires doing something else later. If something else needs to happen, make sure that this thing isn't going to happen again, and set up so that the other thing can happen later.

That is the whole concept of a state machine in a nutshell.

You have some state(s) that you can be in. You have some event(s) that cause(s) a transition. You have some action(s) to perform when a transition is needed. The transitions may add or remove states.

For instance, the Arduino might be powered off. Then, the events (cars waiting at the lights, and pedestrians pounding the buttons to cross both ways at all 4 corners) don't matter.

You need to forget about the code for now. Get a piece of paper. Draw some circles. Each represents a state (traffic flowing east and west, traffic flowing east and turning north; pedestrians on the northeast corner wanting to go south, etc.

Some things have to happen to get from one state to another (a car approached the intersection from the south wanting to go north, a car approached from the east wanting to go south, a pedestrian wants to cross on the north side of the intersection).

When transitions are to happen, some things have to be done (the northbound green light has to turn yellow, etc.)

Some events are based on time (a light is not infinitely red, green, or yellow). Some are based on external events (cars at the intersection, people pressing switched).

You need to make a drawing that shows all the states that you can think of. You need to explain that drawing to your dog/teddy bear/wife/mother/whatever, which will cause you to think of other states.

You need to draw lines between each state. On the top of the line, define what event causes the transition to the next state. Below the line, list what needs to happen to make the transition.

Only when you have done that can you begin to write code.

Your code then needs to check for external events (car detected in northbound lane, car in northbound lane wanting to turn east, car in northbound lane wanting to turn west, pedestrian on south west corner wanting to go north, etc.) and for time-based events (the northbound light has been yellow for 10 seconds, etc.).

For each event, you need to establish a priority (light has been yellow for 10 seconds gets top priority, a pedestrian on the northeast corner wanting to go west gets a different priority, a car in the northbound left turn lane gets another priority).

You need to then execute the task with the highest priority.

Priorities change over time. A car that has been waiting, to turn left to go east, for 20 minutes is not acceptable.

You might be getting the impression that building a decent controller for a big, busy intersection is not a trivial task. You'd be right.

loop() this is also infinite loop. No need again one super loop.

void loop(){
//put your code here. its run again and again.
}

The demo Several Things at a Time may help you understand the concept of relying on loop() for repetition.

...R