Addressible RGB LED Control with HC 05 Bluetooth Module`

Hi Everyone,

I am trying to code my addressible RGB LED strip with a HC -05 bluetooth module. SO basically send data from my phone to the arduino and want the arduino to react to the data it recieves. The data is stored in 'char' data type.

The characters are recieved individually and i want to convert it to string (or any other method) so that the if statement can understand the command.

Please help me

#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 149
#define DATA_PIN 3
#define BRIGHTNESS  128
CRGB leds[NUM_LEDS];      //Setting up the led array. Array is called leds and the Size of the array is the no of leds 

char data;

void setup() {
Serial.begin(9600);

FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  
    if(Serial.available() > 0)
    {
      data=Serial.read();
      Serial.print(data);
      
      //Serial.print(" ");
        if (data == 'O'+'N'+')')??  //the data recieved is 3 character ON) from the app. How do I write the if statement to understand that command
        {
          Serial.println("Something has come through ");
        }
        if (data == 'OFF)')
        {
          Serial.println("ON has come through ");
        }
            for(int i = 0; i<NUM_LEDS; i++){
             fill_solid(leds, i, CRGB::Red);
             FastLED.show();
             delay(10);
            }
            for(int i = NUM_LEDS; i>= 0 ; i--){
              leds[i] = CRGB::Black;
              FastLED.show();
              delay(10);
            }
              fill_solid(leds, NUM_LEDS, CRGB::Blue);
              FastLED.show();
              delay(5000);
          
          Serial.print("Mithi you rock!");
        }
        if(data == '2')
        {
         fill_gradient_RGB(leds, NUM_LEDS, CRGB::Blue, CRGB::Red, CRGB::Green);
          FastLED.show();
          delay(5000);
  
          fill_rainbow(leds, NUM_LEDS, 0, 255/NUM_LEDS);
          FastLED.show();
          delay(5000);
        }
    }
    //delay(10);
}
}

Look up the Arduino/referens/serial. It explians a lot regarding serial and its chars.
The vaiable data can only hold one char, "ON" or "Off".
Use single character as the commands. O for on and some other character for Off.

#include <FastLED.h>
#define NUM_LEDS 149
#define DATA_PIN 3
#define BRIGHTNESS  128
CRGB leds[NUM_LEDS];      //Setting up the led array. Array is called leds and the Size of the array is the no of leds

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  fill_solid(leds, NUM_LEDS, CRGB::Blue);
  FastLED.show();
  delay(5000);
  Serial.print("Mithi you rock!");
}

void loop() {
  String data = "";
  if (Serial.available() > 0)  {
    data = Serial.readString();
    data.trim();
    Serial.print(data);
  }
  if (data == "ON)") {
    Serial.println("Something has come through ");

    for (int i = 0; i < NUM_LEDS; i++) {
      fill_solid(leds, i, CRGB::Red);
      FastLED.show();
      delay(10);
    }
  }
  if (data == "OFF)") {
    Serial.println("OFF has come through ");
    for (int i = NUM_LEDS; i >0 ; i--) {
      leds[i-1] = CRGB::Black;
      FastLED.show();
      delay(10);
    }
  }
  if (data == "2")  {
    fill_gradient_RGB(leds, NUM_LEDS, CRGB::Blue, CRGB::Red, CRGB::Green);
    FastLED.show();
    delay(5000);

    fill_rainbow(leds, NUM_LEDS, 0, 255 / NUM_LEDS);
    FastLED.show();
    delay(5000);
  }
}

Error message

data = data.trim();

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:91:11: note:

no known conversion for argument 1 from 'void' to 'StringSumHelper&&'

exit status 1

no match for 'operator=' (operand types are 'String' and 'void')

ah, yes, sorry
replace

data = data.trim();

with

data.trim();

thank you it is working now

cool

is there a way to read the first 3 letters of a string.

so the RGB values are detected as follow: xxx.yyy.zzz (for example 120.210.100)

I want to store the xxx data in an int

for example,
int red = int( String [0] && String [1] & String [2])

Is this possible?

and how Arduino recognize if it commando und if it colors?

so colours are numbers that it gets from a bluetooth app

commando 2 is number too

Could you please explain a bit more. The RGB values are numbers read as 123.221.100 by the serial monitor. I want to only pick out the 3 digit numbers separately such as

red = 123
green = 221
blue =100

is there a way to do that?

it is simple. without text commandos.

  static int ColorData[3] = {0};
  if (Serial.available() > 0)  {
    ColorData[0] = Serial.parseInt();
    ColorData[1] = Serial.parseInt();
    ColorData[2] = Serial.parseInt();   
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.