Skip to content

Instantly share code, notes, and snippets.

@emilyhorsman
Created February 17, 2015 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilyhorsman/2b876c88039ed21a0bb7 to your computer and use it in GitHub Desktop.
Save emilyhorsman/2b876c88039ed21a0bb7 to your computer and use it in GitHub Desktop.
/*
* Traffic signal RGB LED from Adafruit Arduino lesson suggestion
*/
int redLEDPin = 11;
int greenLEDPin = 10;
int blueLEDPin = 9;
int buttonPin = 7;
boolean lastButtonState;
// Technically this "yellow" is orange which shows up nicer on the LED.
// { off red yellow green }
byte redLEDStates[4] = { 0x00, 0xFF, 0xFF, 0x00 };
byte greenLEDStates[4] = { 0x00, 0x00, 0x66, 0xFF };
byte blueLEDStates[4] = { 0x00, 0x00, 0x00, 0x00 };
int currentState = 0;
void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
lastButtonState = digitalRead(buttonPin);
}
void loop() {
boolean newButtonState = digitalRead(buttonPin);
if (newButtonState == HIGH && lastButtonState == LOW) {
currentState++;
if (currentState > 3) currentState = 0;
updateTrafficLEDs();
}
lastButtonState = newButtonState;
delay(10); // debounce
}
void updateTrafficLEDs() {
analogWrite(redLEDPin, redLEDStates[currentState]);
analogWrite(greenLEDPin, greenLEDStates[currentState]);
analogWrite(blueLEDPin, blueLEDStates[currentState]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment