#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h> // Include the Wi-Fi library
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Define the pin numbers for the traffic lights
// (Your existing pin definitions here)
// Define the pin numbers for the Hall effect sensors
// (Your existing pin definitions here)
// Define the I2C pins for the LCD module
// (Your existing pin definitions here)
// Define the durations for each traffic light state (in milliseconds)
// (Your existing duration definitions here)
// Define the states for each traffic light
// (Your existing enum definition here)
// Initialize the current state for each traffic light to red
// (Your existing signalState1 and signalState2 definitions here)
// Initialize the LCD module
// Set the LCD I2C address and dimensions
// Set the pin modes for the traffic lights
// (Your existing pin mode definitions here)
// Set the pin modes for the Hall effect sensors
// (Your existing pin mode definitions here)
// Initialize all traffic lights to red
// Define the pin numbers for the traffic lights
const int redPin1 = 21;
const int yellowPin1 = 22;
const int greenPin1 = 23;
const int redPin2 = 15;
const int yellowPin2 = 2;
const int greenPin2 = 0;
// Define the pin numbers for the Hall effect sensors
const int sensorPin1 = 33;
const int sensorPin2 = 34;
// Define the I2C pins for the LCD module
const int i2cSDAPin = 4; // SDA pin
const int i2cSCLPin = 5; // SCL pin
// Define the durations for each traffic light state (in milliseconds)
const unsigned long greenDuration = 15000; // 15 seconds
const unsigned long yellowDuration = 3000; // 3 seconds
const unsigned long redDuration = 10000; // 10 seconds
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long remainingTime = 0;
// Define the states for each traffic light
enum SignalState {
RED,
YELLOW,
GREEN
};
// Initialize the current state for each traffic light to red
SignalState signalState1 = RED;
SignalState signalState2 = RED;
// Initialize the LCD module
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address and dimensions
void setup() {
// Set the pin modes for the traffic lights
pinMode(redPin1, OUTPUT);
pinMode(yellowPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(yellowPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
// Set the pin modes for the Hall effect sensors
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
// Initialize all traffic lights to red
Wire.begin(4, 5);
lcd.init();
lcd.backlight();
digitalWrite(redPin1, HIGH);
digitalWrite(redPin2, HIGH);
Wire.begin(4, 5);
lcd.init();
lcd.backlight();
// (Your existing digitalWrite(redPin1, HIGH) and digitalWrite(redPin2, HIGH) here)
// Initialize the LCD module
lcd.begin(16, 2); // Initialize the LCD with the specified dimensions
lcd.print("Traffic Lights"); // Display a message on the LCD
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("System");
delay(7000); // Wait for 2 seconds
lcd.clear(); // Clear the LCD screen
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Check the state of Hall effect sensor 1
if (digitalRead(sensorPin1) == HIGH) {
// Traffic light sequence for direction 1
signalState1 = RED;
digitalWrite(redPin1, HIGH);
delay(redDuration);
startTime = millis(); // Store the current time
while (elapsedTime < redDuration) {
elapsedTime = millis() - startTime;
remainingTime = redDuration - elapsedTime;
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState1 = YELLOW;
digitalWrite(redPin1, LOW);
digitalWrite(yellowPin1, HIGH);
digitalWrite(greenPin1, LOW);
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal 1: YELLOW"); // Update the LCD display
delay(yellowDuration);
startTime = millis(); // Store the current time
while (elapsedTime < yellowDuration) {
elapsedTime = millis() - startTime;
remainingTime = yellowDuration - elapsedTime;
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState1 = GREEN;
digitalWrite(greenPin1, HIGH);
digitalWrite(yellowPin1, LOW);
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal 1: GREEN "); // Update the LCD display
delay(greenDuration);
startTime = millis(); // Store the current time
while (elapsedTime < greenDuration) {
elapsedTime = millis() - startTime;
remainingTime = greenDuration - elapsedTime;
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal1:slowdown"); // Update the LCD display
digitalWrite(greenPin1, HIGH);
delay(500);
digitalWrite(greenPin1, LOW);
delay(500);
digitalWrite(greenPin1, HIGH);
delay(500);
digitalWrite(greenPin1, LOW);
delay(500);
digitalWrite(greenPin1, HIGH);
delay(500);
digitalWrite(greenPin1, LOW);
delay(500);
digitalWrite(greenPin1, HIGH);
delay(500);
digitalWrite(greenPin1, LOW);
delay(500);
digitalWrite(greenPin1, HIGH);
delay(500);
digitalWrite(greenPin1, LOW);
delay(500);
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal 1: YELLOW"); // Update the LCD display
signalState1 = YELLOW;
digitalWrite(redPin1, LOW);
digitalWrite(yellowPin1, HIGH);
digitalWrite(greenPin1, LOW);
delay(yellowDuration);
startTime = millis(); // Store the current time
while (elapsedTime < yellowDuration) {
elapsedTime = millis() - startTime;
remainingTime = yellowDuration - elapsedTime;
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState1 = RED;
digitalWrite(yellowPin1, LOW);
digitalWrite(redPin1, HIGH);
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal 1: RED "); // Update the LCD display
} else {
// If no car is detected, keep the signal in direction 1 red
signalState1 = RED;
digitalWrite(redPin1, HIGH);
digitalWrite(yellowPin1, LOW);
digitalWrite(greenPin1, LOW);
lcd.setCursor(0, 0); // Move the cursor to the first row
lcd.print("Signal 1: RED "); // Update the LCD display
}
if (digitalRead(sensorPin2) == HIGH) {
// Traffic light sequence for direction 1
signalState2 = RED;
digitalWrite(redPin2, HIGH);
delay(redDuration);
startTime = millis(); // Store the current time
while (elapsedTime < redDuration) {
elapsedTime = millis() - startTime;
remainingTime = redDuration - elapsedTime;
lcd.setCursor(0, 0); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState2 = YELLOW;
digitalWrite(redPin2, LOW);
digitalWrite(yellowPin2, HIGH);
digitalWrite(greenPin2, LOW);
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal 2: YELLOW"); // Update the LCD display
delay(yellowDuration);
startTime = millis(); // Store the current time
while (elapsedTime < yellowDuration) {
elapsedTime = millis() - startTime;
remainingTime = yellowDuration - elapsedTime;
lcd.setCursor(0, 0); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState2 = GREEN;
digitalWrite(greenPin2, HIGH);
digitalWrite(yellowPin2, LOW);
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal 2: GREEN "); // Update the LCD display
delay(greenDuration);
startTime = millis(); // Store the current time
while (elapsedTime < greenDuration) {
elapsedTime = millis() - startTime;
remainingTime = greenDuration - elapsedTime;
lcd.setCursor(0, 0); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal2:slowdown"); // Update the LCD display
digitalWrite(greenPin2, HIGH);
delay(500);
digitalWrite(greenPin2, LOW);
delay(500);
digitalWrite(greenPin2, HIGH);
delay(500);
digitalWrite(greenPin2, LOW);
delay(500);
digitalWrite(greenPin2, HIGH);
delay(500);
digitalWrite(greenPin2, LOW);
delay(500);
digitalWrite(greenPin2, HIGH);
delay(500);
digitalWrite(greenPin2, LOW);
delay(500);
digitalWrite(greenPin2, HIGH);
delay(500);
digitalWrite(greenPin2, LOW);
delay(500);
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal 2: YELLOW"); // Update the LCD display
signalState2 = YELLOW;
digitalWrite(redPin2, LOW);
digitalWrite(yellowPin2, HIGH);
digitalWrite(greenPin2, LOW);
delay(yellowDuration);
startTime = millis(); // Store the current time
while (elapsedTime < yellowDuration) {
elapsedTime = millis() - startTime;
remainingTime = yellowDuration - elapsedTime;
lcd.setCursor(0, 0); // Move the cursor to the second row
lcd.print("Time: ");
lcd.print(remainingTime / 1000); // Convert milliseconds to seconds
lcd.print(" seconds "); // Update the LCD display
// Rest of your code here
}
startTime = 0;
elapsedTime = 0;
remainingTime = 0;
signalState2 = RED;
digitalWrite(yellowPin2, LOW);
digitalWrite(redPin2, HIGH);
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal 2: RED "); // Update the LCD display
} else {
// If no car is detected, keep the signal in direction 1 red
signalState2 = RED;
digitalWrite(redPin2, HIGH);
digitalWrite(yellowPin2, LOW);
digitalWrite(greenPin2, LOW);
lcd.setCursor(0, 1); // Move the cursor to the first row
lcd.print("Signal 2: RED "); // Update the LCD display
}
// Check for street congestion
bool isStreetCrowded = (signalState1 == GREEN || signalState2 == GREEN);
// Add your congestion handling logic here
}