Traffic light
So I tried to code a traffic light. This is a rough sketch. The idea is to have a toy car roll over the contacts and it initiate the traffic signaling.
Traffic Light from Zeven Rodriguez on Vimeo.
This is the code I used
int pinRed = 3;
int pinYellow = 4;
int pinGreen = 5;
int switchButton = 2; // button
int switchIni = 0; // Initial state of switch
void setup(){
pinMode(pinRed, OUTPUT);
pinMode(pinYellow, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(switchButton, INPUT); //takes in switch signal
}
void loop(){
switchIni = digitalRead(switchButton);
if (switchIni == 1){
digitalWrite(pinRed, HIGH);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, LOW);
delay(1000);
digitalWrite(pinYellow, HIGH);
digitalWrite(pinRed, LOW);
digitalWrite(pinGreen, LOW);
delay(1000);
digitalWrite(pinGreen, HIGH);
digitalWrite(pinYellow, LOW);
digitalWrite(pinRed, LOW);
delay(5000);
}
else{
digitalWrite(pinRed, HIGH);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, LOW);
}
}