uuid='00001101-0000-1000-8000-00805F9B34FB' #couldn't get bluetooth to work without this uuid..
connId=droid.bluetoothConnect(uuid,mac).result #connect to bluetooth moduel mac specified above
droid.bluetoothWrite('RAINBOWHSV 4095 150\n') #Write this string out via serial RAINBOWHSV=command 4095=S(saturation) 150=V(brightness)
droid.makeToast("Rainbow on for" + str(onDuration)) #pop up an android "toast" saying the duration
droid.bluetoothWrite('ALLOFF\n') #Send ALLOFF command over serial to turn stop RAINBOWHSV function
********************************************************************************************
#include <SerialCommand.h>
SerialCommand sCmd; // The SerialCommand object
const int RedLED = 3; // red led connected to PWM pin
const int GreenLED = 4; // green led connected to PWM pin
const int BlueLED = 5; // blue led connected to PWM pin
const int UVLED = 6; // uv led connected to PWM pin
void setup() {
analogWriteResolution(12); //set PWM to 12 bit resolution (Only teensy 3 supports 12bit pwm, you would need to change the formulas for 8bit pwm on arduino)
Serial1.begin(9600); //only using 9600 buad for now, for arduino you would need to use "Serial" instead of "Serial1"
pinMode(RedLED, OUTPUT); // set pin as output
pinMode(GreenLED, OUTPUT); // set pin as output
pinMode(BlueLED, OUTPUT); // set pin as output
pinMode(UVLED, OUTPUT); // set pin as output
analogWrite(RedLED, 0); //Set all LEDs to off
analogWrite(GreenLED, 0);
analogWrite(BlueLED, 0);
analogWrite(UVLED, 0);
// Setup callbacks for SerialCommand commands
sCmd.addCommand("RED", RED_led); //RED + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
sCmd.addCommand("GREEN", GREEN_led); //GREEN + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
sCmd.addCommand("BLUE", BLUE_led); //BLUE + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
sCmd.addCommand("UV", UV_led); //UV + analog write argument (Teensy 3 0 to 4096, Arduino 0 to 256)
sCmd.addCommand("ALLOFF", LEDS_off); // Turns LED off
sCmd.addCommand("TEST", Test_reply); // Relpys back "Testing, testing 1, 2, 3" over bluetooth serial
sCmd.addCommand("RAINBOWSIN", RainbowSin); //Do a rainbow using 3x 120deg phase seperated sine waves (doesn't look quite as good)
sCmd.addCommand("RAINBOWHSV", RainbowHSV); //Do a rainbow using cycling HUE + Saturation & Brightness serial arguments
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
}
void loop() {
sCmd.readSerial(); //not doing much in loop, just listening for serial commands then running attached functions when valid command is received
}
void RED_led() {
int aNumber;
char *arg;
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg);
analogWrite(RedLED, aNumber);
}
}
void GREEN_led() {
int aNumber;
char *arg;
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg);
analogWrite(GreenLED, aNumber);
}
}
void BLUE_led() {
int aNumber;
char *arg;
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg);
analogWrite(BlueLED, aNumber);
}
}
void UV_led() {
int aNumber;
char *arg;
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg);
analogWrite(UVLED, aNumber);
}
}
void LEDS_off() {
analogWrite(RedLED, 0);
analogWrite(GreenLED, 0);
analogWrite(BlueLED, 0);
analogWrite(UVLED, 0);
}
void RainbowSin() {
LEDS_off();
while(!Serial1.available()){
int LED1; // these variables will be used to hold the led PWM values
int LED2;
int LED3;
float x; /*this a variable that will receive the angle value from variable i. This value is converted to radians in the sine function and will be used to generate the PWM values */
float r; // these variables will receive the PWM values calculated by the three sine functions
float g;
float b;
for (int i=0; i<360; i++){
//convert i into a floating point variable that can be used with PI
x=float(i);
/* to calculate r,g,b the sine function is modified to increase amplitute (127*) to create a phase shift (x+1/2*PI) and (x+3/2*PI) finally the sine wave is raised to illiminate negative values below zero by adding 1*/
r=127*(sin(x/180*PI)+1);
g=127*(sin(x/180*PI+3/2*PI)+1);
b=127*(sin(x/180*PI+0.5*PI)+1);
//convert flaot r,g,b to integers that can be assigned to LED PWM numbers
LED1= int(r);
LED2= int(g);
LED3= int(b);
//write LED levels to p0, p1, p4 (ASSIGN PWM values to LEDs)
analogWrite (RedLED,LED1);
analogWrite (GreenLED,LED2);
analogWrite (BlueLED,LED3);
//wait for 1/100 of a second
delay(50);
if(i>=360){
i=0;
}
}
}
}
void RainbowHSV(){
LEDS_off();
for (int i=0; i <= 12284; i++){ //cycle hue from 0 to 12284
if (i >= 12284){
i=0;
}
hue = i;
int32_t hue;
int32_t sat;
int32_t bri;
int32_t red;
int32_t green;
int32_t blue;
int32_t red_val;
int32_t green_val;
int32_t blue_val;
char *arg;
arg = sCmd.next();
if (arg != NULL) {
sat = atoi(arg);
}
arg = sCmd.next();
if (arg != NULL) {
bri = atoi(arg);
}
while (hue > 12284) hue -= 12285;
while (hue < 0) hue += 12285;
if (hue < 4095) {
red_val = (5591040 - sat * (hue - 2730)) >> 12;
green_val = (5591040 - sat * (1365 - hue)) >> 12;
blue_val = (5591040 - sat * 1365) >> 12;
}
else if (hue < 8190) {
red_val = (5591040 - sat * 1365) >> 12;
green_val = (5591040 - sat * (hue - 6825)) >> 12;
blue_val = (5591040 - sat * (5460 - hue)) >> 12;
}
else {
red_val = (5591040 - sat * (9555 - hue)) >> 12;
green_val = (5591040 - sat * 1365) >> 12;
blue_val = (5591040 - sat * (hue - 10920)) >> 12;
}
red = ((bri + 1) * red_val) >> 12;
green = ((bri + 1) * green_val) >> 12;
blue = ((bri + 1) * blue_val) >> 12;
analogWrite(RedLED, red);
analogWrite(GreenLED, green);
analogWrite(BlueLED, blue);
if (Serial1.available()){ //if serial received break out of this loop you it can receive other commands
break;
}
delay(5);
//Serial1.print("Hue=");
//Serial1.println(hue);
}
}
void Test_reply() {
Serial1.println("Testing, testing 1, 2, 3");
}
void unrecognized(const char *command) {
Serial1.println("What?");
}