Skip to content

Instantly share code, notes, and snippets.

@evilmachina
Last active December 26, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilmachina/7073753 to your computer and use it in GitHub Desktop.
Save evilmachina/7073753 to your computer and use it in GitHub Desktop.
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
char payload[5] = "100";
unsigned long timeLastCommand;
void setup ()
{
Serial.begin(57600);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}
byte inByte = 0;
char code[4];
int bytesread = 0;
void loop ()
{
handleIncomingData();
if( (millis() - timeLastCommand) > 60000){
setColor(0,0,0);
}
}
void handleIncomingData(){
if (Serial.available() > 0) {
inByte = Serial.read();
//Serial.println(inByte);
if(inByte == 4){
bytesread = 0;
while(bytesread<4) {
if( Serial.available() > 0) {
inByte = Serial.read();
if((inByte == 10)||(inByte == 13)) {
break;
}
code[bytesread] = inByte;
//Serial.print('r');
//Serial.println(bytesread);
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