/*
Traffic light Sequencer
Slightly More advanced with a Turn light
Author Gte718p
*/
//Global variables that will be used in the program
//Give a name to the pin that the relays are connected to
#define RedLight 0
#define YellowLight 1
#define GreenLight 2
#define TurnLight 3
#define ModeButton 4
//sets how long the lamp will be on or off
int DelayTime=1000;
//selects the mode we will be in
int Mode=1;
//How many times the turn light should flash
int TurnFlash=5;
// the setup function runs once when you press reset or power the board
void setup() {
//sets up the pins to be output.
pinMode(RedLight, OUTPUT);
pinMode(YellowLight, OUTPUT);
pinMode(GreenLight, OUTPUT);
pinMode(TurnLight, OUTPUT);
pinMode(ModeButton, INPUT); //creates an input for our button
pinMode(ModeButton,LOW); //activates the pull down resister
digitalWrite(RedLight, HIGH);
digitalWrite(YellowLight, HIGH);
digitalWrite(GreenLight, HIGH);
digitalWrite(TurnLight, HIGH);
attachInterrupt(digitalPinToInterrupt(ModeButton), ModeChange, HIGH);
}
void ModeChange(){
Mode++; //adds one to the mode
if (Mode > 2){ //if the mode gets larger then the number of available mode start at the begging
Mode=1;
}
}
// the loop function runs over and over again forever
void loop() {
if (Mode == 1) //Normal top to bottom
{
digitalWrite(RedLight, LOW); // turn the Relay on
delay(DelayTime); // wait for a second
digitalWrite(RedLight, HIGH); // turn the relay off
delay(DelayTime); // wait for a second
digitalWrite(YellowLight, LOW); // turn the Relay on
delay(DelayTime); // wait for a second
digitalWrite(YellowLight, HIGH); // turn the relay off
delay(DelayTime); // wait for a second
digitalWrite(GreenLight, LOW); // turn the relay on
delay(DelayTime); // wait for a second
digitalWrite(GreenLight, HIGH); // turn the relay off
delay(DelayTime); // wait for a second
//Loop for flashing walk light
for(int i=0; i++; i < TurnFlash){ //starts a counter and loop to flash
digitalWrite(TurnLight, LOW); //Turns relay on
delay(DelayTime); //wait
digitalWrite(TurnLight, HIGH); //Turns relay off
delay(DelayTime); //wait
}
}
if (Mode == 2) //flash all
{
digitalWrite(RedLight, HIGH);
digitalWrite(YellowLight, HIGH);
digitalWrite(GreenLight, HIGH);
digitalWrite(TurnLight, HIGH);
delay(DelayTime);
digitalWrite(RedLight, LOW);
digitalWrite(YellowLight, LOW);
digitalWrite(GreenLight, LOW);
digitalWrite(TurnLight, LOW);
delay(DelayTime);
}
}