Tuesday, May 26, 2015

RGB Slider Color Selector RGB LED | Android and Arduino

This post is about choosing color for the RGB LED light from the android app, I used slider to control the LED colors.




Circuit Diagram for common anode RGB LED:

Circuit Diagram for common cathode RGB LED:


components needed for this project:
Arduino Uno
RGB LED
Bluetooth Module
Connecting Wires
Breadboard 

Android app created using MIT app inventor, which is very simple, 3 sliders had created to assign values of primary colors RGB, when these sliders are moved these different colors are mixed and shown in the canvas, same color code will be send to the arduino. 

Android app: 

android app can be download from this link

Blocks to create Android app:








Arduino Program to control Common anode LED: 

#include <SoftwareSerial.h>

int bluetoothTx = 7;
int bluetoothRx = 8;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
 pinMode(3,OUTPUT); // blue pin of RGB LED
 pinMode(5,OUTPUT); // Green pin of RGB LED
 pinMode(6,OUTPUT); // Red pin of RGB LED

 digitalWrite(3,HIGH);
 digitalWrite(5,HIGH);
 digitalWrite(6,HIGH);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int color1 = bluetooth.read();
    unsigned int color2 = bluetooth.read();
    unsigned int color = (color2 *256) + color1;
    Serial.println(color);
   
    if (color >= 1000 && color <1255){
    int blue = color;
    blue = map(blue, 1000,1255,0,255);
    int blue1 = 255-blue;
    analogWrite(6,blue1);
    Serial.println(blue1);
    delay(10);

    }
   
    if (color >=2000 && color <2255){
      int green = color;
      green = map(green,2000,2255,0,255);
      int green1 = 255 - green;
      analogWrite(5,green1);
      Serial.println(green1);
      delay(10);
     
    }
   
    if (color >=3000 && color < 3255){
      int red = color;
      red = map(red, 3000, 3255,0,255);
      int red1 = 255 - red;
      analogWrite(3,red1);
      Serial.println(red1);
      delay(10);
    }
   

  }


}


Arduino program for common cathode RGB LED:

#include <SoftwareSerial.h>

int bluetoothTx = 7;
int bluetoothRx = 8;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
 pinMode(3,OUTPUT); // blue pin of RGB LED
 pinMode(5,OUTPUT); // Green pin of RGB LED
 pinMode(6,OUTPUT); // Red pin of RGB LED

 digitalWrite(3,LOW);
 digitalWrite(5,LOW);
 digitalWrite(6,LOW);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int color1 = bluetooth.read();
    unsigned int color2 = bluetooth.read();
    unsigned int color = (color2 *256) + color1;
    Serial.println(color);
   
    if (color >= 1000 && color <1255){
    int blue = color;
    blue = map(blue, 1000,1255,0,255);
    analogWrite(6,blue);
    Serial.println(blue);
    delay(10);

    }
   
    if (color >=2000 && color <2255){
      int green = color;
      green = map(green,2000,2255,0,255);
      analogWrite(5,green);
      Serial.println(green);
      delay(10);
     
    }
   
    if (color >=3000 && color < 3255){
      int red = color;
      red = map(red, 3000, 3255,0,255);
      analogWrite(3,red);
      Serial.println(red);
      delay(10);
    }
   

  }


}

14 comments:

  1. Hello ,

    All is OK except the leds whith the same connexions (bluetooth connexion and colrs on the screen off my smartphone is ok but the arduino do not response .

    Perhaps ahd you have similar problem.
    Thanks for your answer.

    ReplyDelete
  2. Not up to the mark! In the inventor in the last block you need to write slider3 instead of slider2!

    ReplyDelete
  3. Hi can i get your .aia files so i can change the design of your app?
    Please send it to my email:manalangjjustin@gmail.com

    ReplyDelete
    Replies
    1. Why you need the aia file... it says exactly how to build the blocks above

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hey ! Can I get your Library #include ? Pleaseeee

    ReplyDelete
    Replies
    1. please send it to my email address : manalangjjustin@gmail.com

      Delete
  6. help me please my app is getting crash and aurdino is not responding after few trials

    ReplyDelete
  7. Hi I can ask you if you can send the app in .aia format so I can open it with app inventor and can i understand it better? My email is: geolmarano@yahoo.it. Thank you

    ReplyDelete
  8. Hey can you explain how this
    unsigned int color1 = bluetooth.read();
    unsigned int color2 = bluetooth.read();
    unsigned int color = (color2 *256) + color1;
    Makes it work? I tried to figure it out for myself, but the bluetooth read would only ever give me values under 255. How does your math operation work out to give the desired results? I'd like to know out of curiosity, as I've been trying to get the color shift to work to no luck. Thank you for posting your code, it really helped me out.

    ReplyDelete
  9. After completing this application,I have continiously facing an "ERROR 510"
    Function Send2ByteNumber, Message could not decode"false" as an integer. If
    possible can you send me your Apk file.

    ReplyDelete
  10. Most amazing MIT appinventor.Great thanks.

    ReplyDelete