Storing Variables to non-volatile memory


I am working on a project that has some power constraints and I am looking to have the program resume where it cut off if the power runs low. When the program (and the robot) execute the provided course, I will be recording the state that the controller is in, via an integer value. When we do run out of power, I would like to be able to have the previous state of the controller saved to non-volatile memory. I have tried to use the progmem declaration as such:

#include <pololu/orangutan.h>
#include <avr/pgmspace.h>

int state PROGMEM;

int main()

		state = 0; // initialize state

but I am getting the following compiler error:

Does anyone have a suggestion on how I can accomplish this? I only want to store about four integer values on the controller (SV-328P). Thanks and have a great day!


You need to write to EEPROM, not flash which is PROGMEM. Assuming you are using WinAVR, take a look at the functions in eeprom.h such as eeprom_write_byte() or eeprom_write_word();

Be aware that you can only write to EEPROM about 100,000 times. Seems like a lot till to pound a memory location in a tight loop!

Hope this helps,

Thanks for the quick reply. This worked like a charm. In addition, I found a good tutorial for anyone else who is interested in utilizing the EEPROM :