Created
February 17, 2015 19:43
-
-
Save emilyhorsman/2b876c88039ed21a0bb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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