Skip to content

Instantly share code, notes, and snippets.

@jonathanprozzi
Created July 18, 2016 16:31
Show Gist options
  • Save jonathanprozzi/94b9e7552532163f41951a8fff071da7 to your computer and use it in GitHub Desktop.
Save jonathanprozzi/94b9e7552532163f41951a8fff071da7 to your computer and use it in GitHub Desktop.
Traffic Light Controller - Experimenting with Arduino
/* Arduino Experimenter Challenge: Broken Traffic Light
* This traffic light is -sooo close- to being correct, but something seems
* off with the logic...
*
* Can you fix it?
*
* Hint: Think about how a functioning traffic light works and build out the logic-
* what steps should the lights go in?
* should two colors ever be on at the same time?
*
* Going Further:
* after you get the light working correctly, invent your own traffic light system!
*
*
*/
// global variables go here. set and change the led pin variables to match what you need
int red = 13;
int yellow = 12;
int green = 8;
void setup() {
// make sure to set the pinMode to output for EACH led variable
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
changeLights();
delay(10000); //process starts over every 10 seconds
}
void changeLights() {
//first, green is on and the other two are off:
digitalWrite(red,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(green,LOW);
delay(5000); //red light for 5 seconds!
//now, yellow light:
digitalWrite(yellow,HIGH);
digitalWrite(green,LOW);
digitalWrite(red,LOW);
delay(2000); //yellow light for 2 seconds!
//now, green light:
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
digitalWrite(yellow,LOW);
//green light!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment