Skip to content

Instantly share code, notes, and snippets.

@dorkrawk
Created April 10, 2013 05:03
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 dorkrawk/5351940 to your computer and use it in GitHub Desktop.
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!
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