const int redpin=11;
const int greenpin =10;
const int bluepin =9;
int d = 500;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
analogWrite(redpin,0);
analogWrite(greenpin,0);
analogWrite(bluepin,0);
Serial.begin(9600);
}
void loop() {
Serial.println("Enter a color (r = Red, g = Green, b = Blue, c = Cyan, m = Magenta, y = Yellow): ");
while (!Serial.available());
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(d); // Small delay before the next input
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redpin, redValue);
analogWrite(greenpin, greenValue);
analogWrite(bluepin, blueValue);
}