/* NOTE: DEMO ONLY!!
Do not attempt to power this many LEDS from the board
*/
int latchPin = 8;//Pin connected to ST_CP of each shift register
int clockPin = 12; //Pin connected to SH_CP of each shift register
int dataPin = 11; //Pin connected to DS of first shift register
// Connect Q7S of the first shift register to DS of the next register(s)
int pirPin = 7;
int flagTripped = 0;
int pir = 0;
// Some timer stuff for the sensor simulation
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;
int interval = 5000; // Set the number dwell time before changing
/*
Shift registrs are arranged so one controls E/W the other controls N/S
The order in which you shift the bytes into the register dictates which direction gets the sequence
B00100100 (36) = E/W Green if used as first byte, N/S Green if second byte
B10010000 (144)= N/S Red (first), E/W red (second)
B01001000 (72)= E/W Orange (first), N/S Orange (second)
WR,WO,WG,ER,EO,EG,0,0
NR,NO,NG,SR,SO,SG,0,0
*/
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(pirPin, INPUT);
Serial.begin(9600);
changeLights(36,144); // Set E/W green, N/S Red
Serial.println("East/West on green");
}
void loop(){
pir = digitalRead(pirPin);
// If motion detected,set the change sequence in action
if(pir == 1 && flagTripped == 0){
flagTripped = 1;
Serial.println("East/West ready to stop");
changeLights(72,144); // Change E/W to orange, keep N/S Red
delay(interval/2);
Serial.println("All stop");
changeLights(144,144); // Both sets red
delay(interval/2);
Serial.println("North/South on green");
changeLights(144,36); // Change E/W to red, N/S green
startMillis = currentMillis = millis(); // Start the sensor timer
}
// Simulate the sensor reset timeout - Not needed in real life due to onboard timer
while(currentMillis - startMillis <= period){
if(digitalRead(pirPin)==1){
//Reset timer if pir retriggered
startMillis=currentMillis = millis();
}
currentMillis=millis();
}
pir = 0;
// If sensor no longer detecting, and the N/S traffic is on green
// Start the change sequence to get E/W back on green
if(pir==0 && flagTripped == 1){
flagTripped = 0;
Serial.println("North/South ready to stop");
changeLights(144,72); // Change N/S to orange, keep E/W Red
delay(interval/2);
Serial.println("All stop");
changeLights(144,144); // Both sets red
delay(interval/2);
Serial.println("East/West on green");
changeLights(36,144); // Change E/W to green, N/S Red
}
}
void changeLights(byte b1, byte b2){
digitalWrite(latchPin, LOW);
// West/East register
shiftOut(dataPin, clockPin, LSBFIRST, b1);
// Nth/Sth register
shiftOut(dataPin, clockPin, LSBFIRST, b2);
digitalWrite(latchPin, HIGH);
}