Hi,
I use the program below to set 2 values to eeprom. I then upload my main program which reads these values and if needed saves new values. This is done to preserve values between resets.
I am uploading onto a pro mini. Sometimes when I upload the below program, then upload my main code I get an out of sync error, but the main program uploads but doesn't work properly. If I then upload A small sketch just to read those two values from eeprom, I find they are both set to zero.
I thought stuff in eeprom would stay there. Does this sound like a hardware issue, or is there something wrong with code? It seems to only happen if I get the out of sync error while uploading.
Cheers.
#include "Arduino.h"
#include <EEPROMex.h>
int addressInt;
int addressByte;
void setup() {
Serial.begin(115200);
int TrackNo = 1;
EEPROM.writeInt(addressInt,TrackNo);
TrackNo = EEPROM.readInt(addressInt);
Serial.println(TrackNo);
bool state = false;
EEPROM.write(addressByte,state);
state = EEPROM.read(addressByte);
Serial.println(state);
}
void loop() {
}
addressInt and addressByte are both 0. So writing state to the eeprom overwrites part of the TrackNo that is stored in the eeprom.
If you want TrackNo at locations 0 and 1 of the eeprom
int addressInt = 0; // this just makes clear that the address is at 0
and state after that
int addressByte = 2; // located after TrackNo in eeprom
PS
Use decent names for the addresses, e.g. addressTrackNo and addressState.
There is no need to use the EEPROMex library when using an up to date version of the IDE as the supplied EEPROM library now has put() and get() functions that allow any type of data to be saved and retrieved
sterretje:
addressInt and addressByte are both 0. So writing state to the eeprom overwrites part of the TrackNo that is stored in the eeprom.
If you want TrackNo at locations 0 and 1 of the eeprom
int addressInt = 0; // this just makes clear that the address is at 0
and state after that
int addressByte = 2; // located after TrackNo in eeprom
PS
Use decent names for the addresses, e.g. addressTrackNo and addressState.
Thanks. So is it a byte per digit? Say my track number was 100 would the addressByte have to be 3?
UKHeliBob:
There is no need to use the EEPROMex library when using an up to date version of the IDE as the supplied EEPROM library now has put() and get() functions that allow any type of data to be saved and retrieved
Thanks, I didn't realise that. I'll look into it later, but I'll stick with what I know at the min :).
Cheers
Seedler:
Thanks. So is it a byte per digit?
It is a byte per byte. int variables (on an arduino) are two bytes long. They hold a value between 32767 and -32768 which, if you work it out, is 2^16 possible values.
PaulMurrayCbr:
It is a byte per byte. int variables (on an arduino) are two bytes long. They hold a value between 32767 and -32768 which, if you work it out, is 2^16 possible values.
Cool, so address 0 and 1 will be used for the TrackNo, and address 2 for state? That's just like you said :).
Cheers