Just confirming that values stored in EEPROM dont get wiped at INT declaration

Hi, all. New to Arduino and still reading. Working to design the logic for my first own sketch. I am planning to use EEPROM to store a value noting the direction I have a motor turning. Then when the UNO Is turned on again (after powered off or suffering an unexpected power off) the first thing the sketch will do is setup the H Bridge to continue in the same direction.

I have not been able to find an explicit statement confirming either way but assume that the int declaration wont go near (or wipe) the EEPROM, and then the EEPROM.read will go and get the previously stored value.

Is that correct? Many thanks!

#include <EEPROM.h>

int trkdiraddress = 2;
int trkdirection;

void setup()
{
  Serial.begin(9600);
}


void loop() {
  trkdirection = EEPROM.read(trkdiraddress);
 }

Yes, the int is just storing the number 2, nothing more. It has no interaction with the EEPROM

The EEPROM.read function uses the number stored in trkdiraddress as an offset from the starting position of the EEPROM's memory.

Excellent thanks!

Will you run into problems if you try and store numbers larger than 255?
Int is 16 bit but the EEPROM library works with 8 bit uint8_t it might be safer to define

uint8_t trkdiraddress = 2;
uint8_t trkdirection;

If you do require to write integers to the EEPROM, you can try this:

#include <EEPROM.h>

int trkdiraddress = 2;
int trkdirection;

void setup()
{
  Serial.begin(9600);
}

void loop() {
  trkdirection = readInt(trkdiraddress);
}

int readInt(int address){
#if defined(__AVR_ATmega328__)
  address %= 512; //Wrap around at 512 as there is 1k of EEPROM    
#elif defined (__AVR_ATmega168__)
  address %= 256; //Wrap around at 256 as there is 512 Bytes of EEPROM    
#elif defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  address %= 2048; //Wrap around at 2k as there is 4k of EEPROM    
#endif
  byte tempHigh = EEPROM.read((address << 2));
  byte tempLow = EEPROM.read((address << 2) + 1);
  int reconstruction = (tempHigh << 8) + tempLow;
  return reconstruction;
}

void storeInt(int address, int deconstruction){
#if defined(__AVR_ATmega328__)
  address %= 512; //Wrap around at 512 as there is 1k of EEPROM    
#elif defined (__AVR_ATmega168__)
  address %= 256; //Wrap around at 256 as there is 512 Bytes of EEPROM    
#elif defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  address %= 2048; //Wrap around at 2k as there is 4k of EEPROM    
#endif
  byte tempHigh = (deconstruction >> 8) & 0xFF;
  byte tempLow = deconstruction & 0xFF;
  EEPROM.write((address << 2), tempHigh);
  EEPROM.write((address << 2) + 1, tempLow);
}