#include <ArduinoJson.h>
#include <FastLED.h>
#define LED_PIN 4 // Pin where the LED strip is connected
#define NUM_LEDS 4 // Number of LEDs in the strip
CRGB leds[NUM_LEDS]; // Array to hold LED colors
String inputBuffer = ""; // Buffer to store incoming serial data
bool newDataAvailable = false; // Flag to indicate new data availability
// Variables to store current LED state
String currentHardware = "";
int currentStartLed = -1;
int currentEndLed = -1;
String currentLogic = "";
int currentRed = 0;
int currentGreen = 0;
int currentBlue = 0;
int currentOnDelay = 0;
int currentOffDelay = 0;
bool isBlinking = false;
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600); // Start serial communication at 9600 baud rate
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS); // Initialize the LED strip
FastLED.show(); // Turn off all the LEDs at the start
}
void loop()
{
// Check if there is incoming serial data
if (Serial.available())
{
// Read the incoming string until newline character
String inputString = Serial.readStringUntil('\n');
// Process the input string
processInput(inputString);
}
// Run the current LED state
if (currentHardware == "hardware(led)")
{
if (currentLogic == "constant")
{
// Set the LEDs to a constant color
setLEDs(currentStartLed, currentEndLed, currentRed, currentGreen, currentBlue);
}
else if (currentLogic == "blinking")
{
// Blink the LEDs with the specified delays
blinkLEDs(currentStartLed, currentEndLed, currentRed, currentGreen, currentBlue, currentOnDelay, currentOffDelay);
}
}
}
void processInput(String input)
{
// Parse JSON data
DynamicJsonDocument doc(1024);
deserializeJson(doc, input);
// Extract values from the JSON object
String type = doc["type"];
JsonObject data = doc["data"];
// Extract data from JSON object
int startLed = data["start"];
int endLed = data["end"];
String logic = data["logic"];
int red = data["red"];
int green = data["green"];
int blue = data["blue"];
int onDelay = data["ondelay"];
int offDelay = data["offdelay"];
// Validate input parameters
if (type != "hardware(led)" || red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255 || onDelay < 0 || offDelay < 0) {
Serial.println("Invalid input parameters");
return;
}
// Adjust LED range to 1-based index (no need to change as it's already 1-based)
// Validate LED range
if (startLed < 1 || endLed > NUM_LEDS || startLed > endLed) {
Serial.println("Invalid LED range");
return;
}
// Validate the logic type
if (logic != "constant" && logic != "blinking") {
Serial.println("Invalid logic type");
return;
}
// Clear previous LED state
clearLEDs();
// Store the current LED state
currentHardware = type;
currentStartLed = startLed - 1; // Adjust to 0-based index for internal use
currentEndLed = endLed - 1; // Adjust to 0-based index for internal use
currentLogic = logic;
currentRed = red;
currentGreen = green;
currentBlue = blue;
currentOnDelay = onDelay;
currentOffDelay = offDelay;
// Reset blinking state
isBlinking = (logic == "blinking");
previousMillis = 0;
Serial.println("Input processed successfully");
}
void clearLEDs() {
// Turn off all the LEDs
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}
void setLEDs(int start, int end, int red, int green, int blue)
{
// Set the color of each LED in the specified range
for (int i = start; i <= end; i++)
{
leds[i] = CRGB(red, green, blue);
}
FastLED.show(); // Update the strip to show the new colors
}
void blinkLEDs(int start, int end, int red, int green, int blue, int onDelay, int offDelay)
{
// Get the current time
unsigned long currentMillis = millis();
// Check if the delay period has elapsed
if (currentMillis - previousMillis >= (isBlinking ? onDelay : offDelay)) {
// Save the current time
previousMillis = currentMillis;
// Toggle the blinking state
isBlinking = !isBlinking;
// Set or clear the color of each LED in the specified range
for (int i = start; i <= end; i++)
{
leds[i] = isBlinking ? CRGB(red, green, blue) : CRGB::Black;
}
FastLED.show(); // Update the strip to show the new colors
}
}