#define colorBtn 6
#define durationBtn 7
#define RedLED 13
#define GreenLED 12
#define BlueLED 11
#define MAXCOLORS 7
#define DELAYCOUNT 5
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 ledDelays[] = {
1000, 800, 600, 400, 200
};
unsigned long debounceDelay = 50;
int8_t ledDelayIdx=0;
int8_t ledColorIdx=0;
void setup() {
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
pinMode(colorBtn, INPUT_PULLUP);
pinMode(durationBtn, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(colorBtn), colorBtnPressHandler, CHANGE);
attachInterrupt(digitalPinToInterrupt(durationBtn), durationBtnPressHandler, CHANGE);
Serial.begin(9600);
}
volatile bool cbtnPinState = HIGH;
unsigned long cbtnlastDebounceTime = 0;
void colorBtnPressHandler(){
// read the button pin current state
bool currentBtnPinState = digitalRead(colorBtn);
// debounce button
if (currentBtnPinState != cbtnPinState) // works when button state is different
{
if (millis() - cbtnlastDebounceTime > debounceDelay) // debouce delay
{
cbtnPinState = currentBtnPinState; // update button state
// works only on key up
if(!cbtnPinState)
{
ledColorIdx++;
ledColorIdx%=MAXCOLORS;
// update serial monitor
Serial.print("Color: ");
Serial.println(colorNames[ledColorIdx]);
}
cbtnlastDebounceTime = millis(); // log the debounce timestamp
}
}
}
volatile bool dbtnPinState = HIGH;
unsigned long dbtnlastDebounceTime = 0;
void durationBtnPressHandler(){
// read the button pin current state
bool currentBtnPinState = digitalRead(durationBtn);
// debounce button
if (currentBtnPinState != dbtnPinState) // works when button state is different
{
if (millis() - dbtnlastDebounceTime > debounceDelay) // debouce delay
{
dbtnPinState = currentBtnPinState; // update button state
// works only on key up
if(!dbtnPinState)
{
ledDelayIdx++;
ledDelayIdx%=DELAYCOUNT;
// update serial monitor
Serial.print("Duration: ");
Serial.print(ledDelays[ledDelayIdx]);
Serial.println("ms");
}
dbtnlastDebounceTime = millis(); // log the debounce timestamp
}
}
}
void loop() {
digitalWrite(RedLED, colors[ledColorIdx][0]);
digitalWrite(GreenLED, colors[ledColorIdx][1]);
digitalWrite(BlueLED, colors[ledColorIdx][2]);
delay(ledDelays[ledDelayIdx]);
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, LOW);
digitalWrite(BlueLED, LOW);
delay(ledDelays[ledDelayIdx]);
delay(10);//ledDelays[ledDelayIdx]);
}