Skip to content

Instantly share code, notes, and snippets.

@davidjmemmett
Created September 20, 2010 18:43
Show Gist options
  • Save davidjmemmett/588408 to your computer and use it in GitHub Desktop.
Save davidjmemmett/588408 to your computer and use it in GitHub Desktop.
/* Richard's Traffic Light Program */
int pinRedLed = 2;
int pinYelLed = 3;
int pinGrnLed = 4;
int lightState = 0;
void setup() {
pinMode(pinRedLed, OUTPUT); /* Set the LED pins to output */
pinMode(pinYelLed, OUTPUT);
pinMode(pinGrnLed, OUTPUT);
digitalWrite(pinRedLed, HIGH); /* Turn the RED light on, so we don't cause any traffic accidents while we initialise <img src="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley"> */
}
void loop() {
lightState++;
if (lightState > 3) { // If the light state > 3, reset it to 0 (red).
lightState = 0;
}
delay(2500);
/* Display correct light sequence */
if (lightState == 0) { // Red
digitalWrite(pinRedLed, HIGH);
lightOff(pinYelLed);
lightOff(pinGrnLed);
}
if (lightState == 1) { // Red and Amber
digitalWrite(pinRedLed, HIGH);
digitalWrite(pinYelLed, HIGH);
lightOff(pinGrnLed);
}
if (lightState == 2) { // Green
digitalWrite(pinGrnLed, HIGH);
lightOff(pinYelLed);
lightOff(pinRedLed);
}
if (lightState == 3) { // Amber-only
digitalWrite(pinYelLed, HIGH);
lightOff(pinRedLed);
lightOff(pinGrnLed);
}
}
void lightOff(int pin) {
digitalWrite(pin, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment