Friday,29-September-2017
Android phone control RGB light
EN
That is my second post about that topic, but this time thanks to that wonderful website MIT APP INVENTOR, I do not need to use serial application from others but to have fun building my own. Here is the project:
Stage for the test : quite simple actually - one RGB diode , few resistors and one HC-05 serial dongle.
Then the arduino code:
// example 0,255,255# - full red // 0-on 255-off #include "EEPROM.h" #define RED 6// D6 timer 0 oc0a #define GREEN 5// D5 timer 0 oc0b #define BLUE 3// D3 timer 2 OCR2B int red = 0; int green = 0; int blue = 0; int bright = 100; void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); callEeprom(); TCCR0A = (1<<COM0A1) | (1<<COM0A0) | (1<<COM0B1) | (1<<COM0B0) | (1<<WGM01) | (1<<WGM00); TCCR0B = (0<< FOC0A) | (0<<FOC0B) | (0<<WGM02) | (0<<CS02) | (1<<CS01) | (1<<CS00); // prescale 64 - cs table 11-6 TCCR2A = (1<<COM2A1) | (1<<COM2A0) | (1<<COM2B1) | (1<<COM2B0) | (1<<WGM21) | (1<<WGM20); TCCR2B = (0<< FOC2A) | (0<<FOC2B) | (0<<WGM22) | (1<<CS22) | (0<<CS21) | (0<<CS20); // prescale 64 - cs table 11-6 OCR0A = 255-red; OCR0B = 255-green; OCR2A = 255; OCR2B = 255-blue; TCNT2=0; TCNT0=0; //inverted mode 255 - OFF / 0-ON Serial.begin(38400); respond(); } void loop(){ while (Serial.available() > 0) { int tmp_red, tmp_green, tmp_blue, tmp_bright; tmp_red = Serial.parseInt(); tmp_green = Serial.parseInt(); tmp_blue = Serial.parseInt(); tmp_bright = Serial.parseInt(); if (Serial.read() == '#') { //R OCR0A=255 - tmp_red; //G OCR0B=255 - tmp_green; //B OCR2B=255 - tmp_blue; EEPROM.write(1,tmp_red); EEPROM.write(2,tmp_green); EEPROM.write(3,tmp_blue); EEPROM.write(4,tmp_bright); }else{ respond(); } delay(10); } }// loop end void respond(){ callEeprom(); Serial.print(red); Serial.print("*"); Serial.print(green); Serial.print("*"); Serial.print(blue); Serial.print("*"); Serial.println(bright); } void callEeprom(){ red = EEPROM.read(1); green = EEPROM.read(2); blue = EEPROM.read(3); bright = EEPROM.read(4); }
Here fast reminder: in “Serial.begin(38400)” , replace “38400” with your HC-05(bluetooth dongle) speed.
Back in my first attempt to accomplish RGB control over BT, I use that helpful page:
http://www.easyrgb.c … m/en/math.php#text19
Now that same RGB > HSL > RGB logic is embed into the android APP , leaving arduino controller with simple task.
And what that logic is? To control the brightness of single R or G or B channel, you need only to reduce it value. But if you have complex colour like R127, G12, B240 you can`t just equally reduce it percentage. For that reason conversion to HSL colour space is made, and only L channel get change.
Also, now in that project as you can see from the code, any new RGB values are stored into EEPROM area of the chip so the next time you switch it on to load last state. In the same time that stored data will be sent on request when the application connect. That is made in one thing in mind - to synchronize app and device. Any new data from the sliders is sent and stored in real time. Of course rainbow, strobo and other effects can be added as well and used in single command button from your APP.
Some additional shots:
And video:
MIT inventor AIA file can be downloaded from here: AIA and ready application here: APK
Please, be advised I will not be responsible for any trouble on your phone/device if you try to test my application or codes!