Skip to content

Instantly share code, notes, and snippets.

@justindra
Last active May 20, 2018 05:31
Show Gist options
  • Save justindra/eeffe3d98e77de59660520ff9cf50384 to your computer and use it in GitHub Desktop.
Save justindra/eeffe3d98e77de59660520ff9cf50384 to your computer and use it in GitHub Desktop.
Arduino Serial LED Fader
/*
Turns on the led using serial
*/
// Initialize the LEDs
int blue = 9;
int green = 10;
int red = 11;
// Initialize the integers to keep the values of the leds
int blue_value;
int red_value;
int green_value;
// the setup routine runs once when you press reset:
void setup() {
// initialize the pin as an output.
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
// Start serial
Serial.begin(9600);
}
void loop() {
// Wait until we receive the 0xFF Character
if (Serial.read() == 0xFF) {
// Wait until we have 3 bytes available
while (!(Serial.available() > 3)) Serial.println("wait");
blue_value = Serial.read();
red_value = Serial.read();
green_value = Serial.read();
// Echo back what we just received for debugging
char sendBuffer[3];
sprintf(sendBuffer, "%d%d%d", blue_value, red_value, green_value);
Serial.println(sendBuffer);
// Write the LED Values to show the colours
analogWrite(blue, blue_value);
analogWrite(green, green_value);
analogWrite(red, red_value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment