TL;DR: please help me figuring out how I can save and load the color setting to/from EEPROM.
Hello, I'm working on my first Arduino/NeoPixel project (with the help of a lot of lending from scripts I found on the internet). Basically a led strip with all leds showing the same color with manual input for color and brightness adjustment.
The color selection is controlled using a rotary encoder knob which cycles through "colorWheel" in the sketch. The brightness is controlled using pushbuttons which make it go up or down in mode/case with a certain brightness setting attached to it.
So far everything is working as it should, however I want it to remember it's last color and brightness setting when the strip is powered off. As soon as power is restored it should resume with the same color and brightness setting.
I finally managed to make this so for the brightness setting: each time I press the brightness buttons it will write the mode to EEPROM (EEPROM.put). When the Arduino is powered on after power loss it will read the EEPROM (EEPROM.get) and display the leds with the last brightness setting.
I'm also trying to do the same for the color setting (which are generated with uint32_t colorWheel), to make the leds light up when powering on with the rgb values from the last selection, but whatever I'm trying (and I've tried a lot), it's not working...
Could anyone help me out with suggestions? Thank you!
#include <Encoder.h>
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
const int NUM_LEDS = 28;
const int LED_PIN = 5;
const int BRIGHTNESS = 255;
const int WHEEL_SIZE = 256;
const int ENCODER_PIN_1 = 2;
const int ENCODER_PIN_2 = 3;
const int BUTTON_1 = 13;
const int BUTTON_2 = 12;
const int BUTTON_3 = 8;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_RGB + NEO_KHZ800);
Encoder encoder(ENCODER_PIN_1, ENCODER_PIN_2);
int mode = 0;
long lastPush = 0;
int autoPosition = 0;
void setup() {
Serial.begin(9600);
pinMode(13, INPUT);
digitalWrite(13, HIGH);
pinMode(12, INPUT);
digitalWrite(12, HIGH);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
strip.begin();
}
long normalize(long value, long radix) {
long rval = value % radix;
if (rval < 0) return radix + rval;
else return rval;
}
void loop()
{
EEPROM.get(0, mode);
if (mode > 9) {
mode = 5;
EEPROM.put(0, 0);
}
int button1 = digitalRead(BUTTON_1);
if (button1 == 0) {
if ((millis() - lastPush) > 250) {
lastPush = millis();
mode++;
if (mode > 9) mode = 9;
EEPROM.put(0, mode); // store the chose effect
}
}
int button2 = digitalRead(BUTTON_2);
if (button2 == 0) {
if ((millis() - lastPush) > 250) {
lastPush = millis();
mode--;
if (mode < 1) mode = 1;
EEPROM.put(0, mode); // store the chose effect
asm volatile (" jmp 0"); // reset the Arduino
}
}
int button3 = digitalRead(BUTTON_3);
if (button3 == 0) {
if ((millis() - lastPush) > 250) {
lastPush = millis();
if (mode < 1) mode = 5;
else mode = 0;
EEPROM.put(0, mode); // store the chose effect
asm volatile (" jmp 0"); // reset the Arduino
}
}
long knobValue = encoder.read() / 2;
long ledPosition = normalize(knobValue, NUM_LEDS);
long colorValue = normalize(knobValue * 5, WHEEL_SIZE);
long sleepValue = abs(knobValue) % 500;
switch (mode) {
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(255);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(200);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 3:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(150);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 4:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(100);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 5: for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(70);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 6:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(50);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 7:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(35);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 8:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(20);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
case 9:
for (int i = 0; i < NUM_LEDS; i++) {
strip.setBrightness(00);
strip.setPixelColor(i, colorWheel(BRIGHTNESS, colorValue));
}
break;
}
strip.show();
}
// given a wheel position in 0-255 range
// return a rainbow color adjusted by intensity 0 to 1.0
uint32_t colorWheel(float intensity, byte wheelPos)
{
const int WHEEL_THIRD = (WHEEL_SIZE - 1) / 3;
if (intensity < 0.0 ) intensity = 0.0;
if (intensity > 1.0) intensity = 1.0;
// as wheelPos progresses from 0 to 255 once, colorIndex should progress from 0 to 255 3 times
// find out position in current third of wheel then multiple by 3 to get full color value
byte colorIndex = (wheelPos % WHEEL_THIRD) * 3;
int fadeColor = (255 - colorIndex) * intensity; // color going down
int increaseColor = colorIndex * intensity; // color going up
switch (wheelPos / WHEEL_THIRD) {
case 0: // first third of the wheel, red fading, no green, blue increasing
return Adafruit_NeoPixel::Color(fadeColor, 0, increaseColor);
EEPROM.put(5, Adafruit_NeoPixel::Color); // store the chose effect
break;
case 1: // second third of the wheel no red, green increasing, blue fading
return Adafruit_NeoPixel::Color(0, increaseColor, fadeColor);
EEPROM.put(5, Adafruit_NeoPixel::Color); // store the chose effect
break;
case 2: // last third of the wheel, red increasing, green fading, no blue
return Adafruit_NeoPixel::Color(increaseColor, fadeColor, 0);
EEPROM.put(5, Adafruit_NeoPixel::Color); // store the chose effect
break;
}
}