Skip to content

Instantly share code, notes, and snippets.

@bemurphy bemurphy/rgb.c
Created Dec 26, 2011

Embed
What would you like to do?
red green blue for tri color led and tdd
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
String inputString = "";
boolean stringComplete = false;
void setup() {
Serial.begin(9600);
inputString.reserve(100);
}
void loop(){
if (stringComplete) {
Serial.println(inputString);
if (inputString == "failed") {
setColor(RED_LED_PIN);
} else if (inputString == "pending") {
setColor(BLUE_LED_PIN);
} else {
setColor(GREEN_LED_PIN);
}
inputString = "";
stringComplete = false;
}
}
void resetLED() {
analogWrite(RED_LED_PIN, 0);
analogWrite(GREEN_LED_PIN, 0);
analogWrite(BLUE_LED_PIN, 0);
}
void setColor(int pin) {
resetLED();
analogWrite(pin, 128);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') {
stringComplete = true;
} else {
inputString += inChar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.