// This code implements traffic lights behaviour on the crossing of two roads.
// Shift registers are used to save some digital pins on Arduino.
// Slightly strangely but mainly due to gradual development of the system
// each shift register has two car traffic lights, one for each road
// and one pedestrian traffic light, for each road connected.
// Define the pins for the 74HC595 shift register 1
const int dataPin1 = 2; // SER
const int clockPin1 = 3; // SRCLK
const int latchPin1 = 4; // RCLK
// Define the pins for the 74HC595 shift register 2
const int dataPin2 = 5; // SER
const int clockPin2 = 6; // SRCLK
const int latchPin2 = 7; // RCLK
// Traffic light 1 states
const byte RED1 = 0b00000001;
const byte YELLOW1 = 0b00000010;
const byte GREEN1 = 0b00000100;
// Traffic light 2 states
const byte RED2 = 0b00001000;
const byte YELLOW2 = 0b00010000;
const byte GREEN2 = 0b00100000;
// Pedestrian trafic light 1 states
const byte REDP1 = 0b01000000;
const byte GREENP1 = 0b10000000;
// Pedestrian trafic light 2 states
const byte REDP2 = 0b01000000;
const byte GREENP2 = 0b10000000;
void setup() {
// Initialize the shift register control pins as outputs
pinMode(dataPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(latchPin1, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(latchPin2, OUTPUT);
// Initialize the traffic lights
updateShiftRegister1(RED1 | GREEN2 | GREENP1);
updateShiftRegister2(RED1 | GREEN2 | REDP2);
}
void loop() {
// First traffic light: Green, Second traffic light: Red
// First pedestrian traffic light: Red
// Second pedestrian traffic light: Green
updateShiftRegister2(GREEN1 | RED2 | REDP1);
updateShiftRegister1(GREEN1 | RED2 | GREENP2);
delay(120000); // 2 minutes = 120000 ms
// First traffic light: Yellow, Second traffic light: Red
// First pedestrian traffic light: Red
// Second pedestrian traffic light: Red
updateShiftRegister1(YELLOW1 | RED2 | REDP1);
updateShiftRegister2(YELLOW1 | RED2 | REDP2);
delay(5000); // 5 seconds = 5000 ms
// First traffic light: Red, Second traffic light: Green
// First pedestrian traffic light: Green
// Second pedestrian traffic light: Red
updateShiftRegister1(RED1 | GREEN2 | GREENP1);
updateShiftRegister2(RED1 | GREEN2 | REDP2);
delay(120000); // 2 minutes = 120000 ms
// First traffic light: Red, Second traffic light: Yellow
// First pedestrian traffic light: Red
// Second pedestrian traffic light: Red
updateShiftRegister1(RED1 | YELLOW2 | REDP1);
updateShiftRegister2(RED1 | YELLOW2 | REDP2);
delay(5000); // 5 seconds = 5000 ms
}
// Functions to update the shift registers
void updateShiftRegister1(byte data) {
// Set latchPin low to prepare for data loading
digitalWrite(latchPin1, LOW);
// Shift out the data
shiftOut(dataPin1, clockPin1, MSBFIRST, data);
// Set latchPin high to store the data in the register
digitalWrite(latchPin1, HIGH);
}
void updateShiftRegister2(byte data) {
// Set latchPin low to prepare for data loading
digitalWrite(latchPin2, LOW);
// Shift out the data
shiftOut(dataPin2, clockPin2, MSBFIRST, data);
// Set latchPin high to store the data in the register
digitalWrite(latchPin2, HIGH);
}