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 | |
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