/* hex2rgb Accept hex colour values from serial input, and control an RGB LED accordingly. Input values should begin with a # and be 6 characters long e.g. #FF00FF (magenta) Tristan Roddis, 2008 (tristan@roddis.org) Distribute freely. */ // which pins the LED leads are connected to (common cathode goes to GND) int redPin = 9; int greenPin = 10; int bluePin = 11; int RESET = 7; //reset pin for bluetooth // initial values after reset int redValue = 0; int greenValue = 255; int blueValue = 0; void reset_bt(){ // Reset the bluetooth interface digitalWrite(RESET, HIGH); delay(10); digitalWrite(RESET, LOW); delay(2000); } void config_bt(){ Serial.println("SET BT PAGEMODE 3 2000 1"); Serial.println("SET BT NAME ARDUINOBT"); Serial.println("SET BT ROLE 0 f 7d00"); Serial.println("SET CONTROL ECHO 0"); Serial.println("SET BT AUTH * 12345"); Serial.println("SET CONTROL ESCAPE - 00 1"); } void setup() { // reset_bt(); // config_bt(); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); // Serial.begin(9600); Serial.begin(115200); // connect to the serial port // Reset the bluetooth interface digitalWrite(RESET, HIGH); delay(10); digitalWrite(RESET, LOW); delay(2000); //configure the bluetooth module Serial.println("SET BT PAGEMODE 3 2000 1"); Serial.println("SET BT NAME BTLAMP"); Serial.println("SET BT ROLE 0 f 7d00"); Serial.println("SET CONTROL ECHO 0"); Serial.println("SET BT AUTH * 12345"); Serial.println("SET CONTROL ESCAPE - 00 1"); colourCycle(); // Serial.println("Started up"); } void loop() { Serial.println("ready"); if (Serial.available() > 0) { byte inByte = Serial.read(); if (inByte == '#') { char colourString[6]; Serial.println("reading colour"); // read first 6 characters int i = 0; while (Serial.available() > 0 && i < 6) { colourString[i] = Serial.read(); i++; } // split into individual colour values // strtol = string to long - converts from hex to decimal char redString[3] = {colourString[0], colourString[1] }; int redVal = strtol(redString, NULL, 16); char greenString[3] = {colourString[2], colourString[3] }; int greenVal = strtol(greenString, NULL, 16); char blueString[3] = {colourString[4], colourString[5] }; int blueVal = strtol(blueString, NULL, 16); // write it back (for debugging) Serial.println(redVal); Serial.println(greenVal); Serial.println(blueVal); // bounds checking if ( (redVal >= 0 && redVal <= 255 && greenVal >= 0 && greenVal <= 255 && blueVal >= 0 && blueVal <= 255) ) { blinkOK(); // set LED values redValue = redVal; greenValue = greenVal; blueValue = blueVal; } else { // values out of range blinkError(); } } } else { analogWrite(redPin, redValue); analogWrite(greenPin, greenValue); analogWrite(bluePin, blueValue); } } void blinkError() { digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); digitalWrite(redPin, 1); delay(500); digitalWrite(redPin, 0); delay(500); digitalWrite(redPin, 1); delay(500); digitalWrite(redPin, 0); } void blinkOK() { digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); digitalWrite(greenPin, 1); delay(500); digitalWrite(greenPin, 0); digitalWrite(bluePin, 1); delay(500); digitalWrite(bluePin, 0); digitalWrite(greenPin, 1); delay(500); digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); } void colourCycle() { int redValue = 0; int greenValue = 0; int blueValue = 0; int pwm = 0; while (pwm < 766) { // set colours according to value of pwm if (pwm < 256) { redValue = pwm; greenValue = 0; blueValue = 255 - pwm; } else if (pwm < 511) { redValue = 510 - pwm; greenValue = pwm - 255; blueValue = 0; } else if (pwm < 766) { redValue = 0; greenValue = 765 -pwm; blueValue = pwm - 510; } analogWrite(redPin, redValue); analogWrite(greenPin, greenValue); analogWrite(bluePin, blueValue); //Serial.println(pwm); delay(3); pwm++; } }