const int redLED = 2; // Red LED pin for traffic light
const int yellowLED = 3; // Yellow LED pin for traffic light
const int greenLED = 4; // Green LED pin for traffic light
const int pedestrianButton = 5; // Pedestrian pushbutton pin
const int reedSwitch = 6; // Reed switch input pin
const int rgbRed = 9; // Red segment of the RGB LED
const int rgbGreen = 10; //Green segment of RGB LED
const int rgbBlue = 11; // Green segment of the RGB LED
const unsigned long greenDuration = 40000; // Green Light Duration
const unsigned long originalRedDuration = 50000; // Original red light duration
const unsigned long yellowDuration = 3000; // Yellow light duration
const unsigned long extendedRedDuration = 65000; //Extended Red Duration for Pedestrian Crossing
unsigned long lastMillis = 0;
unsigned long currentMillis = 0;
boolean pedestrianCrossing = false;
boolean vehicleDetected = false;
char currentTrafficState = 'R'; //R = RED, G = Green, Y = Yellow
void setup() {
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(pedestrianButton, INPUT_PULLUP);
pinMode(reedSwitch, INPUT_PULLUP);
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
updateTrafficLight('R');
analogWrite(rgbBlue,0);
analogWrite(rgbGreen,255);
analogWrite(rgbRed,0);
Serial.begin(9600);
}
void loop() {
//Serial Debugging
//Serial.print("Pedestrian Crossing State :");
//Serial.println(pedestrianCrossing);
//Serial.print("Pedestrian Button :");
//Serial.println(digitalRead(pedestrianButton));
currentMillis = millis();
if (digitalRead(pedestrianButton) == LOW){
pedestrianCrossing = true;
}
else{}
if (digitalRead(reedSwitch) == LOW) {
vehicleDetected = true;
} else {
vehicleDetected = false;
}
switch (currentTrafficState) {
case 'R':
if (pedestrianCrossing == false && currentMillis - lastMillis >= originalRedDuration) {
updateTrafficLight('G');
analogWrite(rgbRed, 255); // Turn of Pedestrian Red Light
analogWrite(rgbGreen, 0); // Turn Turn on Pedestrian Green Light
}
else if (pedestrianCrossing == true && currentMillis - lastMillis >= extendedRedDuration){
updateTrafficLight('G');
analogWrite(rgbRed, 0); // Turn of Pedestrian Red Light
analogWrite(rgbGreen, 255); // Turn Turn on Pedestrian Green Light
pedestrianCrossing = false;
}
break;
case 'G':
if (vehicleDetected == false && currentMillis - lastMillis >= greenDuration) {
updateTrafficLight('Y');
analogWrite(rgbGreen, 0); // Turn off Pedestrian Green Light
analogWrite(rgbRed, 255); // Turn on Pedestrian Stop
}
if (vehicleDetected == true) {
updateTrafficLight('G');
}
break;
case 'Y':
if (currentMillis - lastMillis >= yellowDuration) {
updateTrafficLight('R');
analogWrite(rgbRed, 0); // Turn off Pedestrian stop Light
analogWrite(rgbGreen, 255); // Turn on Pedestrian go light
}
break;
}
}
void updateTrafficLight(char newTrafficState) {
currentTrafficState = newTrafficState;
lastMillis = currentMillis;
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
switch (currentTrafficState) {
case 'R':
digitalWrite(redLED, HIGH);
break;
case 'G':
digitalWrite(greenLED, HIGH);
break;
case 'Y':
digitalWrite(yellowLED, HIGH);
break;
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
r1:1
r1:2
r4:1
r4:2
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
r2:1
r2:2
r3:1
r3:2
r6:1
r6:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
r5:1
r5:2