Last active
May 20, 2018 05:31
-
-
Save justindra/eeffe3d98e77de59660520ff9cf50384 to your computer and use it in GitHub Desktop.
Arduino Serial LED Fader
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
/* | |
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