/* * 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 #define lcdrxPin 2 // not used - the LCD only receives data #define lcdtxPin 3 // pin that connects to the lcd rx pin #define ledPin 13 // a little blinky to see the data flowing int redPin = 9; // Red LED, connected to digital pin 9 int greenPin = 11; // Green LED, connected to digital pin 10 int bluePin = 10; // Blue LED, connected to digital pin 11 // set up a new serial port SoftwareSerial LCDSerial = SoftwareSerial(lcdrxPin, lcdtxPin); byte pinState = 0; int endbyte = 35; //end of serial data long previousMillis = 0; // will store last time data was sent to serial port long interval = 15000; // interval at which to send data (milliseconds) // theurl is what the phone will use to connect to a website // the format is domain,/path/filename.ext -- Note the comma and no http:// // make sure to change the server and page path to match your server adddress char theurl[] = "website.com,/pathtofile/filename.php"; #define slen 64 // max lenght for the serial string - serial buffer max is 128 bytes char serInStr[slen]; // array to hold the incoming serial string bytes boolean serverinfo = false; // variable to define initial settings void setup() { // sets the led pins as output pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); // define pin modes for tx, rx, led pins: pinMode(lcdrxPin, INPUT); pinMode(lcdtxPin, OUTPUT); pinMode(ledPin, OUTPUT); analogWrite(redPin, 254); // set them all to mid brightness analogWrite(greenPin, 254); // set them all to mid brightness analogWrite(bluePin, 254); // set them all to mid brightness // set the data rate for the SoftwareSerial port LCDSerial.begin(9600); // initalize the lcd and clear the screen clearLCD(); backlightOn(); delay(100); // set the data rate for the main serial port // 9600 is just the value I used, depending on your phone (or other device) // you connect to, you may want to bump it up Serial.begin(9600); } void loop() { // start the count down if (millis() - previousMillis > interval) { // if the end has been met previousMillis = millis(); // zero the counter // we just need the phone to fetch data from the website defined by the theurl variable // so only the endbyte is passed 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 get request to if (serInStr[0] == (char) endbyte && serverinfo == false) { LCDSerial.print("sending info"); // update LCD with status delay(1000); // wait a bit for the LCD to catch up 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 web data - so we print to the lcd writetolcd(datalenght); // if you are not using a LCD, you can use pick apart the info // on the serInStr string and make things happen like turn a pin high or low etc // in this case we expect to get some hex from the web and pass them to a RGB LED writetoLED(datalenght); } } } int readSerialString () { int i=0; // if the serial is not available, we return an error // you will notice i'm not really checking for errors in the main loop but the sample I copied this from was ;) if(!Serial.available()) { return -1; } int c = 0; // we wait on the serial port while it is open // and not too long // we append each byte received to serInStr // in the end we return the lenght of the data received while (Serial.available() && i < slen) { 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 writetolcd (int i) { // This could have been easier but the lcd is twichy // so i found somewhere some info on how to send // the info one byte at a time if(i==7) { clearLCD(); } for (int f = 0; f < i; f++){ LCDSerial.print(serInStr[f]); delay(200); toggle(13); } } void writetoLED (int i) { if(i==7 && serInStr[0] == '#') { long colorVal = strtol(serInStr+1,NULL,16); // we need to invert the values received because the pins we can control // are acting as 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 ); } } // the remaining functions are straight from the Arduino Playground LCD example // http://www.arduino.cc/playground/Learning/SparkFunSerLCD void toggle(int pinNum) { // set the LED pin using the pinState variable: digitalWrite(pinNum, pinState); // if pinState = 0, set it to 1, and vice versa: pinState = !pinState; } void selectLineOne(){ //puts the cursor at line 0 char 0. LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(128, BYTE); //position } void selectLineTwo(){ //puts the cursor at line 0 char 0. LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(192, BYTE); //position } void clearLCD(){ LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(0x01, BYTE); //clear command. } void backlightOn(){ //turns on the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(157, BYTE); //light level. } void backlightOff(){ //turns off the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(128, BYTE); //light level for off. } void serCommand(){ //a general function to call the command flag for issuing all other commands LCDSerial.print(0xFE, BYTE); } void setlenght(){ //turns off the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(4, BYTE); //light level for off. }