#define colorBtn 6
#define durationBtn 7
#define RED_PIN 13
#define GREEN_PIN 12
#define BLUE_PIN 11
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", "magenta", "black" // black is just off so do not need to toggle to black
};
int delays[] = { 1000, 800, 600, 400, 200 };
volatile int currentColor = 0;
volatile int currentDelay = 0;
volatile bool colorBtnPressed = false;
volatile bool durationBtnPressed = false;
unsigned long lastToggleTime = 0;
bool ledState = false;
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), colorBtnISR, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), durationBtnISR, FALLING);
Serial.println("RGB LED Controller initialized");
Serial.println("Color: white");
Serial.println("Duration: 1000 ms");
}
void loop() {
if (colorBtnPressed) {
handleColorChange();
}
if (durationBtnPressed) {
handleDurationChange();
}
toggleLED();
}
void colorBtnISR() {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 200) {
colorBtnPressed = true;
lastInterruptTime = interruptTime;
}
}
void durationBtnISR() {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 200) {
durationBtnPressed = true;
lastInterruptTime = interruptTime;
}
}
void handleColorChange() {
currentColor = (currentColor + 1) % 7; // Exclude black (index 7)
Serial.print("Color: ");
Serial.println(colorNames[currentColor]);
colorBtnPressed = false;
}
void handleDurationChange() {
currentDelay = (currentDelay + 1) % 5;
Serial.print("Duration: ");
Serial.print(delays[currentDelay]);
Serial.println(" ms");
durationBtnPressed = false;
}
void toggleLED() {
unsigned long currentTime = millis();
if (currentTime - lastToggleTime >= delays[currentDelay]) {
ledState = !ledState;
lastToggleTime = currentTime;
if (ledState) {
digitalWrite(RED_PIN, colors[currentColor][0]);
digitalWrite(GREEN_PIN, colors[currentColor][1]);
digitalWrite(BLUE_PIN, colors[currentColor][2]);
} else {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
}