/*
Prototype 1 Goals = Random LED colors + Identify LED groups + Serial-based LED control.
✅ assignRandomColors() → Lights all LEDs with random Red, Green, or Blue colors and prints their indices.
✅ checkSerialCommand() → Allows changing individual LEDs via serial input (e.g., "led5,green").
✅ applyColorToLEDs() → Enables batch control of multiple LEDs at once, using both ranges and individual numbers.
USAGE: type string inside serial monitor
1) single led -> led{value},{colour}
2) led range -> led{value1}-{value2},colour
3) assign random -> "randomize"
*/
#include <Adafruit_NeoPixel.h>
#define LED_PIN 2 // Pin connected to the data input of the LED strip
#define NUM_LEDS 40 // 2 rows * 20 columns
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200); // Serial communication for LED control
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Step 1: Assign random colors and return LED indices
assignRandomColors();
}
void loop() {
// Step 2: Listen for Serial input to manually change LED colors
checkSerialCommand();
}
// Function to assign a random RED, GREEN, or BLUE color to each LED and track indices
void assignRandomColors() {
String redLEDs = "RED's are: {";
String greenLEDs = "GREEN's are: {";
String blueLEDs = "BLUE's are: {";
for (int i = 0; i < NUM_LEDS; i++) {
int randomColor = random(3); // Random number: 0, 1, or 2
uint8_t r = (randomColor == 0) ? 255 : 0;
uint8_t g = (randomColor == 1) ? 255 : 0;
uint8_t b = (randomColor == 2) ? 255 : 0;
strip.setPixelColor(i, strip.Color(r, g, b));
// Append LED index to respective list
if (randomColor == 0) redLEDs += String(i) + ", ";
if (randomColor == 1) greenLEDs += String(i) + ", ";
if (randomColor == 2) blueLEDs += String(i) + ", ";
}
strip.show();
// Remove last comma and close brackets
if (redLEDs.length() > 11) redLEDs.remove(redLEDs.length() - 2);
if (greenLEDs.length() > 15) greenLEDs.remove(greenLEDs.length() - 2);
if (blueLEDs.length() > 14) blueLEDs.remove(blueLEDs.length() - 2);
redLEDs += "}";
greenLEDs += "}";
blueLEDs += "}";
// Print results
Serial.println(redLEDs);
Serial.println(greenLEDs);
Serial.println(blueLEDs);
}
// Function to check for Serial input and set LED color(s) accordingly
void checkSerialCommand() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read input command
command.trim(); // Remove unwanted spaces
// Add this block to trigger random re-coloring
if (command.equalsIgnoreCase("randomize")) {
assignRandomColors();
Serial.println("LEDs randomized.");
return; // Exit early
}
if (command.startsWith("led")) {
int commaIndex = command.lastIndexOf(',');
if (commaIndex > 0) {
String ledList = command.substring(3, commaIndex); // Extract LED numbers
String color = command.substring(commaIndex + 1); // Extract color
applyColorToLEDs(ledList, color);
}
}
}
}
// Function to apply color to multiple LEDs (supports individual LEDs and ranges)
void applyColorToLEDs(String ledList, String color) {
uint32_t newColor = parseColor(color);
// Split the LED list by commas
int start = 0;
int nextComma = ledList.indexOf(',');
while (start >= 0) {
String segment;
if (nextComma == -1) {
segment = ledList.substring(start);
} else {
segment = ledList.substring(start, nextComma);
}
segment.trim();
if (segment.indexOf('-') != -1) {
// Handle range format (e.g., 5-10)
int dashIndex = segment.indexOf('-');
int startRange = segment.substring(0, dashIndex).toInt();
int endRange = segment.substring(dashIndex + 1).toInt();
if (startRange >= 0 && endRange < NUM_LEDS && startRange <= endRange) {
for (int i = startRange; i <= endRange; i++) {
strip.setPixelColor(i, newColor);
}
} else {
Serial.println("Error: Invalid range.");
}
} else {
// Handle individual LEDs (e.g., 5, 8, 12)
int ledNumber = segment.toInt();
if (ledNumber >= 0 && ledNumber < NUM_LEDS) {
strip.setPixelColor(ledNumber, newColor);
} else {
Serial.println("Error: Invalid LED number.");
}
}
if (nextComma == -1) {
start = -1;
} else {
start = nextComma + 1;
nextComma = ledList.indexOf(',', start);
}
}
strip.show();
Serial.print("Updated LEDs (");
Serial.print(ledList);
Serial.print(") to ");
Serial.println(color);
}
// Function to convert color names to RGB values
uint32_t parseColor(String color) {
color.toLowerCase();
if (color == "red") return strip.Color(255, 0, 0);
if (color == "green") return strip.Color(0, 255, 0);
if (color == "blue") return strip.Color(0, 0, 255);
if (color == "yellow") return strip.Color(255, 255, 0);
if (color == "cyan") return strip.Color(0, 255, 255);
if (color == "magenta") return strip.Color(255, 0, 255);
if (color == "white") return strip.Color(255, 255, 255);
if (color == "off") return strip.Color(0, 0, 0);
return strip.Color(0, 0, 0); // Default: Off if invalid color
}