Variables Storage in eeprom

I am fairly new to Arduino programming and working on a project where I'm looking to store the variables in the eeprom so I can recall them after reboots. I'm trying to run it on a elegoo uno r3.

First I am uploading the variable structures via a put sketch (below)

/*
  Used to first set up struct for eeprom storage
*/
#include <EEPROM.h>

//User Changed Variables Structure
struct Stored_Variables {
  String Name;
  int Value;
};

Stored_Variables Number_Input = { "Number Input", 10 };
Stored_Variables Pistol_Default = { "Pistol Default", 26 };
Stored_Variables Rifle_Default = { "Rifle Default", 100 };
Stored_Variables Steps_Per_BB = { "Steps Per BB", 339 };

void setup() {
  // put your setup code here, to run once:
  //  Serial.begin(9600);
  //  Serial.println();

  Serial.begin(9600);

  while (!Serial) {

    ;  // wait for serial port to connect. Needed for native USB port only
  }

  
  int eeAddress = 0;  //Location we want the data to be put.

  EEPROM.put(eeAddress, Number_Input);

  Serial.print("Written Number_Input data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");

  eeAddress += sizeof(Number_Input);  //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, Pistol_Default);

  Serial.print("Written Pistol_Default data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");

  eeAddress += sizeof(Pistol_Default);  //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, Rifle_Default);

  Serial.print("Written Rifle_Default data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");

  eeAddress += sizeof(Rifle_Default);  //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, Steps_Per_BB);

  Serial.print("Written Steps_Per_BB data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
}

void loop() {
  // put your main code here, to run repeatedly:
}

Then when trying to read the values back it shows � for the first 2 character on strings and int values are wrong for 3rd and 4th variable. is there something I may have overlooked

Code for Get sketch below

/***

    eeprom_get example.

    This shows how to use the EEPROM.get() method.

    To pre-set the EEPROM data, run the example sketch eeprom_put.

    This sketch will run without it, however, the values shown

    will be shown from what ever is already on the EEPROM.

    This may cause the serial object to print out a large string

    of garbage if there is no null character inside one of the strings

    loaded.

    Written by Christopher Andrews 2015

    Released under MIT licence.

***/

#include <EEPROM.h>

struct Stored_Variables {
  String Name;
  int Value;
};

void setup() {

  Serial.begin(9600);

  while (!Serial) {

    ;  // wait for serial port to connect. Needed for native USB port only
  }

  int eeAddress = 0;  //EEPROM address to start reading from

  Stored_Variables Number_Input;  //Variable to store custom object read from EEPROM.

  EEPROM.get(eeAddress, Number_Input);

  Serial.println("Read Number_Input from EEPROM: ");

  Serial.println(Number_Input.Name);

  Serial.println(Number_Input.Value);

  eeAddress = sizeof(Number_Input);  //Move address to the next byte after float 'f'.

  Stored_Variables Pistol_Default;  //Variable to store custom object read from EEPROM.

  EEPROM.get(eeAddress, Pistol_Default);

  Serial.println("Read Pistol_Default from EEPROM: ");

  Serial.println(Pistol_Default.Name);

  Serial.println(Pistol_Default.Value);

  eeAddress = sizeof(Pistol_Default);  //Move address to the next byte after float 'f'.

  Stored_Variables Rifle_Default;  //Variable to store custom object read from EEPROM.

  EEPROM.get(eeAddress, Rifle_Default);

  Serial.println("Read Rifle_Default.Name from EEPROM: ");

  Serial.println(Rifle_Default.Name);

  Serial.println(Rifle_Default.Value);

  eeAddress = sizeof(Rifle_Default);  //Move address to the next byte after float 'f'.

  Stored_Variables Steps_Per_BB;  //Variable to store custom object read from EEPROM.

  EEPROM.get(eeAddress, Steps_Per_BB);

  Serial.println("Read Steps_Per_BB from EEPROM: ");

  Serial.println(Steps_Per_BB.Name);

  Serial.println(Steps_Per_BB.Value);
}


void loop() {

  /* Empty loop */
}

Serial monitor output

�mber Input

10

Read Pistol_Default from EEPROM:

�stol Default

26

Read Rifle_Default.Name from EEPROM:

�stol Default

26

Read Steps_Per_BB from EEPROM:

�stol Default

26

The String class uses dynamically-allocated heap memory to store the actual characters. You can't save a String class variable to EEPROM this way. Make the struct entry a char array.

Thanks for your help changing String straight to char solved the problem

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