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