Skip to content

Instantly share code, notes, and snippets.

@alexbonhomme
Last active December 26, 2015 22:09
Show Gist options
  • Save alexbonhomme/7220926 to your computer and use it in GitHub Desktop.
Save alexbonhomme/7220926 to your computer and use it in GitHub Desktop.
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int red = 0;
int green = 0;
int blue = 0;
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
if (Serial.available() > 1) {
switch (Serial.read()) {
case 'r':
if (Serial.read() == '#') {
Serial.print(red);
Serial.print(" ");
Serial.print(green);
Serial.print(" ");
Serial.println(blue);
}
break;
case 'w':
while (Serial.available() > 0) {
int newRed = Serial.parseInt();
int newGreen = Serial.parseInt();
int newBlue = Serial.parseInt();
if (Serial.read() == '#') {
red = constrain(newRed, 0, 255);
green = constrain(newGreen, 0, 255);
blue = constrain(newBlue, 0, 255);
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
Serial.println("OK");
}
}
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment