# Crossroad Traffic Light Controller Arduino/ESP32 project for controlling a simple two-way crossroad traffic light system with pedestrian buttons. The sketch is in `crossroad.ino`. ## Features - Controls North/South and East/West traffic lights. - Uses two pedestrian buttons. - Runs with a non-blocking `millis()` state machine. - Prints state changes and countdown logs to the Serial Monitor. - Uses button debounce to avoid repeated false presses. ## Hardware This project is written for an ESP32. ### Traffic Light Pins | Direction | Color | GPIO | | --- | --- | --- | | North/South | Red | 23 | | North/South | Yellow | 22 | | North/South | Green | 21 | | East/West | Red | 19 | | East/West | Yellow | 18 | | East/West | Green | 5 | ### Pedestrian Button Pins | Button | GPIO | | --- | --- | | Pedestrian button 1 | 32 | | Pedestrian button 2 | 33 | ## Wiring Notes ### LEDs The code uses active-low LEDs: ```cpp const int LED_ON = LOW; const int LED_OFF = HIGH; ``` That means an LED turns on when the ESP32 pin is `LOW`. Typical wiring: ```text 3.3V -> resistor -> LED -> GPIO pin ``` If your LEDs are wired from GPIO to resistor to GND, the lights may behave inverted. ### Buttons The pedestrian buttons use `INPUT_PULLUP`: ```cpp pinMode(PED_BTN_1, INPUT_PULLUP); pinMode(PED_BTN_2, INPUT_PULLUP); ``` Each button should be wired like this: ```text GPIO 32 -> button -> GND GPIO 33 -> button -> GND ``` When the button is not pressed, the pin reads `HIGH`. When the button is pressed, the pin connects to GND and reads `LOW`. ## Timing | Event | Duration | | --- | --- | | Normal green light | 15 seconds | | Normal yellow light | 3 seconds | | Wait after pedestrian button press | 3 seconds | | Pedestrian both-yellow phase | 3 seconds | | Pedestrian both-red phase | 10 seconds | | Button debounce | 50 milliseconds | ## Normal Traffic Flow The normal traffic cycle is: ```text NS green, EW red NS yellow, EW red EW green, NS red EW yellow, NS red repeat ``` ## Pedestrian Flow When either pedestrian button is pressed: 1. The request is registered. 2. The controller waits 3 seconds. 3. Both directions turn yellow. 4. After 3 seconds, both directions turn red. 5. After 10 seconds, the controller resumes the traffic state that was active when the button was pressed. ## Serial Monitor Open the Serial Monitor at: ```text 115200 baud ``` Example logs: ```text STATE: NS GREEN, EW RED TIME BEFORE CHANGE (NS GREEN, EW RED): 15 sec TIME BEFORE CHANGE (NS GREEN, EW RED): 14 sec PEDESTRIAN REQUEST REGISTERED PEDESTRIAN: will resume NS GREEN, EW RED PEDESTRIAN: both yellow starts in 3 sec STATE: BOTH YELLOW TIME BEFORE CHANGE (BOTH YELLOW): 3 sec STATE: BOTH RED TIME BEFORE CHANGE (BOTH RED): 10 sec STATE: NS GREEN, EW RED ``` ## Uploading 1. Open `crossroad.ino` in the Arduino IDE. 2. Select your ESP32 board. 3. Select the correct USB port. 4. Upload the sketch. 5. Open Serial Monitor at `115200` baud to see logs.