EEPROM put/set question

Hi,
I'm trying to use EEPROM.set() to write a struct in the rom, but when I use EEPROM.get() and print the data, the value is completely different than what I expect.

here's an example of what I'm trying to do:

struct RGBColor
{
  int r;
  int g;
  int b;  
};

void setup()
{
RGBColor colWrite;
  colWrite.r = 128;
  colWrite.g = 0;
  colWrite.b = 0;

EEPROM.put(0, colWrite);

 RGBColor colRead;
 EEPROM.get(0, colRead);

Serial.println(colRead.r); // outputs 1073741520 instead of 128

}

So like I said, I put a value of 128 in col.r, but the value I get back from a get is 1073741520
What am I doing wrong?

Thanks!

Why variable names messed up?
Just fix it and it will work.

Ah yeah sorry about that, I messed up when I made the example code to be easy to read (now edited).
The actual code is correct and I get the wrong result.

If the actual code is the below one, it gives 128. I've copied the code here before you change the code in the opening post again.

struct RGBColor
{
  int r;
  int g;
  int b;  
};

void setup()
{
RGBColor colWrite;
  colWrite.r = 128;
  colWrite.g = 0;
  colWrite.b = 0;

EEPROM.put(0, colWrite);

 RGBColor colRead;
 EEPROM.get(0, colRead);

Serial.println(colRead.r); // outputs 1073741520 instead of 128

}

Which board are you using?

I made a new sketch to make sure it was not coming from somewhere else in the code, but I still get the wrong value.

I am actually using a NodeMCU is that different on it?

yeah, it was because of the board. Turns out it's working a bit differently on nodeMCU.
I had to modify the code to something like this:

#include <EEPROM.h>

struct RGBColor
{
  int r;
  int g;
  int b;  
};

void setup() 
{
  Serial.begin(115200);
  EEPROM.begin(512);
  
  RGBColor colWrite;
  colWrite.r = 128;
  colWrite.g = 0;
  colWrite.b = 0;

  EEPROM.put(0, colWrite);
  EEPROM.commit(); 
   
  RGBColor colRead;
  EEPROM.get(0, colRead);

  Serial.println(colRead.r); // outputs 1073741520 instead of 128

}

now I get the correct value!
Thanks guys!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.