#include <WS2812FX.h>
#include <FastLED.h>
#include <SoftwareSerial.h>
#define LED_PIN 6
#define LED_COUNT 60
// WS2812FX object
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// HC-05 Bluetooth pins
SoftwareSerial BT(10, 11); // RX, TX
String input = ""; // Incoming data from app
int r = 255, g = 0, b = 0; // Default color
void setup() {
Serial.begin(9600);
BT.begin(9600); // HC-05 default baud
ws2812fx.init();
ws2812fx.setBrightness(200);
ws2812fx.setSpeed(50);
ws2812fx.start();
}
void loop() {
ws2812fx.service();
// Read Bluetooth data
while (BT.available()) {
char c = BT.read();
if(c == '\n') { // End of command
processCommand(input);
input = "";
} else {
input += c;
}
}
}
// -----------------------------
// Process Commands
// Magic LED app sends:
// "RGB:255,0,0" -> Color
// "E0" -> Effect 0
// "S50" -> Speed
// "B150" -> Brightness
void processCommand(String cmd) {
// RGB color command
if(cmd.startsWith("RGB")) {
int c1 = cmd.indexOf(':');
int c2 = cmd.indexOf(',');
int c3 = cmd.lastIndexOf(',');
r = cmd.substring(c1+1,c2).toInt();
g = cmd.substring(c2+1,c3).toInt();
b = cmd.substring(c3+1).toInt();
ws2812fx.setColor(ws2812fx.Color(r,g,b));
}
// Effect command
if(cmd.startsWith("E")) {
int effectNum = cmd.substring(1).toInt();
if(effectNum >=0 && effectNum <= 105) { // max 106 effects
ws2812fx.setMode(effectNum);
}
}
// Speed command
if(cmd.startsWith("S")) {
int spd = cmd.substring(1).toInt();
if(spd>=10 && spd<=255) ws2812fx.setSpeed(spd);
}
// Brightness command
if(cmd.startsWith("B")) {
int bri = cmd.substring(1).toInt();
if(bri>=0 && bri<=255) ws2812fx.setBrightness(bri);
}
}