Code:
int button = 13;
int redLED = 12;
int yellowLED = 11;
int greenLED = 10;
int redPedLED = 9; //Ped = pedestrian
int greenPedLED = 8;
int buttonDelay = 1000; //Delay between pushing the button and light changing
int lightDelay = 800; //Delay between switches
int stopTime = 3000; //How long the stop light stays red
void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT);
for (int i = 12; i > 7; i--){
pinMode(i, OUTPUT); //setting up board ports 8-12
}
}
void loop() {
// put your main code here, to run repeatedly:
// digitalRead(button) == LOW is pushed somehow..
//Starting Position
digitalWrite(greenLED, HIGH);
digitalWrite(redPedLED, HIGH);
//When button is pushed
if (digitalRead(button)==LOW){
//Transition from green traffic light to green pedestrian light
delay(buttonDelay);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
delay(lightDelay);
digitalWrite(yellowLED, LOW);
digitalWrite(redPedLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(greenPedLED, HIGH);
delay(stopTime);
//Transition back to green traffic light and red pedestrian light
blinker(greenPedLED, 500, 3);
digitalWrite(greenPedLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(redPedLED, HIGH);
digitalWrite(greenLED, HIGH);
}
}
//Function for blinking since i didn't feel like writing it a bunch
//of times and now you can customize the blink i guess
void blinker(int led, int interval, int blinks) {
for (int i = 0; i < blinks; i++){
digitalWrite(led, LOW);
delay(interval);
digitalWrite(led, HIGH);
delay(interval);
}
}
Comments