I have a project in which I need to store a setting value in EEPROM. Just one value-- and it's an integer. I've tried to follow the patterns of the Arduino Examples libraries for EEPROMUpdate and EEPROMGet, but the results I'm getting don't make sense.
I'm using an Arduino Nano Every. Here's my setup() code with some preamble:
#define DEBUGGING_MESSAGES_ENABLED
#ifdef DEBUGGING_MESSAGES_ENABLED
#define DebugMsg(x) do { Serial.print(x); } while (0)
#define DebugMsgLn(x) do { Serial.println(x); } while (0)
#else
#define DebugMsg(x)
#define DebugMsgLn(x)
#endif
void setLEDColor(const byte rgb[]) {
analogWrite(ledRed, rgb[P_RED]);
analogWrite(ledGreen, rgb[P_GREEN]);
analogWrite(ledBlue, rgb[P_BLUE]);
}
int normalizeRelayTimerMinutes(int timerMinutes) {
static const int minRelayTimerMinutes = 1;
static const int maxRelayTimerMinutes = 20;
timerMinutes = max(timerMinutes, minRelayTimerMinutes);
timerMinutes = min(timerMinutes, maxRelayTimerMinutes);
return timerMinutes;
}
void setup() {
#ifdef DEBUGGING_MESSAGES_ENABLED
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// and delay an extra second, because Serial port isn't *really* connected correctly yet...
delay(1000);
#endif
DebugMsgLn("PAT setup(): Initializing...");
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
digitalWrite(modeButton, LOW);
pinMode(modeButton, INPUT);
digitalWrite(tripButton, LOW);
pinMode(tripButton, INPUT);
digitalWrite(remoteButton, LOW);
pinMode(remoteButton, INPUT);
digitalWrite(relayControl, HIGH); // relay is active-low; pullup to default as OFF
pinMode(relayControl, OUTPUT);
setLEDColor(color[RGB_WHITE]);
delay(1000);
static const int eeAddr = 10;
int relayTimerValue = -42; // something absurd, just to prove EEPROM.get() has effect
EEPROM.get(eeAddr, relayTimerValue);
DebugMsg("PAT setup(): relayTimerValue = "); DebugMsgLn(relayTimerValue);
int relayTimerNormalValue = normalizeRelayTimerMinutes(relayTimerValue);
if (relayTimerValue != relayTimerNormalValue) {
DebugMsg("PAT setup(): non-normal value of relayTimerValue found in EEPROM: ");
DebugMsg(relayTimerValue);
DebugMsg("; NORMALIZED value: "); DebugMsgLn(relayTimerNormalValue);
relayTimerValue = relayTimerNormalValue;
EEPROM.update(eeAddr, relayTimerValue);
}
relayTimerMs = relayTimerValue * minToMs;
DebugMsg("PAT setup(): relayTimerMs = "); DebugMsg(relayTimerMs); DebugMsgLn("ms");
delay(1000);
setLEDColor(color[RGB_OFF]);
DebugMsgLn("PAT is READY.");
}
And here's the output on the Serial Monitor, generated by downloading the program with a new value for eeAddr, letting it run, then hitting reset after it completes, then doing that once again...
19:47:49.791 -> PAT setup(): Initializing...
19:47:50.771 -> PAT setup(): relayTimerValue = -1
19:47:50.806 -> PAT setup(): non-normal value of relayTimerValue found in EEPROM: -1; NORMALIZED value: 1
19:47:50.912 -> PAT setup(): relayTimerMs = 60000ms
19:47:51.888 -> PAT is READY.
19:48:11.894 -> PAT setup(): Initializing...
19:48:12.866 -> PAT setup(): relayTimerValue = -255
19:48:12.935 -> PAT setup(): non-normal value of relayTimerValue found in EEPROM: -255; NORMALIZED value: 1
19:48:13.010 -> PAT setup(): relayTimerMs = 60000ms
19:48:13.990 -> PAT is READY.
19:48:24.492 -> PAT setup(): Initializing...
19:48:25.488 -> PAT setup(): relayTimerValue = -255
19:48:25.522 -> PAT setup(): non-normal value of relayTimerValue found in EEPROM: -255; NORMALIZED value: 1
19:48:25.628 -> PAT setup(): relayTimerMs = 60000ms
19:48:26.604 -> PAT is READY.
I'm a bit lost... The value that is read seems to be -1 the first time (which I can see might make sense, if EEPROM is factory-initialized to 0xFFFFFFFFFFF....). But I change it to +1, and then STORE it. On reboot, I read it back as -255... Is there some data size issue at play here? I dont' get it....
Grateful for any hints...