Created
February 17, 2026 08:33
-
-
Save DivyanshuLohani/df8e160672d1debd40cb573ccb73120d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <FastLED.h> | |
| /************************************** | |
| U S E R S E T U P | |
| **************************************/ | |
| #define MAX_LEDS 10 // number of controllable segments (NOT physical LEDs) | |
| #define LED_TYPE WS2811 | |
| #define LED_PINS 6 | |
| #define COLOR_ORDER BRG | |
| #define BRIGHTNESS 255 | |
| #define SERIAL_RATE 115200 | |
| #define OFF_TIMEOUT 15000 // ms before auto-off if no data (0 disables) | |
| /************************************** | |
| L E D S E T U P | |
| **************************************/ | |
| CRGB leds[MAX_LEDS]; | |
| unsigned long lastDataTime = 0; | |
| /************************************** | |
| S I M P L E {R,G,B} P A R S E R | |
| **************************************/ | |
| char buffer[20]; | |
| uint8_t bufIndex = 0; | |
| uint16_t currentLed = 0; | |
| void processBlock(char *data) { | |
| int r, g, b; | |
| // Parse "{r,g,b}" | |
| if (sscanf(data, "{%d,%d,%d}", &r, &g, &b) == 3) { | |
| if (currentLed < MAX_LEDS) { | |
| leds[currentLed].r = constrain(r, 0, 255); | |
| leds[currentLed].g = constrain(g, 0, 255); | |
| leds[currentLed].b = constrain(b, 0, 255); | |
| currentLed++; | |
| } | |
| } | |
| } | |
| void setup() { | |
| Serial.begin(SERIAL_RATE); | |
| FastLED.addLeds<LED_TYPE, LED_PINS, COLOR_ORDER>(leds, MAX_LEDS); | |
| FastLED.setBrightness(BRIGHTNESS); | |
| FastLED.clear(); | |
| FastLED.show(); | |
| lastDataTime = millis(); | |
| } | |
| /************************************** | |
| M A I N L O O P | |
| **************************************/ | |
| void loop() { | |
| // ---- Read serial stream ---- | |
| while (Serial.available()) { | |
| char c = Serial.read(); | |
| if (c == '{') { // start new block | |
| bufIndex = 0; | |
| buffer[bufIndex++] = c; | |
| } | |
| else if (bufIndex > 0) { // inside block | |
| buffer[bufIndex++] = c; | |
| if (c == '}') { // block finished | |
| buffer[bufIndex] = '\0'; | |
| processBlock(buffer); | |
| bufIndex = 0; | |
| } | |
| if (bufIndex >= sizeof(buffer) - 1) { | |
| bufIndex = 0; // overflow protection | |
| } | |
| } | |
| } | |
| // ---- When all LEDs updated → show frame ---- | |
| if (currentLed >= MAX_LEDS) { | |
| FastLED.show(); | |
| currentLed = 0; | |
| lastDataTime = millis(); | |
| } | |
| // ---- Optional auto-off if stream stops ---- | |
| if (OFF_TIMEOUT > 0 && millis() - lastDataTime > OFF_TIMEOUT) { | |
| FastLED.clear(); | |
| FastLED.show(); | |
| lastDataTime = millis(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment