// Pins for 74HC595
const int latchPin = 10; // Latch pin
const int clockPin = 9; // Clock pin
const int dataPin = 8; // Data pin
// Digit selection pins (common cathode)
const int digitPins[] = {11, 12, 13, 14}; // Control pins for 4 digits
// Traffic light pins (grouped by direction)
const int redNorth = 22, yellowNorth = 23, greenNorth = 24;
const int redSouth = 22, yellowSouth = 23, greenSouth = 24; // Synced with North
const int redEast = 25, yellowEast = 26, greenEast = 27;
const int redWest = 25, yellowWest = 26, greenWest = 27; // Synced with East
// Digit patterns for 7-segment display (common cathode)
const byte digits[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
// Setup pins for 74HC595
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Setup pins for digit selection
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], LOW); // Ensure all digits are off initially
}
// Setup traffic light pins
pinMode(greenNorth, OUTPUT);
pinMode(yellowNorth, OUTPUT);
pinMode(redNorth, OUTPUT);
pinMode(greenEast, OUTPUT);
pinMode(yellowEast, OUTPUT);
pinMode(redEast, OUTPUT);
// Start with all lights off
turnOffAllLights();
}
void loop() {
// Execute traffic light cycle with countdown
fullCycle(
greenNorth, yellowNorth, redNorth,
greenEast, yellowEast, redEast,
digitPins[0], digitPins[1], digitPins[2], digitPins[3]
);
}
// Turn off all traffic lights
void turnOffAllLights() {
digitalWrite(greenNorth, LOW);
digitalWrite(yellowNorth, LOW);
digitalWrite(redNorth, LOW);
digitalWrite(greenEast, LOW);
digitalWrite(yellowEast, LOW);
digitalWrite(redEast, LOW);
}
void fullCycle(
int greenPrimary, int yellowPrimary, int redPrimary,
int greenOpposite, int yellowOpposite, int redOpposite,
int displayPrimary1, int displayPrimary2, int displayOpposite1, int displayOpposite2) {
// Phase 1: N-S Green, E-W Red
digitalWrite(greenPrimary, HIGH); // N-S Green ON
digitalWrite(redPrimary, LOW); // N-S Red OFF
digitalWrite(redOpposite, HIGH); // E-W Red ON
digitalWrite(yellowPrimary, LOW); // N-S Yellow OFF
countdown(9, displayPrimary1, displayPrimary2); // 10 seconds for N-S Green
digitalWrite(greenPrimary, LOW); // Turn OFF N-S Green
// Phase 2: N-S Yellow, E-W Red
digitalWrite(yellowPrimary, HIGH); // N-S Yellow ON
countdown(2, displayPrimary1, displayPrimary2); // 3 seconds for N-S Yellow
digitalWrite(yellowPrimary, LOW); // Turn OFF N-S Yellow
digitalWrite(redPrimary, HIGH); // Turn ON N-S Red
// Phase 3: E-W Yellow, N-S Red
digitalWrite(yellowOpposite, HIGH); // E-W Yellow ON
digitalWrite(redOpposite, LOW); // Turn OFF E-W Red
countdown(2, displayOpposite1, displayOpposite2); // 3 seconds for E-W Yellow
digitalWrite(yellowOpposite, LOW); // Turn OFF E-W Yellow
// Phase 4: E-W Green, N-S Red
digitalWrite(greenOpposite, HIGH); // E-W Green ON
countdown(9, displayOpposite1, displayOpposite2); // 10 seconds for E-W Green
digitalWrite(greenOpposite, LOW); // Turn OFF E-W Green
// Phase 5: E-W Yellow, N-S Red
digitalWrite(yellowOpposite, HIGH); // E-W Yellow ON
countdown(2, displayOpposite1, displayOpposite2); // 3 seconds for E-W Yellow
digitalWrite(yellowOpposite, LOW); // Turn OFF E-W Yellow
digitalWrite(redOpposite, HIGH); // Turn ON E-W Red
// Phase 6: N-S Yellow, E-W Red
digitalWrite(yellowPrimary, HIGH); // N-S Yellow ON
digitalWrite(redPrimary, LOW);
countdown(2, displayPrimary1, displayPrimary2); // 3 seconds for N-S Yellow
digitalWrite(yellowPrimary, LOW); // Turn OFF N-S Yellow
digitalWrite(redPrimary, LOW); // Turn OFF N-S Red
// Back to Phase 1: N-S Green
}
// Function to display countdown on 7-segment displays
void countdown(int seconds, int digit1, int digit2) {
for (int i = seconds; i >= 0; i--) {
int digitValues[] = {i / 10, i % 10}; // Extract tens and ones digits
// Multiplex the 7-segment display for both digits
for (int j = 0; j < 2; j++) {
activateDigit(j == 0 ? digit1 : digit2);
sendToShiftRegister(digits[digitValues[j]]);
delay(5); // Short delay for persistence of vision
deactivateAllDigits();
}
delay(995); // 1-second delay minus multiplexing time
}
}
// Function to send data to the 74HC595 shift register
void sendToShiftRegister(byte data) {
digitalWrite(latchPin, LOW); // Prepare to send data
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH); // Latch the data to output pins
}
// Function to activate a specific digit
void activateDigit(int digitPin) {
digitalWrite(digitPin, HIGH); // Turn on the specific digit
}
// Function to deactivate all digits
void deactivateAllDigits() {
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], LOW); // Turn off all digits
}
}