Is There a Way to Save Variables?

I'm making a GardenBot (gardenbot.org), but writing my own code for it XD. I have min/max values for the sensor readings, but when I power down/reset the Arduino, the values are lost :astonished:! Is there a way to save variables to the internal memory of the ATMega 328 chip? Thanks!

Sure, use your on chip EEPROM memory.

http://arduino.cc/en/Reference/EEPROM

Lefty

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1281456125#2

Might do what you want, or check out the EEPROM library (that the above library uses).

retrolefty: Sure, use your on chip EEPROM memory.

http://arduino.cc/en/Reference/EEPROM

Lefty

Thanks! I didn't even know that page existed. Do I have to start at, for example, the last address to make sure I don't overwrite any of the bootloader or code?

EEPROM memory is not program memory; you can't overwrite the bootloader. Be aware that the EEPROM has limited erase/write cycles,so only write to it when necessary.

058606:

retrolefty: Sure, use your on chip EEPROM memory.

http://arduino.cc/en/Reference/EEPROM

Lefty

Thanks! I didn't even know that page existed. Do I have to start at, for example, the last address to make sure I don't overwrite any of the bootloader or code?

No, EEPROM memory is it's own unique memory with it's own address, nothing to do with internal flash (program) or SRAM (data) memories. Different AVR chips have different size EEPROM, but if you just need to store a way a handle full of variables they all are large enough. You do have to keep track yourself what eeprom addresses you used and how large the variables are. The basic commands just write or read one byte at a time and a int is 2 bytes long, so you you have to manage where and how many bytes to read for you use.

Lefty

Groove: EEPROM memory is not program memory; you can't overwrite the bootloader. Be aware that the EEPROM has limited erase/write cycles,so only write to it when necessary.

Thanks. How often do you think I should write/overwrite? If I did not add any more delays in the program, it would write/overwrite about every 3 seconds. However, I could code it so it would only write/overwrite if the value changed...

retrolefty:

058606:

retrolefty: Sure, use your on chip EEPROM memory.

http://arduino.cc/en/Reference/EEPROM

Lefty

Thanks! I didn't even know that page existed. Do I have to start at, for example, the last address to make sure I don't overwrite any of the bootloader or code?

No, EEPROM memory is it's own unique memory with it's own address, nothing to do with internal flash (program) or SRAM (data) memories. Different AVR chips have different size EEPROM, but if you just need to store a way a handle full of variables they all are large enough. You do have to keep track yourself what eeprom addresses you used and how large the variables are. The basic commands just write or read one byte at a time and a int is 2 bytes long, so you you have to manage where and how many bytes to read for you use.

Lefty

Will you please give me an example of splitting an integer into two bytes?

P.S. The values I'm saving only go from 0 to 100.

However, I could code it so it would only write/overwrite if the value changed...

That's the way to do it. I think the erase/write limit is something like 1 million (front page of the datasheet), which may sound a lot, but 20 times a minute would soon use it up.

Groove:

However, I could code it so it would only write/overwrite if the value changed...

That's the way to do it. I think the erase/write limit is something like 1 million (front page of the datasheet), which may sound a lot, but 20 times a minute would soon use it up.

Ok, it would DEFINATELY write a lot less than 20 times a minute, as the sensors are (sun)light (using a photocell) and soil moisture (using two straightened paper clips) (I've had a non/saving script running for about an hour, and the variables haven't changed much). I'm going to post a thread about my project soon!

One more little question: Is there a way to clear the Serial communication window? I have the Arduino posting values every 3 seconds, and I don't like having a gigantic list.

Another approach is to look at the low-power modes, where the processor is stopped, but the SRAM contents are maintained.

Ok, now I know how to save variables, but Lefty says that they use two addresses. Do I have to split the variable up, or just give it enough space to "overflow" into the next address? Also, how would I "put the variable back together" after reading it from the two addresses?

Have a look for EEPROMWriteAnything.

Groove: Have a look for EEPROMWriteAnything.

Thanks! Reading that made me realize that my numbers will only need to use one address!

058606: Ok, now I know how to save variables, but Lefty says that they use two addresses. Do I have to split the variable up, or just give it enough space to "overflow" into the next address? Also, how would I "put the variable back together" after reading it from the two addresses?

Yes, if you only use the basic read and write eeprom commands you have to break up your larger (then byte size) variables and write them into consective eeprom addresses, int take two addresses, long takes four addresses, etc.

EEPROMWriteAnything is a more complex library that handles that for you but is a little more work to get setup. However the EEPROMWriteAnything example code doesn't compile without errors in IDE version 22. There was a posting yesterday about how to fix it by putting the EEPROMWriteAnything structure into a tabbed window on the IDE, look for it.

Lefty

Another possibility is a SD memory chip. There are adapters available that let you plug one in and bring pins out for connecting to the SPI ports. Then you can save 2 Gb of data into normal disk files. Then just read them into your spreadsheet. Of course, you need to do a little work formatting it when writing. There is a "SdFat" library that does most of the work.

In fact I'm sure I saw a project recently where someone just soldered wires onto a micro-to-normal adapter (the sort that come with micro-sized SD cards). You would need to get the pins right, but basically an SD card is (amongst other things) a processor that supports SPI. So you solder a few wires to the adapter, connect them to your SPI pins, and then slip in a micro-SD card. Collect data for a week or a month, remove the card and plug it into your PC. Job done.

The adapter card I bought has a 14050BG hex buffer chip on it. Not sure how essential that is for reliable operation.