hi, i've been trying to make an intersection of traffic lights, 2 are for pedestrians and 2 for cars. there are 2 buttons that should start the cycle for the pedestrian traffic light (when cartrafficlight2 turns red). the problem is that the variable "mode" doesnt seem to change when i press the button, also, it starts with the Pedestrian cycle for some reason, even if i dont press it.. the wiring is done good...
// Define constants for the traffic light pins
const int redCar1 = 13; // Red light for car lane 1
const int greenCar1 = 12; // Green light for car lane 1
const int yellowCar1 = 11; // Yellow light for car lane 1
const int redCar2 = 10; // Red light for car lane 2
const int greenCar2 = 9; // Green light for car lane 2
const int yellowCar2 = 8; // Yellow light for car lane 2
// Define constants for the pedestrian light pins
const int redPedestrian1 = 7; // Red light for pedestrian lane 1
const int greenPedestrian1 = 6; // Green light for pedestrian lane 1
const int yellowPedestrian1 = 5; // Yellow light for pedestrian lane 1
const int redPedestrian2 = 4; // Red light for pedestrian lane 2
const int greenPedestrian2 = 3; // Green light for pedestrian lane 2
const int yellowPedestrian2 = 2; // Yellow light for pedestrian lane 2
// Define constants for button pins
const int button1 = 1; // Button for requesting pedestrian crossing
const int button2 = 0; // Another button for requesting pedestrian crossing
int mode = 0; // Mode variable to switch between car and pedestrian lights
void setup() {
// Set all car light pins as OUTPUT
pinMode(redCar1, OUTPUT);
pinMode(greenCar1, OUTPUT);
pinMode(yellowCar1, OUTPUT);
pinMode(redCar2, OUTPUT);
pinMode(greenCar2, OUTPUT);
pinMode(yellowCar2, OUTPUT);
// Set all pedestrian light pins as OUTPUT
pinMode(redPedestrian1, OUTPUT);
pinMode(greenPedestrian1, OUTPUT);
pinMode(yellowPedestrian1, OUTPUT);
pinMode(redPedestrian2, OUTPUT);
pinMode(greenPedestrian2, OUTPUT);
pinMode(yellowPedestrian2, OUTPUT);
// Initialize button pins
digitalWrite(button1, LOW); // Set button1 to LOW
digitalWrite(button2, LOW); // Set button2 to LOW
pinMode(button1, INPUT); // Set button1 as INPUT
pinMode(button2, INPUT); // Set button2 as INPUT
}
void loop() {
// Check if either button is pressed
if (digitalRead(button1) == HIGH || digitalRead(button2) == HIGH) {
mode = 1; // Switch to pedestrian mode if a button is pressed
}
// Choose the traffic light cycle based on the mode
if (mode == 0) {
carTrafficLightCycle(); // Run car traffic light cycle
} else {
pedestrianTrafficLightCycle(); // Run pedestrian traffic light cycle
}
}
// Function to manage the car traffic light cycle
void carTrafficLightCycle() {
// Turn on red lights for pedestrians
digitalWrite(redPedestrian1, HIGH);
digitalWrite(redPedestrian2, HIGH);
// Car green light for lane 1
digitalWrite(redCar1, LOW);
digitalWrite(yellowCar1, LOW);
digitalWrite(greenCar1, HIGH);
delay(3000); // Keep green light on for 3 seconds
// Change to yellow light for lane 1
digitalWrite(greenCar1, LOW);
digitalWrite(yellowCar1, HIGH);
delay(3000); // Keep yellow light on for 3 seconds
// Switch off car lights and prepare for next cycle
digitalWrite(redCar1, HIGH);
delay(500); // Short delay
digitalWrite(redCar2, LOW); // Activate car lane 2
digitalWrite(yellowCar1, LOW);
delay(1500); // Keep lane 2 green for 1.5 seconds
// Activate green light for car lane 2
digitalWrite(greenCar2, HIGH);
delay(3000); // Keep green light on for 3 seconds
// Prepare to switch back to red lights
digitalWrite(redCar1, HIGH);
digitalWrite(greenCar2, HIGH);
digitalWrite(greenCar2, LOW);
delay(3000); // Keep red lights for 3 seconds
// Final state to reset
digitalWrite(redCar1, HIGH);
digitalWrite(greenCar2, LOW);
digitalWrite(redCar2, HIGH);
delay(1000); // Short delay before the next cycle
}
// Function to manage the pedestrian traffic light cycle
void pedestrianTrafficLightCycle() {
// Turn on red lights for pedestrians
digitalWrite(redPedestrian1, LOW);
digitalWrite(redPedestrian2, LOW);
// Activate green lights for pedestrians
digitalWrite(greenPedestrian1, HIGH);
digitalWrite(greenPedestrian2, HIGH);
delay(2000); // Keep green light on for 2 seconds
// Switch off pedestrian green lights
digitalWrite(greenPedestrian1, LOW);
digitalWrite(greenPedestrian2, LOW);
// Activate yellow lights for pedestrians
digitalWrite(yellowPedestrian1, HIGH);
digitalWrite(yellowPedestrian2, HIGH);
delay(2000); // Keep yellow light on for 2 seconds
// Turn off pedestrian yellow lights
digitalWrite(yellowPedestrian1, LOW);
digitalWrite(yellowPedestrian2, LOW);
mode = 0; // Reset mode to car traffic lights
}
this is the code
