// Define pins for RGB LED
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
const int dt = 500; // Small delay before the next input
void setup() {
// Set RGB LED pins as output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600); // Start serial communication
}
void loop() {
Serial.println("Enter a color (r = Red, g = Green, b = Blue, c = Cyan, m = Magenta, y = Yellow): ");
while (!Serial.available()); // Wait for user input
String input = Serial.readStringUntil('\n'); // Read the full input line
input.trim(); // Remove any extra whitespace, newline or carriage return
char color = input.charAt(0); // Extract the first character
// Set the LED color based on user input
switch (color) {
case 'r': setColor(255, 0, 0); break; // Red
case 'g': setColor(0, 255, 0); break; // Green
case 'b': setColor(0, 0, 255); break; // Blue
case 'c': setColor(0, 255, 255); break; // Cyan
case 'm': setColor(255, 0, 255); break; // Magenta
case 'y': setColor(255, 255, 0); break; // Yellow
default: Serial.println("Invalid color."); break;
}
delay(dt); // Small delay before the next input
}
// Function to set RGB LED color
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}