Created
January 24, 2016 12:55
-
-
Save dicamarques14/631f7c0e0ff6dbec17fe to your computer and use it in GitHub Desktop.
Control RGB LED with arduino over serial
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
// Output | |
int redPin = 9; // Red LED, connected to digital pin 9 | |
int greenPin = 10; // Green LED, connected to digital pin 10 | |
int bluePin = 11; // Blue LED, connected to digital pin 11 | |
long int inByte; | |
int wait = 10; //10ms | |
void setup() | |
{ | |
pinMode(redPin, OUTPUT); // sets the pins as output | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void outputColour(int red, int green, int blue) { | |
analogWrite(redPin, red); | |
analogWrite(bluePin, blue); | |
analogWrite(greenPin, green); | |
} | |
int* getColour() { | |
int* colour; | |
int i; | |
i = 0; | |
//for some reason it only works if we put a dud value between the C and | |
// the R value | |
while (i < 4) | |
{ | |
if (Serial.available() > 0) { | |
colour[i] = Serial.read(); | |
i++; | |
} | |
} | |
return colour; | |
} | |
// Main program | |
void loop() | |
{ | |
if (Serial.available() > 0) { | |
// get incoming byte: | |
inByte = Serial.read(); | |
if (inByte == 'C') { | |
int* one; | |
one = getColour(); | |
//1 2 3 not 0 1 2 due to the dud value | |
outputColour(one[1],one[2],one[3]); | |
} | |
} | |
delay(wait); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment