/* * Serial RGB LED * ------------------ * Serial commands control the brightness of R,G,B LEDs * * Command structure is "#RRGGBB" * * * Created 18 October 2006 * copyleft 2006 Tod E. Kurt interval) { // if the end has been met previousMillis = millis(); // zero the counter // Send the endbyte so the phone will fetch a page with the rgb code for the led Serial.print(endbyte, BYTE); } else { // if the count down is still running // we just keep watching the serial port int datalenght = readSerialString(); // if we get a # on the serial and server info (theurl) has not been sent // we need to tell the phone where to send the data if (serInStr[0] == (char) endbyte && serverinfo == false) { sendserverinfo(); // send the server info } else { // if we get anything other than the endbyte and server info has been sent it means the phone is // talking back with the rgb code from the web writetoLED(datalenght); } } } int readSerialString () { int i=0; int c = 0; // we wait on the serial port while it is available // and not too long while (Serial.available() && i < slen) { // delay a little so we give the buffer time to fill up delay(500); c = Serial.read(); // the phone sends a ! at the end so we keep that off the string if (c !=33) { serInStr[i++] = c; } } return i; } void sendserverinfo() { Serial.print(theurl); delay(100); Serial.print(endbyte, BYTE); serverinfo = true; } void writetoLED (int i) { if(i==7 && serInStr[0] == '#') { long colorVal = strtol(serInStr+1,NULL,16); // We have to invert the values here because the pins we control are acting the ground int rcolor = (((colorVal&0xff0000)>>16) - 255) * -1; int gcolor = (((colorVal&0x00ff00)>>8) - 255) * -1; int bcolor = (((colorVal&0x0000ff)>>0) - 255) * -1; analogWrite(redPin, rcolor ); analogWrite(greenPin, gcolor ); analogWrite(bluePin, bcolor ); } }