#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int ped_red = 13;
const int ped_green = 12;
const int car_green = 4;
const int car_yellow = 3;
const int car_red = 2;
const int BUTTON = 8;
int BUTTONstate = 0;
// Initialize the LCD, set the address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
pinMode(ped_red, OUTPUT);
pinMode(ped_green, OUTPUT);
pinMode(car_green, OUTPUT);
pinMode(car_yellow, OUTPUT);
pinMode(car_red, OUTPUT);
pinMode(BUTTON, INPUT);
// Initialize the LCD with 16 columns and 2 rows
lcd.begin(16, 2);
lcd.backlight();
}
void loop()
{
// Turned off traffic light (LED BLINKING)
digitalWrite(car_green, HIGH);
digitalWrite(ped_red, HIGH);
delay(100);
digitalWrite(ped_red, LOW);
delay(400);
// Update LCD for pedestrian waiting
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pedestrian Waiting");
int BUTTONstate = digitalRead(BUTTON); // int buttonState = digitalRead(buttonPin);
if (BUTTONstate == HIGH) // when button pressed, system on
{
digitalWrite(car_green, LOW);
digitalWrite(ped_red, HIGH);
blinkcar_yellow(5);
// Update LCD for pedestrian crossing
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pedestrian Crossing");
digitalWrite(ped_green, HIGH);
digitalWrite(car_red, HIGH);
digitalWrite(ped_red, LOW);
delay(5000);
digitalWrite(ped_green, LOW);
digitalWrite(car_red, LOW);
}
else {
digitalWrite(ped_green, LOW);
delay(0);
}
}
void blinkcar_yellow(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(car_yellow, HIGH); // Turn on the yellow LED
delay(500); // Wait 500ms
digitalWrite(car_yellow, LOW); // Turn off the yellow LED
delay(500); // Wait 500ms
}
}