Skip to content

Instantly share code, notes, and snippets.

@hdurdle
Last active June 12, 2016 03:49
Show Gist options
  • Save hdurdle/f5f93156b06c1d98b514 to your computer and use it in GitHub Desktop.
Save hdurdle/f5f93156b06c1d98b514 to your computer and use it in GitHub Desktop.
Arduino RGB Serial Control
#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);
}
@VeggieVampire
Copy link

So how does the code work? Do I send 255 255 255 from the Serial at 57600 baud?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment