While helping someone in their thread, I devised a way to allow setting up initial EEPROM data, however the solution is usable for any one time action like setting up IP addresses, an RTC or other devices with persistent memory. This is a simple solution, however I felt its useful enough to post on the forums.
The method I used will cause the initial code to run once only then subsequent power cycles/resets have no effect.
However this code will automatically cause your initial code to run when you upload a new version of your code.
The added bonus of the code is you get a time of compilation string which you can use to initialize an RTC as the code runs straight after uploading, or maybe run it through a CRC and use it to seed randomSeed; great for debugging when random values do not repeat each change of code.
WARNING: You must place your initial code where the comment specifies. It is placed before a while loop so if a power outage occurs before the initial setup completes, the setup will still run the next time the device is powered.
Also you can add logic weather to run the loop at all ( skip if error ), and it will rerun next reset/power.
#include <avr/eeprom.h>
unsigned char _CompileTimeP[] PROGMEM = __DATE__ " " __TIME__,
_CompileTimeE[ sizeof( _CompileTimeP ) ] EEMEM;
void setup() {
unsigned char b, *e = _CompileTimeE - 1, *p = _CompileTimeP - 1;
while( b = pgm_read_byte( ++p ) ){
if( b != eeprom_read_byte( ++e ) ){
//ADD YOUR EEMEM INITIAL SETUP HERE.
while( b = pgm_read_byte( p++ ) ) eeprom_write_byte( e++, b );
break;
}
}
//NORMAL SETUP HERE
}
void loop() {}
EDIT: left in an variable that is not needed, fixed!