#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} // Cyan, Yellow, Magenta
};
char* colorNames[] = {
"White", "Red", "Green", "Blue",
"Cyan", "Yellow", "Magenta"
};
int durations[] = {
1000, 800, 600, 400, 200
};
volatile int i = 0;
volatile int x = 0;
unsigned long Millis = 0;
bool ledState = false;
unsigned long lastDebounceTimeColor = 0;
unsigned long lastDebounceTimeDuration = 0;
unsigned long debounceDelay = 500;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), handleColorBtn, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), handleDurationBtn, FALLING);
Serial.begin(9600);
setColor(i);
Serial.print("Color: ");
Serial.println(colorNames[i]);
Serial.print("Duration: ");
Serial.print(durations[x]);
Serial.println(" ms");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - Millis >= durations[x]) {
Millis = currentMillis;
ledState = !ledState;
if (ledState) {
setColor(i);
} else {
turnOffLED();
}
}
}
void setColor(int index) {
digitalWrite(redPin, colors[index][0]);
digitalWrite(greenPin, colors[index][1]);
digitalWrite(bluePin, colors[index][2]);
}
void turnOffLED() {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
void handleColorBtn() {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTimeColor > debounceDelay) {
lastDebounceTimeColor = currentTime;
changeColor();
}
}
void handleDurationBtn() {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTimeDuration > debounceDelay) {
lastDebounceTimeDuration = currentTime;
changeDuration();
}
}
void changeColor() {
i = (i + 1) % 7;
setColor(i);
Serial.print("Color: ");
Serial.println(colorNames[i]);
}
void changeDuration() {
x = (x + 1) % 5;
Serial.print("Duration: ");
Serial.print(durations[x]);
Serial.println(" ms");
}