Hello,
I'm working with Nodemcu and I need to save some values on the memory. I'm using EEPROM library and sometimes I observe that EEPROM.read() is reading a wrong value, but just for uint8_t variables.
I can't say exactly when it happens because it happens sometimes.
I have been looking and the return type of read function is a uint8_t.
This could be a problem with uint8_t or maybe I'm doing something wrong? What is the best type of variable to read and write just one byte with EEPROM library?
I'm using VS Code and PlatformIO.
Thanks for any tips.
are you storing uint8_t's?
What is the best type of variable to read and write just one byte with EEPROM library?
int8_t's or uint8_t's
Yes, I'm storing uint8_t's.
What values are you putting in and what values are you getting out that are in error?
Posting your properly formatted code, in code tags, would be great,
The user selects the values on the display, the values can be any value between 1 and 250.
The reading value is different at each time the error occurs.
Here is the code to read values:
#define INIT_MEMORY 0
#define SETPOINT INIT_MEMORY + 4
#define TEMPO_FAN SETPOINT + 4
#define FLAG_ON TEMPO_FAN + 1
#define TEMPO_VALV FLAG_ON + 1
#define TEMPO_CHAMA TEMPO_VALV + 1
uint8_t tempo_fan = 10;
uint8_t tempo_valv = 2;
uint8_t tempo_chama = 5;
float setpoint = 25.0;
bool flag_On = false;
setpoint = EEPROM_readFloat(SETPOINT);
tempo_fan = EEPROM.read(TEMPO_FAN);
flag_On = EEPROM.read(FLAG_ON);
tempo_valv = EEPROM.read(TEMPO_VALV);
tempo_chama = EEPROM.read(TEMPO_CHAMA);
And the code to save values:
EEPROM_writeFloat(SETPOINT, setpoint);
EPROM.write(TEMPO_FAN, tempo_fan);
EEPROM.write(FLAG_ON, flag_On);
EEPROM.write(TEMPO_VALV, tempo_valv);
EEPROM.write(TEMPO_CHAMA, tempo_chama);
EEPROM.commit();
And the functions to read and write a float:
void EEPROM_writeFloat(int ee, float value)
{
byte* p = (byte*)(void*)&value;
for (size_t i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
}
double EEPROM_readFloat(int ee)
{
float value = 0.0;
byte* p = (byte*)(void*)&value;
for (size_t i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return value;
}
I solved the problem.
When I was flashing NodeMcu out of my circuit it was acting like pressing the buttons and changing the values that are saved in the memory.
So the memory is ok.
Thanks @Idahowalker