// Your traffic light should have 3 LEDS: A red, yellow and green LED
// The red LED should last 10 seconds, then switch to green. Green should
// last 15 seconds, then switch to yellow. Yellow should last for 2
// second, then switch to red. This should repeat forever.
// You should also have a button added to the arduino. If the button is
// pressed while the light is red, it will reduce the time to 5 seconds
// until the light turns green. If the button is pressed with less than
// 5 seconds left on the red light, it will not change the remaining
// time until it turns green. The button will also make the next green
// light last 5 seconds longer if it is pressed while the light is red.
int valuered = 10000;
int valuey = 2000;
int valueg = 15000;
int timer;
int previoustime = 0;
int counter = 0;
int up = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, INPUT);
}
void loop() {
timer = millis() - previoustime;
counter = digitalRead(11);
if (counter == HIGH){
if (timer < 5000) {
valuered = 5000;
valueg = 20000;
}
else if (timer < 10000) {
valueg = 20000;
}
}
if (up == 0){
if (timer < valuered){
digitalWrite(9, HIGH);
}
else if (timer > valuered) {
digitalWrite(9, LOW);
up++;
}
}
else if (up == 1){
if (timer < (valueg + valuered)){
digitalWrite(3, HIGH);
}
else if (timer > (valueg + valuered)){
digitalWrite(3, LOW);
up++;
}
}
else if (up == 2){
if (timer < (valueg + valuered + valuey)){
digitalWrite(7, HIGH);
}
else if (timer > (valueg + valuered + valuey)){
digitalWrite(7, LOW);
up = 0;
valuered = 10000;
valueg = 15000;
previoustime = millis();
}
}
}