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);
}
}
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