#define colorBtn 6
#define durationBtn 7
#define RED 13
#define GREEN 12
#define BLUE 11
bool colors[][3] = {
{1,1,1}, {1,0,0}, {0,1,0}, {0,0,1},
{0,1,1}, {1,1,0}, {1,0,1}, {0,0,0}
};
char* colorNames[] = {
"white","red","green","blue",
"cyan","yellow","magenta","black"
};
int delays[] = {
1000, 800, 600, 400, 200
};
int colorIndex = 0;
int delayIndex = 0;
bool ledState = false;
bool lastColorState = HIGH;
bool lastDurationState = HIGH;
unsigned long previousMillis = 0;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
Serial.begin(9600);
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
Serial.print("Duration: ");
Serial.print(delays[delayIndex]);
Serial.println(" ms");
}
void loop() {
unsigned long currentMillis = millis();
// Blink RGB LED
if(currentMillis - previousMillis >= delays[delayIndex]){
previousMillis = currentMillis;
ledState = !ledState;
digitalWrite(RED, ledState && colors[colorIndex][0]);
digitalWrite(GREEN, ledState && colors[colorIndex][1]);
digitalWrite(BLUE, ledState && colors[colorIndex][2]);
}
// Color button
bool colorState = digitalRead(colorBtn);
if(lastColorState == HIGH && colorState == LOW){
colorIndex++;
if(colorIndex >= 7)
colorIndex = 0;
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
delay(50); // debounce
}
lastColorState = colorState;
// Duration button
bool durationState = digitalRead(durationBtn);
if(lastDurationState == HIGH && durationState == LOW){
delayIndex++;
if(delayIndex >= 5)
delayIndex = 0;
Serial.print("Duration: ");
Serial.print(delays[delayIndex]);
Serial.println(" ms");
delay(50); // debounce
}
lastDurationState = durationState;
}