title | slug | date_published | date_updated |
---|---|---|---|
Traffic light |
traffic-light |
2009-09-15 06:20:51 UTC |
2014-03-29 23:33:15 UTC |
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](http://vimeo.com/6582107) from [Zeven Rodriguez](http://vimeo.com/user2302394) on [Vimeo](http://vimeo.com).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);
}
}