/* Semáforo de coches se encuentra continuamente en verde,
cada vez que se actua un pulsador se realiza la secuencia
normal del semáforo una sola vez. Hay un tiempo de espera
entre cada actuación del pulsador
*/
int carRed = 13; // assign the car lights
int carYellow = 12;
int carGreen = 11;
int button = 7; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
Serial.begin(9600);
}
void loop() {
Serial.println(millis()-changeTime);
int state = digitalRead(button); // check if button is pressed and it is over 5 seconds since last button press
if (state == HIGH && (millis() - changeTime) > crossTime) {
changeLights(); // Call the function to change the lights
}
}
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(1000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(2000); // wait 1 second till its safe
digitalWrite(carRed, LOW); // red off
digitalWrite(carGreen, HIGH);
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
}