#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", "blue", "green", "red",
"yellow", "cyan", "magenta", "black"
};
int delays[] = {
1000, 800, 600, 400, 200
};
volatile int currentColor = 0;
volatile int currentDelay = 0;
unsigned long lastColorPress = 0;
unsigned long lastDurationPress = 0;
const unsigned long debounceDelay = 1000;
unsigned long previousMillis = 0;
bool ledState = LOW;
void setup()
{
Serial.begin(9600);
pinMode(11, OUTPUT); // R
pinMode(12, OUTPUT); // G
pinMode(13, OUTPUT); // B
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), changeColor, RISING);
attachInterrupt(digitalPinToInterrupt(durationBtn), changeDuration, RISING);
printState();
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delays[currentDelay])
{
previousMillis = currentMillis;
ledState = !ledState;
digitalWrite(11, ledState && colors[currentColor][0]);
digitalWrite(12, ledState && colors[currentColor][1]);
digitalWrite(13, ledState && colors[currentColor][2]);
}
delay(10);
}
void changeColor()
{
unsigned long currentMillis = millis();
if (currentMillis - lastColorPress >= debounceDelay)
{
currentColor = (currentColor + 1) % 8;
lastColorPress = currentMillis;
printState();
}
}
void changeDuration()
{
unsigned long currentMillis = millis();
if (currentMillis - lastDurationPress >= debounceDelay)
{
currentDelay = (currentDelay + 1) % 5;
lastDurationPress = currentMillis;
printState();
}
}
void printState()
{
Serial.print("Current Color: ");
Serial.println(colorNames[currentColor]);
Serial.print("Current Delay: ");
Serial.print(delays[currentDelay]);
Serial.println(" ms");
}