Hello everyone,
I am trying to change the colour of led strip WS2812B using smartphone app, bluetooth module HC-05 and FastLed library.
Example of received string from App: 4A255S149D136F
I extracted each colour of RGB led strip using substring function and changed to int.
And after the colour update led strip doesn't change, blink or went off.
I will be very greatful if someone look at my code and give me advice what I can do (void pixel_colour() section).
Bluetooth baud rate and voltages to RX, TX seems to be ok. ON and OFF buttons in the App works fine.
I tried so many approachings to the problem and finish with code below:
#include <FastLED.h>
#include <SoftwareSerial.h>
#define NUM_LEDS 6
#define DATA_PIN 5
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
CRGB leds[NUM_LEDS];
SoftwareSerial Bluetooth(53, 52);
int brightness = 200;
String dataI = "";
char dataIn;
int valG = 100;
int valR = 100;
int valB = 100;
void setup() {
delay(30);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness (brightness);
Serial.begin(9600);
Bluetooth.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
dataIn = Serial.read();
Serial.println(dataIn);
delay(20);
if (dataIn == '1') {
delay(10);
for (int fullStrip = 0; fullStrip <= NUM_LEDS - 1; fullStrip++) {
leds[fullStrip] = CRGB(valR, valG, valB);
}
}
else if (dataIn == '2') {
delay(10);
for (int fullStrip = 0; fullStrip <= NUM_LEDS - 1; fullStrip++) {
leds[fullStrip] = CRGB(0, 0, 0);
}
}
FastLED.show();
delay(30);
}
}
void pixel_colour() {
while (Serial.available() > 0) {
dataI = Serial.readString();
delay(20);
if (dataI.startsWith("4")) {
String G = dataI.substring(dataI.indexOf("A") + 1, dataI.indexOf("S"));
valG = G.toInt();
String R = dataI.substring(dataI.indexOf("S") + 1, dataI.indexOf("D"));
valR = R.toInt();
String B = dataI.substring(dataI.indexOf("D") + 1, dataI.indexOf("F"));
valB = B.toInt();
}
for (int fullStrip = 0; fullStrip <= NUM_LEDS - 1; fullStrip++) {
leds[fullStrip] = CRGB(valG, valR, valB);
}
FastLED.show();
delay(30);
}
}