#define colorBtn 6
#define durationBtn 7
// Define the colors in the RGB format
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
};
// Define the color names
char* colorNames[] = {
"white", "blue", "green", "red",
"yellow", "cyan", "magenta", "black"
};
// Define the blinking durations
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; // debounce delay in milliseconds
unsigned long previousMillis = 0;
bool ledState = LOW;
void setup() {
Serial.begin(9600); // Initialize Serial communication
pinMode(11, OUTPUT); // Red
pinMode(12, OUTPUT); // Green
pinMode(13, OUTPUT); // Blue
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), changeColor, RISING);
attachInterrupt(digitalPinToInterrupt(durationBtn), changeDuration, RISING);
// Print initial state
printState();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delays[currentDelay]) {
// Save the last time you toggled the LED
previousMillis = currentMillis;
// If the LED is off turn it on and vice-versa
ledState = !ledState;
// Set the LEDs to the current color
digitalWrite(11, ledState && colors[currentColor][0]);
digitalWrite(12, ledState && colors[currentColor][1]);
digitalWrite(13, ledState && colors[currentColor][2]);
}
delay(10);
}
// Interrupt service routine for changing color
void changeColor() {
unsigned long currentMillis = millis();
if (currentMillis - lastColorPress >= debounceDelay) {
currentColor = (currentColor + 1) % 8; // Cycle through colors including black
lastColorPress = currentMillis;
printState();
}
}
// Interrupt service routine for changing duration
void changeDuration() {
unsigned long currentMillis = millis();
if (currentMillis - lastDurationPress >= debounceDelay) {
currentDelay = (currentDelay + 1) % 5; // Cycle through delays
lastDurationPress = currentMillis;
printState();
}
}
// Function to print the current state
void printState() {
Serial.print("Current Color: ");
Serial.println(colorNames[currentColor]);
Serial.print("Current Delay: ");
Serial.print(delays[currentDelay]);
Serial.println(" ms");
}