#define redPin 13
#define greenPin 12
#define bluePin 11
#define colorBtn 6
#define durationBtn 7
bool colors[][3] = {
{1,1,1}, {1,0,0}, {0,1,0}, {0,0,1}, // white, red, green, blue
{0,1,1}, {1,1,0}, {1,0,1}, {0,0,0} // cyan, yellow, magenta, black
};
char* colorNames[] = {
"white", "red", "green", "blue",
"cyan", "yellow", "magenta", "black"
};
int delays[] = {1000, 800, 600, 400, 200};
volatile int colorIndex = 0;
volatile int delayIndex = 0;
unsigned long lastColorPress = 0;
unsigned long lastDurationPress = 0;
const unsigned long debounceDelay = 200;
unsigned long previousMillis = 0;
bool ledState = false;
void setup() {
Serial.begin(9600);
// Set RGB pins as output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Set button pins as input with pull-up
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
// Attach interrupts
attachInterrupt(digitalPinToInterrupt(colorBtn), changeColor, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), changeDuration, FALLING);
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
Serial.print("Duration: ");
Serial.print(delays[delayIndex]);
Serial.println(" ms");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delays[delayIndex]) {
previousMillis = currentMillis;
ledState = !ledState;
applyColor(ledState ? colorIndex : 7); // 7 = black (off)
}
}
void applyColor(int index) {
digitalWrite(redPin, colors[index][0]);
digitalWrite(greenPin, colors[index][1]);
digitalWrite(bluePin, colors[index][2]);
}
void changeColor() {
unsigned long now = millis();
if (now - lastColorPress > debounceDelay) {
colorIndex = (colorIndex + 1) % 7; // skip black
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
lastColorPress = now;
}
}
void changeDuration() {
unsigned long now = millis();
if (now - lastDurationPress > debounceDelay) {
delayIndex = (delayIndex + 1) % 5;
Serial.print("Duration: ");
Serial.print(delays[delayIndex]);
Serial.println(" ms");
lastDurationPress = now;
}
}