Created
December 14, 2012 01:56
-
-
Save jonathanhculver/4281883 to your computer and use it in GitHub Desktop.
Arduino code to display a different color LED based on what number it's given.
This file contains hidden or 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
| int incoming = 0; | |
| int REDPin = 4; // RED pin | |
| int GREENPin = 5; // GREEN pin | |
| int BLUEPin = 6; // BLUE pin | |
| void setup() { | |
| // setup serial and led pins | |
| Serial.begin(9600); | |
| pinMode(REDPin, OUTPUT); | |
| pinMode(GREENPin, OUTPUT); | |
| pinMode(BLUEPin, OUTPUT); | |
| } | |
| void loop() { | |
| /* if data */ | |
| if(Serial.available() > 0) { | |
| /* get number from python*/ | |
| incoming = Serial.read(); | |
| Serial.print(incoming, DEC); | |
| Serial.print('\n'); | |
| /* light based on number */ | |
| if(incoming > 10) { | |
| lightRed(); | |
| } else if(incoming > 5){ | |
| lightYellow(); | |
| } else if(incoming > 0){ | |
| lightGreen(); | |
| } else { | |
| lightsOff(); | |
| } | |
| } | |
| delay(2000); | |
| } | |
| void lightRed() { | |
| analogWrite(REDPin, 255); | |
| analogWrite(GREENPin, 0); | |
| analogWrite(BLUEPin, 0); | |
| } | |
| void lightGreen() { | |
| analogWrite(REDPin, 0); | |
| analogWrite(GREENPin, 255); | |
| analogWrite(BLUEPin, 0); | |
| } | |
| void lightBlue() { | |
| analogWrite(REDPin, 0); | |
| analogWrite(GREENPin, 130); | |
| analogWrite(BLUEPin, 255); | |
| } | |
| void lightYellow() { | |
| analogWrite(REDPin, 255); | |
| analogWrite(GREENPin, 230); | |
| analogWrite(BLUEPin, 0); | |
| } | |
| void lightsOff() { | |
| analogWrite(REDPin, 0); | |
| analogWrite(GREENPin, 0); | |
| analogWrite(BLUEPin, 0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment