Skip to content

Instantly share code, notes, and snippets.

@murjam

murjam/RGBW.ino Secret

Created April 19, 2021 21:00
#include "FastLED.h"
#include "FastLED_RGBW.h"
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
#include <ArduinoJson.h>
#define NUM_LEDS 277
#define LEDS_PIN 6
#define WINDOW_FIRST_LED 183
#define WINDOW_LAST_LED 247
RGBW leds[NUM_LEDS];
CRGB *ledsRGB = (CRGB *) &leds[0];
const String MODE_RGBW = "rgbw";
const String MODE_RAINBOW = "rainbow";
const String MODE_PARTY = "party";
String mode = MODE_RGBW;
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t white = 255;
uint8_t brightness = 60;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
FastLED.addLeds<WS2812, LEDS_PIN, RGB>(ledsRGB, getRGBWsize(NUM_LEDS));
FastLED.setBrightness(20);
FastLED.show();
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
StaticJsonDocument<150> doc;
DeserializationError error = deserializeJson(doc, Serial);
if (error && error != DeserializationError::Ok) {
Serial.print("{\"error\":\"");
Serial.print(error.f_str());
Serial.println("\"}");
while (Serial.available() > 0) {
Serial.read();
}
Serial.flush();
return;
}
JsonObject root = doc.as<JsonObject>();
char* modeInput = doc["mode"].as<char*>();
String newMode = String(modeInput);
if (
MODE_RGBW.equals(newMode) ||
MODE_RAINBOW.equals(newMode) ||
MODE_PARTY.equals(newMode)
) {
mode = newMode;
if (MODE_RGBW.equals(mode)) {
if (doc["r"].is<uint8_t>()) {
red = doc["r"].as<uint8_t>();
}
if (doc["g"].is<uint8_t>()) {
green = doc["g"].as<uint8_t>();
}
if (doc["b"].is<uint8_t>()) {
blue = doc["b"].as<uint8_t>();
}
if (doc["w"].is<uint8_t>()) {
white = doc["w"].as<uint8_t>();
}
}
if (doc["brightness"].is<uint8_t>()) {
brightness = doc["brightness"].as<uint8_t>();
}
}
Serial.print("{\"mode\":\"");
Serial.print(mode);
Serial.print("\",\"r\":");
Serial.print(red);
Serial.print(",\"g\":");
Serial.print(green);
Serial.print(",\"b\":");
Serial.print(blue);
Serial.print(",\"w\":");
Serial.print(white);
Serial.print(",\"brightness\":");
Serial.print(brightness);
Serial.println("}");
Serial.flush();
}
if (MODE_RAINBOW.equals(mode)) {
FastLED.setBrightness(50);
rainbowLoop();
}
else if (MODE_PARTY.equals(mode)) {
FastLED.setBrightness(200);
disco();
}
else {
FastLED.setBrightness(brightness);
windowFill(RGBW(red, green, blue, white), RGBW(red / 7, green / 7, blue / 7, white / 7), 1000);
}
}
bool isWindowLed(int i) {
return i >= WINDOW_FIRST_LED && i <= WINDOW_LAST_LED;
}
void windowFill(RGBW other, RGBW window, int delayMs) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = isWindowLed(i) ? window : other;
}
FastLED.show();
delay(delayMs);
}
void fill(RGBW c, int delayMs) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = c;
}
FastLED.show();
delay(delayMs);
}
void disco() {
RGBW offColor = RGBW(0, 0, 0, 0);
for (int i = 0; i < 2; i++) {
fill(RGBW(CHSV(random(0, 255), 255, 255)), 200);
fill(offColor, 200);
}
RGBW randomColor = RGBW(CHSV(random(0, 255), 255, 255));
int inc = random(2, 5);
int snakeLength = 10;
for (int i = 0; i < NUM_LEDS; i+=inc) {
leds[i] = offColor;
for (int j = i + 1; j < i + snakeLength && j < NUM_LEDS; j++) {
leds[j] = randomColor;
}
FastLED.show();
}
randomColor = RGBW(CHSV(random(0, 255), 255, 255));
for (int i = NUM_LEDS - 1; i >= 0; i-=inc) {
for (int j = i + 1; j < i + snakeLength && j < NUM_LEDS; j++) {
leds[j] = randomColor;
}
if (i + snakeLength + 1 < NUM_LEDS) {
leds[i + snakeLength + 1] = offColor;
}
FastLED.show();
}
rainbowLoop();
}
void rainbow() {
static uint8_t hue;
static uint16_t rgbSize;
static boolean ascending = true;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = RGBW(CHSV((i * ((double) rgbSize / NUM_LEDS)) + hue, 255, 100));
}
FastLED.show();
hue++;
if (rgbSize == 0) {
ascending = true;
} else if (rgbSize > 20000) {
ascending = false;
}
if (ascending) {
rgbSize++;
} else {
rgbSize--;
}
}
void rainbowLoop(){
long millisIn = millis();
long loopTime = 3000;
while (millis() < millisIn + loopTime) {
rainbow();
delay(20);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment