Hi there,
I am currently working on a project seeking to recreate a 4-way traffic intersection with pedestrian crossings. So far I have the lights set up to change following the UK light pattern without using delays. I am designing this with the assumption that cars will only move straight across the intersection. Pedestrians will have a button to either cross vertically or horizontally on each corner (a total of 8 pushbuttons) and only one crossing direction can be clear at a time. I'm unsure how to write a statement to allow the buttons to be used. Any help or nod in the right direction would be greatly appreciated!
Thanks!
int horizontalCrossing = 7;
int verticalCrossing = 8;
//vertical
int redv= 1;
int yellowv= 2;
int greenv= 3;
// horizontal
int redh= 4;
int yellowh= 5;
int greenh= 6;
enum phases {
red, redYellow, green, yellow, red2, redYellow2, green2, yellow2
};
phases phase, nextPhase;
unsigned long lightsTimer = 0;
void setup() {
pinMode(horizontalCrossing, INPUT);
pinMode(verticalCrossing, INPUT);
pinMode(redv,OUTPUT);
pinMode(yellowv,OUTPUT);
pinMode(greenv,OUTPUT);
pinMode(redh,OUTPUT);
pinMode(yellowh,OUTPUT);
pinMode(greenh,OUTPUT);
phase = red;
}
void loop() {
phaseChange();
}
void phaseChange() {
if(millis() >= lightsTimer) {
phase = nextPhase;
switch(phase) {
case red:
digitalWrite(redv, HIGH);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, LOW);
nextPhase = redYellow;
lightsTimer = millis() + 7000UL;
break;
case redYellow:
digitalWrite(redv, HIGH);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, HIGH);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, LOW);
nextPhase = green;
lightsTimer = millis() + 1000UL;
break;
case green:
digitalWrite(redv, LOW);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, HIGH);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, LOW);
nextPhase = yellow;
lightsTimer = millis() + 5000UL;
break;
case yellow:
digitalWrite(redv, LOW);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, HIGH);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, LOW);
nextPhase = red2;
lightsTimer = millis() + 1000UL;
break;
case red2:
digitalWrite(redv, HIGH);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, LOW);
nextPhase = redYellow2;
lightsTimer = millis() + 7000UL;
break;
case redYellow2:
digitalWrite(redv, HIGH);
digitalWrite(redh, HIGH);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, HIGH);
digitalWrite(greenh, LOW);
nextPhase = green2;
lightsTimer = millis() + 1000UL;
break;
case green2:
digitalWrite(redv, HIGH);
digitalWrite(redh, LOW);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, LOW);
digitalWrite(greenh, HIGH);
nextPhase = yellow2;
lightsTimer = millis() + 5000UL;
break;
case yellow2:
digitalWrite(redv, HIGH);
digitalWrite(redh, LOW);
digitalWrite(yellowv, LOW);
digitalWrite(greenv, LOW);
digitalWrite(yellowh, HIGH);
digitalWrite(greenh, LOW);
nextPhase = red;
lightsTimer = millis() + 1000UL;
break;
default:
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
break;
}
}
}