Go Down

Topic: RGB led control bluetooth Program (Read 52 times) previous topic - next topic

Creative_VFX26

Hello everyone,
I'm writing to you because i need help me on my project.  I have recently decided to create  a bluetooth controlled rgb led.  Besides i want to highlight the fact that I am completely a noob. So  in order to achieve my goal, i followed the instructions of webpage:

https://www.instructables.com/id/ArduinoAndroid-BLUETOOTH-RGB-LED-CONTROL-ANDROID-A/


However i want to improve the program, but i don't know how to do it correctly.
 First of all  , i would like the led to be switched on , and to be completely "blue" as soon as i plugged the arduino board to the computer , which is not the case(without having to use the app). Moreover i would also like the led to fade, ( while keeping the same color i have selected), 2 seconds after i select the color using the smartphone app( as described in the webpage)
 

Would you mind helping me please ????? It would be so great .


the program i have used to achieve my project:

#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial mySerial(10,11); //Define PIN11 & PIN12 as RX and TX pins

int PIN_RED = 3;
int PIN_GREEN = 5;
int PIN_BLUE = 6;

String RGB = "";
String RGB_Previous = "255.255.255";
String ON = "ON";
String OFF = "OFF";
boolean RGB_Completed = false;
 
void setup() {
  pinMode (PIN_RED, OUTPUT);
  pinMode (PIN_GREEN, OUTPUT);
  pinMode (PIN_BLUE, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  RGB.reserve(30);
}
 
void loop() {

  while(mySerial.available()){
    char ReadChar = (char)mySerial.read();
 
    if(ReadChar == ')'){
      RGB_Completed = true;
    }else{
       RGB += ReadChar;
    }
  }
 
  if(RGB_Completed){
 
      Serial.print("RGB:");
      Serial.print(RGB);
      Serial.print("     PreRGB:");
      Serial.println(RGB_Previous);
     
      if(RGB==ON){
          RGB = RGB_Previous;
          Light_RGB_LED();
      }else if(RGB==OFF){
          RGB = "0.0.0";
          Light_RGB_LED();
      }else{
          Light_RGB_LED();   
          RGB_Previous = RGB;     
      }
      RGB = "";
      RGB_Completed = false;     
  }
}
 
void Light_RGB_LED(){
 
  int SP1 = RGB.indexOf(' ');
  int SP2 = RGB.indexOf(' ', SP1+1);
  int SP3 = RGB.indexOf(' ', SP2+1);
  String R = RGB.substring(0, SP1);
  String G = RGB.substring(SP1+1, SP2);
  String B = RGB.substring(SP2+1, SP3);

  Serial.print("R=");
  Serial.println( constrain(R.toInt(),0,255));
  Serial.print("G=");
  Serial.println(constrain(G.toInt(),0,255));
  Serial.print("B=");
  Serial.println( constrain(B.toInt(),0,255));
 
  analogWrite(PIN_RED,  (R.toInt()));
  analogWrite(PIN_GREEN, (G.toInt()));
  analogWrite(PIN_BLUE,  (B.toInt()));
 
}

lesept

Hi
To turn the led on and blue at the beginning, I think you must add at the end of the setup :

Code: [Select]
RGB = "0.0.255";
Light_RGB_LED();


Have you tested the code? Is it working as you expect?
If not, I think you should change these lines
Code: [Select]
  int SP1 = RGB.indexOf(' ');
  int SP2 = RGB.indexOf(' ', SP1+1);
  int SP3 = RGB.indexOf(' ', SP2+1);
for
Code: [Select]
  int SP1 = RGB.indexOf('.');
  int SP2 = RGB.indexOf('.', SP1+1);
  int SP3 = RGB.indexOf('.', SP2+1);
Do you see the difference ? The spaces between quotes are replaces by dots.

How does it work? What kind of message do you send from the smartphone?

For the fade, it's a little more complex. You need to count 2 seconds after the led is lit. So put
Code: [Select]
delay(2000);
just before
Code: [Select]
RGB = "";
To do the fade, the easiest way is to call a function here
Code: [Select]
Fade_RGB_LED ();

Then add at the end of your code the code for the function, inspred by you own code
Code: [Select]
void Fade_RGB_LED(){
 
  int SP1 = RGB.indexOf(' ');  // Or replace space by dot
  int SP2 = RGB.indexOf(' ', SP1+1);
  int SP3 = RGB.indexOf(' ', SP2+1);
  String R = RGB.substring(0, SP1);
  String G = RGB.substring(SP1+1, SP2);
  String B = RGB.substring(SP2+1, SP3);
 int Red = R.toInt();
 int Green= G.toInt();
 int Blue= B.toInt();
 
for (int i = 0;i<255;i++) {
  analogWrite(PIN_RED, map(i,0,255,Red,0)  );
  analogWrite(PIN_GREEN, map(i,0,255,Green,0));
  analogWrite(PIN_BLUE,  map(i,0,255,Blue,0));
  delay (30);
 }
}
(not tested) If it fades too quickly, increase the value set to 30. Too slow, decrease it.

By the way, the code you copied from the instructable is very bad, it could be written much more nicely...
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Creative_VFX26

Thank for your help. However it is not working properly.
First of all,  i 'm using the app of the webpage in order to send information to the arduino board via bluetooth.
And yes the code was working perfectly before.
 However there is a problem with the new code you gave me. First of all the light is blue at the beginning, yet when  i try to switch color using the app it becomes white ( it might be meaning that the blue channel stays at 255) Moreover the light is not fading at all. I don't understand.
Can you help me please ?

Go Up