Created
April 10, 2013 05:03
-
-
Save dorkrawk/5351940 to your computer and use it in GitHub Desktop.
This is the first little Arduino program I wrote (outside of basic tutorials). It turns on a blue, red, or green LED based on a key stroke. Neat!
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
char input = 0; | |
int blue = 9; | |
int red = 10; | |
int green = 11; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
delay(100); | |
pinMode(blue, OUTPUT); | |
pinMode(red, OUTPUT); | |
pinMode(green, OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if(Serial.available() > 0) { | |
input = Serial.read(); | |
handleInput(input); | |
} | |
} | |
void handleInput(char i) { | |
if(i == 'r') { | |
lightOn(red); | |
} else if (i == 'b') { | |
lightOn(blue); | |
} else if (i == 'g') { | |
lightOn(green); | |
} else if (i == 'o') { | |
allOn(); | |
} else if (i == 'x') { | |
allOff(); | |
} | |
} | |
void allOn() { | |
digitalWrite(blue, HIGH); | |
digitalWrite(red, HIGH); | |
digitalWrite(green, HIGH); | |
Serial.println("All On!"); | |
} | |
void allOff() { | |
digitalWrite(blue, LOW); | |
digitalWrite(red, LOW); | |
digitalWrite(green, LOW); | |
Serial.println("Lights Out!"); | |
} | |
void lightOn(int color) { | |
digitalWrite(color, HIGH); | |
Serial.print(color); | |
Serial.println(" on."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment