// Pin definitions for the RGB LED
const int redPin = 9; // Red LED pin
const int greenPin = 10; // Green LED pin
const int bluePin = 11; // Blue LED pin
void setup() {
// Initialize serial communication for user input
Serial.begin(9600);
// Set RGB LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Print instructions for the user
Serial.println("Enter a color name to change the RGB LED:");
Serial.println("Supported colors: red, green, blue, yellow, cyan, magenta, white, off.");
}
void loop() {
// Check if there is serial input available
if (Serial.available() > 0) {
// Read the user input as a string
String input = Serial.readStringUntil('\n');
input.trim(); // Remove any trailing newline or spaces
input.toLowerCase(); // Convert to lowercase for case-insensitivity
// Map the input to RGB values and set the LED
if (input == "red") {
setColor(255, 0, 0);
} else if (input == "green") {
setColor(0, 255, 0);
} else if (input == "blue") {
setColor(0, 0, 255);
} else if (input == "yellow") {
setColor(255, 255, 0);
} else if (input == "cyan") {
setColor(0, 255, 255);
} else if (input == "magenta") {
setColor(255, 0, 255);
} else if (input == "white") {
setColor(255, 255, 255);
} else if (input == "off") {
setColor(0, 0, 0);
} else {
Serial.println("Invalid color. Supported colors: red, green, blue, yellow, cyan, magenta, white, off.");
}
}
}
// Function to set the RGB LED color
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
// Provide feedback to the user
Serial.print("LED set to: R=");
Serial.print(redValue);
Serial.print(", G=");
Serial.print(greenValue);
Serial.print(", B=");
Serial.println(blueValue);
}