/*
RGB LED with button demo
LED is common cathode!
*/
const int BTN_PIN = 5;
const int RED_LED_PIN = 4;
const int GRN_LED_PIN = 3;
const int BLU_LED_PIN = 2;
const char* MODE_COLORS[] = {
"Red", "Green", "Blue"
};
int mode = 0;
int oldMode = -1;
int oldBtnVal = HIGH; // pullup idles HIGH
void allOff() {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GRN_LED_PIN, LOW);
digitalWrite(BLU_LED_PIN, LOW);
}
void checkButton() {
int btnVal = digitalRead(BTN_PIN);
if (btnVal != oldBtnVal) {
oldBtnVal = btnVal;
if (btnVal == LOW) {
mode++;
if (mode == 3) mode = 0;
}
delay(20);
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(BLU_LED_PIN, OUTPUT);
Serial.println("Push the button!\n");
}
void loop() {
checkButton();
if (mode != oldMode) {
oldMode = mode;
allOff();
Serial.println(MODE_COLORS[mode]);
if (mode == 0) {
digitalWrite(RED_LED_PIN, HIGH);
} else if (mode == 1) {
digitalWrite(GRN_LED_PIN, HIGH);
} else if (mode == 2) {
digitalWrite(BLU_LED_PIN, HIGH);
}
}
}