I'm using the Arduino's built-in EEPROM to store certain parameters across resets. However, after each programming reflash the EEPROM is erased (which is the proper behavior, IMO).
However, instead of detecting 0xFF in the EEPROM storage at runtime, I'm hoping there's a way to specify default values to be flashed to EEPROM at compile time so that I don't have to test for 0xFF at runtime. If this is possible, how should I specify that in my source code?
The EEPROM should not be erased. Which Arduino are you using? Which programmer?
IF it is being erased during programming, then nothing you can do at compile time can change that, since programming happens after compiling.
A better way to do this is to reserve one EEPROM area to store the software version. Then the code needs to check this version before reading any values from the EEPROM. If you change your software and re-organize the EEPROM then this allows you to overwrite the old values in the EEPROM with the compile-time defaults.
#define ADDR_VERSION 0 //location of the software version in EEPROM
#define CURRENT_EEPROM_VERSION 3 //we are on revision 3 of the EEPROM storage structure
//(0xFF or 255 is never a valid value for version)
#define ADDR_MYVAR 1 //location of MyVar in EEPROM
byte MyVar = 42; //note this default value will be used if EEPROM isn't programmed
void setup() {
if(EEPROM.read(ADDR_VERSION) != CURRENT_EEPROM_VERSION) {
//EEprom is wrong version or was not programmed
EEPROM.write(ADDR_MYVAR, MyVar); //write the default to the EEprom
} else {
MyVar = EEPROM.read(ADDR_MYVAR);
}
}
not tested, should be pretty much alike the following:
#include <avr/pgm_space.h>
const char MY_STR_FLASH[] PROGMEM = {"peter and annette love each other.};
#include <avr/eeprom.h>
const char MY_STR_EEPROM[] EEPROM = {"peter and annette love each other.};
Note that you may have to write the eeprom data-file explicitly.
derVernichter:
not tested, should be pretty much alike the following:
#include <avr/pgm_space.h>
const char MY_STR_FLASH[] PROGMEM = {"peter and annette love each other.}; #include <avr/eeprom.h>
const char MY_STR_EEPROM[] EEPROM = {"peter and annette love each other.};
Note that you may have to write the eeprom data-file explicitly.
Th EEPROM sentence will only work with ISP programming, The Current Arduino bootloader does not support EEPROM uploads.
Look into this lib Arduino Playground - EEPROM-Erom, if you wish, it has data verification built in. It auto sets your values to default if data is missing/corrupt.