Semáforo de autos y peatonal con botón usando Arduino

Para empezar a probar circuitos básicos, decidí seguir algunos de los tutoriales, en particular me pareció interesante el de Semáforo de autos y peatonal interactivo que está en el libro Arduino Starter Kit Manual de Mike McRoberts , este es el diagrama:

Y este el circuito que armé basado en el diagrama, en la foto funcionando con una batería de 9v, teniendo cuidado que el positivo esté en el centro del conector a la placa.

Arduino con circuito de Semáforo
Arduino con circuito de Semáforo

Como el semáforo estaba programado para la lógica de Reino Unido, cambié un poco la lógica del sketch (programa) y los tiempos para Argentina:

// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 7000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is
over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
changeLights();
}
}
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(1500); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
digitalWrite(pedGreen, LOW); // ped red off
for (int x=0; x<10; x++) {
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(pedRed, LOW);
delay(500);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
}

El resultado final puede verse en el siguiente video para móviles

o embebido:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.