#define RED_PIN 13
#define GREEN_PIN 12
#define BLUE_PIN 11
#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} //cymk
};
char* colorNames[] = {
"white","red","green","blue",
"cyan","yellow","magenta" // black is just off so do not need to toggle to black
};
int delays[] = {1000, 800, 600, 400, 200};
volatile int colorIndex = 0;
volatile int durationIndex = 0;
unsigned long lastBlinkTime = 0;
bool ledState = false;
volatile unsigned long lastColorInterrupt = 0;
volatile unsigned long lastDurationInterrupt = 0;
const unsigned long debounceDelay = 200;
void changeColor() {
unsigned long currentTime = millis();
if (currentTime - lastColorInterrupt > debounceDelay) {
colorIndex = (colorIndex + 1) % 7;
Serial.print("Color: ");
Serial.println(colorNames[colorIndex]);
lastColorInterrupt = currentTime;
}
}
void changeDuration() {
unsigned long currentTime = millis();
if (currentTime - lastDurationInterrupt > debounceDelay) {
durationIndex = (durationIndex + 1) % 5;
Serial.print("Duration: ");
Serial.print(delays[durationIndex]);
Serial.println(" ms");
lastDurationInterrupt = currentTime;
}
}
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), changeColor, FALLING);
attachInterrupt(digitalPinToInterrupt(durationBtn), changeDuration, FALLING);
Serial.println("System Started");
Serial.println("Color: white");
Serial.println("Duration: 1000 ms");
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - lastBlinkTime >= delays[durationIndex]) {
lastBlinkTime = currentTime;
ledState = !ledState;
if (ledState) {
digitalWrite(RED_PIN, colors[colorIndex][0]);
digitalWrite(GREEN_PIN, colors[colorIndex][1]);
digitalWrite(BLUE_PIN, colors[colorIndex][2]);
} else {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
}