// Forum: https://forum.arduino.cc/t/rgbw-pattern-problem/1247866
// This Wokwi project: https://wokwi.com/projects/395245481110779905
// Previous Wokwi project: https://wokwi.com/projects/395236442837506049
//
// Version 1: 15 April 2024
// Version 2: 15 April 2024
// changed > to >= in millis timer.
// table is now called: colorCombinationTable
// colorMode -> colorIndex
// added that the EEPROM is used when the red reset button is pressed.
//
// Warning: It is now specifically for a Uno board.
// The EEPROM does not keep its contents in Wokwi.
// -------------------------------------------
// Glue for the Uno
#define PA13 13
#define PA4 4
#define PA5 9
#define PA6 6
#define PA7 5
#define PB1 3
// -------------------------------------------
#include <EEPROM.h>
#include <Bounce2.h>
const int RGBWpin[4] = {PA6, PA5, PA7, PB1};
const int ledPin = PA13;
const int buttonPin = PA4;
unsigned long durationNewColor = 5000UL;
unsigned long previousMillisTimeout;
unsigned long durationBackToNormal = 15000UL;
bool setColorMode = false;
int colorIndex;
const int defaultColorIndex = 1;
Bounce button = Bounce();
// Eleven different led values, RGBW
const int colorCombinationTable[11][4] =
{
{ 0, 0, 0, 0}, // off, not used
{ 255, 0, 0, 0}, // color 1
{ 0, 255, 0, 0}, // color 2
{ 0, 0, 255, 0}, // color 3
{ 0, 0, 0, 255}, // color 4
{ 150, 150, 0, 0}, // color 5
{ 150, 0, 150, 0}, // color 6
{ 0, 150, 150, 0}, // color 7
{ 100, 255, 50, 0}, // color 8
{ 20, 120, 200, 0}, // color 9
{ 180, 60, 30, 0}, // color 10
};
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("---- RGBW pattern sketch ----");
pinMode(ledPin, OUTPUT);
colorIndex = EEPROM.read(0);
Serial.print("colorIndex read = ");
Serial.println(colorIndex);
if(colorIndex < 1 or colorIndex > 10)
{
// It is not valid, fall back to default value.
colorIndex = defaultColorIndex;
Serial.print("colorIndex changed to ");
Serial.println(colorIndex);
}
button.attach(buttonPin, INPUT_PULLUP);
button.interval(20); // debounce interval in ms
// Set the values
setRGBW(colorIndex);
}
void loop()
{
button.update(); // keep the Bounce2 library going
if(!setColorMode)
{
// Is the button pressed for a long time ?
if(button.read() == LOW && button.currentDuration() >= durationNewColor)
{
// Switch to the mode to set the color.
digitalWrite(ledPin,HIGH);
setColorMode = true;
previousMillisTimeout = millis();
}
}
else
{
// Set a new color mode
if(button.fell())
{
// The button was pressed, next color
colorIndex++;
if(colorIndex > 10)
colorIndex = 1;
EEPROM.write(0, colorIndex);
Serial.print("(saved:");
Serial.print(colorIndex);
Serial.print("),");
// Update the new value to the leds
setRGBW(colorIndex);
// Remember the time that a button was last pressed.
previousMillisTimeout = millis();
}
else
{
// No button pressed for a very long time ?
// Then go back to normal.
if(millis() - previousMillisTimeout >= durationBackToNormal)
{
digitalWrite(ledPin,LOW);
setColorMode = false;
}
}
}
}
void setRGBW(int value)
{
Serial.print(value);
Serial.print(",");
for(int i = 0; i < 4; i++)
{
analogWrite(RGBWpin[i], colorCombinationTable[value][i]);
}
}
Press the red reset button
on the board,to restart.
Press green button for more than 5 seconds to change color.