int red = A0; // Assigning pin A0 to the red LED
int yellow = A1; // Assigning pin A1 to the yellow LED
int green = A2; // Assigning pin A2 to the green LED
int yellowTime = 5; // Time for yellow light to blink
int greenTime = 30; // Time for green light
int redTime = 45; // Time for red light
int num_array[10][7] = { // Array to store binary representation of numbers for 7-segment display
{1,1,1,1,1,1,0}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,0,1}, //2
{1,1,1,1,0,0,1}, //3
{0,1,1,0,0,1,1}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1}, //6
{1,1,1,0,0,0,0}, //7
{1,1,1,1,1,1,1}, //8
{1,1,1,1,0,1,1} //9
};
void setup() {
for (int i = 0; i <= 13; i++) { // Setting all pins from 0 to 13 as OUTPUT
pinMode(i, OUTPUT);
}
pinMode(red, OUTPUT); // Setting pin A0 (red LED) as OUTPUT
pinMode(yellow, OUTPUT); // Setting pin A1 (yellow LED) as OUTPUT
pinMode(green, OUTPUT); // Setting pin A2 (green LED) as OUTPUT
digitalWrite(red, LOW); // Turning off the red LED initially
digitalWrite(yellow, LOW); // Turning off the yellow LED initially
digitalWrite(green, LOW); // Turning off the green LED initially
}
void loop() {
Red(); // Calling function to handle red light
Green(); // Calling function to handle green light
}
void Red() {
digitalWrite(red, HIGH); // Turning on the red LED
digitalWrite(yellow, LOW); // Turning off the yellow LED
digitalWrite(green, LOW); // Turning off the green LED
for (int seconds = redTime; seconds >= 0; seconds--) { // Countdown loop for red light
if (seconds < 6) {
blinkYellowWithDisplay(seconds); // Blink yellow with display sync when less than 6 seconds
break; // Exit after blinking phase to avoid double handling
} else {
display(seconds); // Normal countdown display
}
}
digitalWrite(red, LOW); // Turning off the red LED after countdown
}
void Green() {
digitalWrite(red, LOW); // Turning off the red LED
digitalWrite(yellow, LOW); // Turning off the yellow LED
digitalWrite(green, HIGH); // Turning on the green LED
for (int seconds = greenTime; seconds >= 0; seconds--) { // Countdown loop for green light
if (seconds < 6) {
blinkYellowWithDisplay(seconds); // Call modified blinking with display function
break; // Exit after the blinking phase to avoid redundant handling
} else {
display(seconds); // Normal countdown display
}
}
digitalWrite(green, LOW); // Turning off the green LED after countdown
}
void blinkYellowWithDisplay(int duration) {
for (int i = duration; i >= 0; i--) { // Loop for blinking yellow with display
digitalWrite(yellow, HIGH); // Turning on the yellow LED
display_number(i, false); // Show number
delay(500); // On for half second
digitalWrite(yellow, LOW); // Turning off the yellow LED
clearDisplay(); // Clear display
delay(500); // Off for half second
}
}
void display(int duration) {
if (duration >= 6) { // Only display if duration is 6 or more to avoid conflict with blinking logic
int tens = duration / 10; // Calculate tens digit of the countdown
int ones = duration % 10; // Calculate ones digit of the countdown
display_number(tens, true); // Display tens digit
display_number(ones, false); // Display ones digit
delay(1000); // Wait for one second
}
}
void display_number(int num, bool isTens) {
int startPin = isTens ? 0 : 7; // Determine starting pin for displaying numbers
for (int pin = startPin, segment = 0; pin < startPin + 7 && segment < 7; pin++, segment++) {
digitalWrite(pin, num_array[num][segment]); // Display number on 7-segment display
}
}
void clearDisplay() {
for (int pin = 0; pin <= 13; pin++) { // Clear the 7-segment display
digitalWrite(pin, LOW);
}
}