Skip to content

Instantly share code, notes, and snippets.

@davedarko
Created December 26, 2011 18:31
Show Gist options
  • Save davedarko/1521875 to your computer and use it in GitHub Desktop.
Save davedarko/1521875 to your computer and use it in GitHub Desktop.
RGB Arduino remote code
int red = 11, green=10, blue=9, white=6;
String tempString=String(100), readString = String(100);
void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(white, OUTPUT);
Serial.begin(9600);
}
void loop() {
tempString = readString;
if (Serial.available()) {
delay(10);
readString = "";
while (Serial.available() > 0) readString += (char) Serial.read();
}
if (readString!="" && readString!=tempString) progress(readString);
}
int pos(String s) {
int l = s.length();
for (int i=0; i<l; i++) {
char c = s.charAt(i);
if (c==':') return i;
}
return -1;
}
String substr(String s, int istart, int istop) {
String b = "";
int l = s.length();
for (int i=istart; i<istop; i++) {
char c = s.charAt(i);
b = b+c;
} return b;
}
void progress(String s) {
String color = substr(s, 0 , pos(s));
char buf[12]; // "-2147483648\0"
String cmd = substr(s, pos(s)+1 , s.length());
cmd.toCharArray(buf,12);
int value = atoi( buf );
if (color=="r") analogWrite(red, value);
if (color=="g") analogWrite(green, value);
if (color=="b") analogWrite(blue, value);
if (color=="w") analogWrite(white, value);
if (color=="fn" && cmd=="red") {
analogWrite(white, 0);
analogWrite(blue, 0);
analogWrite(green, 0);
analogWrite(red, 255);
delay(1000);
analogWrite(red, 0);
delay(1000);
}
if (color=="fn" && cmd=="strobe") {
analogWrite(red, 255);
analogWrite(blue, 255);
analogWrite(green, 255);
analogWrite(white, 255);
delay(100);
analogWrite(red, 0);
analogWrite(blue, 0);
analogWrite(green, 0);
analogWrite(white, 0);
delay(100);
}
Serial.println(color+":"+buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment