Last active
June 12, 2016 03:49
-
-
Save hdurdle/f5f93156b06c1d98b514 to your computer and use it in GitHub Desktop.
Arduino RGB Serial Control
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
#define REDPIN 3 | |
#define GREENPIN 5 | |
#define BLUEPIN 6 | |
unsigned long lastCommandDelay = 30000; | |
unsigned long timeLastCommand; | |
byte inByte = 0; | |
char code[4]; | |
int bytesread = 0; | |
void setup () | |
{ | |
Serial.begin(57600); | |
pinMode(REDPIN, OUTPUT); | |
pinMode(GREENPIN, OUTPUT); | |
pinMode(BLUEPIN, OUTPUT); | |
} | |
void loop () | |
{ | |
handleIncomingData(); | |
if( (millis() - timeLastCommand) > lastCommandDelay) | |
{ | |
setColor(0,0,0); | |
} | |
} | |
void handleIncomingData() | |
{ | |
if (Serial.available() > 0) | |
{ | |
inByte = Serial.read(); | |
if(inByte == 4) | |
{ | |
bytesread = 0; | |
while(bytesread<4) | |
{ | |
if( Serial.available() > 0) | |
{ | |
inByte = Serial.read(); | |
if((inByte == 10)||(inByte == 13)) | |
{ | |
break; | |
} | |
code[bytesread] = inByte; | |
bytesread++; | |
} | |
} | |
if(bytesread == 3) | |
{ | |
setColor(code[0],code[1],code[2]); | |
Serial.flush(); | |
} | |
bytesread = 0; | |
timeLastCommand = millis(); | |
} | |
} | |
} | |
void setColor(int r, int g, int b) | |
{ | |
analogWrite(REDPIN, r); | |
analogWrite(GREENPIN, g); | |
analogWrite(BLUEPIN, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So how does the code work? Do I send 255 255 255 from the Serial at 57600 baud?