Variable Not Changing when Button Pressed

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

Hi, @itsmimoo
Welcome to the forum.

// Define constants for button pins
const int button1 = 1; // Button for requesting pedestrian crossing
const int button2 = 0; // Another button for requesting pedestrian crossing

Are you using an Arduino UNO or Nano?
If so then pins 0 and 1 are programing ins and should be avoided for other tasks.

if not a UNO or Nano.
How have you got your buttons wired?
Between input pin and 5V
or
Between input pin and gnd.

If to gnd you will need a 10K resistor between the input pin and 5V, to pull the input HIGH when the button is open.

If to 5V you will need a 10K resistor between the input pin and gnd, to pull the input LOW when the button is open.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Tom.. :smiley: :+1: :coffee: :australia:

i just made this. i tried putting the code in this software but the result is the same as irl.

i just saw it, yeah i will look into it, thank you for your advice and patience.. im new to all of this so i can only use basic functions. my teacher told me to figure it out on my own so i asked here since i def cant do it on my own

  • 1Meg resistors are too large.

  • Do you know what a State Machine is ?

  • NEVER ever use delay(. . .) in your sketches, unless of course you know what doing so can result in.
    Google Arduino millis TIMERS

  • As mentioned, do not use D0 or D1, these are used for communications.
    You can use A0-A5 as digital inputs too.

thank you, yeah i’ve done a new code and it works, using millis. also, i wont put more things in that arduino project so i used D0 D1. and ywah the resistors are at 330ohm now it was a mistake.