Using EEPROM

Hi, I've read multiple threads on different websites but I'm still a little stuck with this.

I am writing a program for an alarm, one of the functions allows the user to alter some values, based on the size of their fuel tank/fuel type. (tankw - width, tankl - length, weight - fuel density, warning - low level volume).

I want these values to be saved after setting, even if there is a power interruption, then loaded during the setup during restart.

Here is my function to update the values after the user goes through the function to set there values:

void setTank()
{
EEPROM.update(tankw,tankw); // write (tankw) to nonvolatile memory
EEPROM.update(tankl,tankl); // write (tankl) to nonvalatile memory
EEPROM.update(weight,weight); // write (weight) to nonvalatile memory
EEPROM.update(warning,warning);
}

and here is the code in my setup:

EEPROM.read(warning);
EEPROM.read(tankl);
EEPROM.read(tankw);
EEPROM.read(weight);

it compiles ok, I just don't think I have it right at the moment!

Any help would be appreciated, regards, Shaun

 EEPROM.read(warning);
  EEPROM.read(tankl);
  EEPROM.read(tankw);
  EEPROM.read(weight);

You read the EEPROM but do not store the results.

Lokie so:

value = EEPROM.read(address);

thats what i thought was missing, loading the results to the correct place, thank you!

Would the update command save them OK?

Update is for saving (writing) values. To retrieve values, use read() or get().

shaunh84:
Hi, I've read multiple threads on different websites but I'm still a little stuck with this.

Have you read the EEPROM library documentation?

Why dont you serial print all your saved values so you can see exactly what is being stored and read

shaunh84:
Here is my function to update the values after the user goes through the function to set there values:

void setTank()

{
EEPROM.update(tankw,tankw); // write (tankw) to nonvolatile memory
EEPROM.update(tankl,tankl); // write (tankl) to nonvalatile memory
EEPROM.update(weight,weight); // write (weight) to nonvalatile memory
EEPROM.update(warning,warning);
}

This is not going to do what you expect it to. As is explained in the EEPROM library reference:
https://www.arduino.cc/en/Reference/EEPROMUpdate
the first parameter of EEPROM.update() is the EEPROM address. You need to define a specific address where each configuration is stored. Something like this:

unsigned int tankwAddress = 0;
unsigned int tanklAddress = 1;

... and so on

then:

EEPROM.update(tankwAddress, tankw); // write (tankw) to nonvolatile memory
EEPROM.update(tanklAddress, tankl); // write (tankl) to nonvolatile memory

Here is an example using put() and get() to store and save parameters in a struct. I used int data types in the struct but you can use any data type and even mix data types.

#include <EEPROM.h>

struct Config
{
   int height;
   int width;
   int weight;
   int alarm;
};

Config config;

void setup()
{
   Serial.begin(115200);

   // give elements values
   config.height = 150; 
   config.width = 100;
   config.weight = 40;
   config.alarm = 3;
   
   EEPROM.put(0, config);
   Serial.println("values written\n");
   
   Serial.println("retrieving values");
   EEPROM.get(0, config);
   Serial.print("height = ");
   Serial.print(config.height);
   Serial.print("   width = ");
   Serial.print(config.width);
   Serial.print("   weight = ");
   Serial.print(config.weight);
   Serial.print("   alarm = ");
   Serial.println(config.alarm);
   Serial.println();
}

void loop()
{

}

Thanks for the responses, I got there in the end, and until just now didn't know how to find old posts, and didnt realise there had been extra responses!

I used addresses to update and read from, had a little fun with numbers bigger than 255 but got there!