/*
Project: RGB LED with arrays
Description: Cycles through 8 color combinations
Creation date: 10/22/23
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
// edit the line below to match your LED type
// don't forget to rewire the common pin to match
const bool COMMON_CATHODE = false; // false = common anode
const uint8_t BTN_PIN = 13;
const uint8_t RGB_LED_PINS[] = {11, 10, 9};
const uint8_t LED_COLOR[][3] = {
{0, 0, 0}, // black (off)
{0, 0, 1}, // blue
{0, 1, 0}, // green
{0, 1, 1}, // green/blue
{1, 0, 0}, // red
{1, 0, 1}, // purple
{1, 1, 0}, // yellow
{1, 1, 1} // white
};
const char COLOR_NAMES[][11] = {
{"Off"},
{"Blue"},
{"Green"},
{"Blue-green"},
{"Red"},
{"Purple"},
{"Yellow"},
{"White"}
};
void lightRGB(int color) {
for (int j = 0; j < 3; j++) {
if (COMMON_CATHODE) {
digitalWrite(RGB_LED_PINS[j], LED_COLOR[color][j]);
} else {
digitalWrite(RGB_LED_PINS[j], !LED_COLOR[color][j]);
}
}
}
bool checkButton() {
static bool oldBtnState = true; // INPUT_PULLUP idles high
bool isButtonPressed = false;
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
isButtonPressed = true;
//Serial.println("Button pressed");
} else {
//Serial.println("Button released");
}
delay(20); // for debounce
}
return isButtonPressed;
}
void setup() {
Serial.begin(9600);
pinMode(BTN_PIN, INPUT_PULLUP);
for (int i = 0; i < 3; i++) {
pinMode(RGB_LED_PINS[i], OUTPUT);
}
Serial.print("Color: ");
Serial.println(COLOR_NAMES[0]);
}
void loop() {
static int mode = 0;
int btnPress = checkButton();
if (btnPress) {
mode++;
if (mode > 7) mode = 0;
btnPress = false;
Serial.print("Color: ");
Serial.println(COLOR_NAMES[mode]);
}
lightRGB(mode);
}