#define colorBtn 6
#define durationBtn 7
bool colors[][3] = {
{1,1,1} , {1,0,0},{0,1,0},{0,0,1}, // wrgb
{0,1,1} , {1,1,0},{1,0,1},{0,0,0} // cymk
};
char* colorNames[] = {
"white","red","green","blue",
"cyan","yellow","megenta","black" // black is just off so do not need to toggle to black
};
int delays[] = {
1000, 800, 600, 400, 200
};
const int redPin = 13;
const int greenPin = 12;
const int bluePin = 11;
volatile int currentColor = 0;
volatile int currentDelay = 0;
volatile bool colorUpdate = false;
volatile bool delayUpdate = false;
unsigned long lastBlink = 0;
unsigned long lastColorPress = 0;
unsigned long lastDurationPress = 0;
bool ledOn = false;
void setColor(bool r, bool g, bool b) {
digitalWrite(redPin, r);
digitalWrite(greenPin, g);
digitalWrite(bluePin, b);
}
void changeColor() {
unsigned long now = millis();
if (now - lastColorPress > 200) {
currentColor++;
if (currentColor >= 7)
currentColor = 0;
colorUpdate = true;
lastColorPress = now;
}
}
void changeDuration() {
unsigned long now = millis();
if (now - lastDurationPress > 200) {
currentDelay++;
if (currentDelay >= 5)
currentDelay = 0;
delayUpdate = true;
lastDurationPress = now;
}
}
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), changeColor, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), changeDuration, FALLING);
Serial.println("Color: white");
Serial.println("Duration: 1000 ms");
}
void loop() {
if (colorUpdate) {
colorUpdate = false;
Serial.print("Color: ");
Serial.println(colorNames[currentColor]);
}
if (delayUpdate) {
delayUpdate = false;
Serial.print("Duration: ");
Serial.print(delays[currentDelay]);
Serial.println(" ms");
}
if (millis() - lastBlink >= (unsigned long)delays[currentDelay]) {
lastBlink = millis();
ledOn = !ledOn;
if (ledOn) {
setColor(
colors[currentColor][0],
colors[currentColor][1],
colors[currentColor][2]
);
} else {
setColor(0, 0, 0);
}
}
}