About Sleep and Prog Mem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

About Sleep and Prog Mem

Postby HooctAwnFonix » Fri Mar 09, 2012 7:43 pm

My question in short is: when Arduino wakes up, does it lose program memory? Does it simply resume loop or does it start from the top, recreate global objects and variables, and run the setup loop again?

The reason is, I'm working on a project where I allow users to input preferences and I save those to EEPROM so that they'll be kept when powered down. I'm writing myself an EEPROM helper library so that I can write strings with one function and retrieve data in the form of a boolean or an int for an example. I can either grab the data from EEPROM each time I need it, or I can store it in a global variable which I think will be a lot more efficient, unless the Arduino starting anew each time it wakes up from sleep...

Thoughts, suggestions?
HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 10:47 am
Location: Cambridge, MA

Re: About Sleep and Prog Mem

Postby adafruit_support_bill » Fri Mar 09, 2012 7:49 pm

RAM is lost, but flash and EEPROM are not. On each power-up or reset, the Arduino will run the last-downloaded sketch, starting with the setup() function.
User avatar
adafruit_support_bill
 
Posts: 16635
Joined: Sat Feb 07, 2009 9:11 am

Re: About Sleep and Prog Mem

Postby HooctAwnFonix » Fri Mar 09, 2012 8:01 pm

Right - I am using EEPROM for this reason, since if Arduino is unplugged I want preferences to stay.

But while plugged in, it will go to sleep and wake up many times, and I am wondering if RAM goes away then as well? I am basically wondering whether I should store EEPROM data in RAM while I loop (which is only run once before going to sleep) or whether I should just go straight to EEPROM each loop.
HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 10:47 am
Location: Cambridge, MA

Re: About Sleep and Prog Mem

Postby adafruit_support_bill » Sat Mar 10, 2012 6:13 am

There are various sleep modes with various levels of power savings. I've only used SLEEP_MODE_IDLE, and I believe that it preserves RAM.
User avatar
adafruit_support_bill
 
Posts: 16635
Joined: Sat Feb 07, 2009 9:11 am

Re: About Sleep and Prog Mem

Postby HooctAwnFonix » Wed Mar 28, 2012 8:33 am

Does accessing EEPROM use more energy?

I'm hesitating about whether or not it's worth trying to store a constantly used variable in prog mem at all
HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 10:47 am
Location: Cambridge, MA


Re: About Sleep and Prog Mem

Postby HooctAwnFonix » Wed Mar 28, 2012 8:56 am

You are saying that because of the limited amount of reads and writes?
HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 10:47 am
Location: Cambridge, MA

Re: About Sleep and Prog Mem

Postby adafruit_support_bill » Wed Mar 28, 2012 9:23 am

You are saying that because of the limited amount of reads and writes?

Only writes are limited. But read through the article I linked. Accessing EEPROM is slow.
User avatar
adafruit_support_bill
 
Posts: 16635
Joined: Sat Feb 07, 2009 9:11 am

Re: About Sleep and Prog Mem

Postby JamesC4S » Sat Mar 31, 2012 11:03 am

HooctAwnFonix wrote:I'm hesitating about whether or not it's worth trying to store a constantly used variable in prog mem at all

You keep mixing up the memory spaces. There are three on the Arudino (ATmega).
1. RAM - Program read-write (2k)
2. EEPROM - Program read-write (1k)
3. PROGMEM (aka FLASH) - Program read-only (32k)
(Values given for ATmega328)

It isn't possible for you (or your program) to write variables in "PROGMEM".
User avatar
JamesC4S
 
Posts: 114
Joined: Sun Sep 26, 2010 10:49 pm
Location: Austin, TX

Re: About Sleep and Prog Mem

Postby philba » Sat Mar 31, 2012 11:49 am

JamesC4S wrote:...
It isn't possible for you (or your program) to write variables in "PROGMEM".

Well, in an absolute sense you can write to flash memory, it's what boot loaders do but only on a block basis. However, for a newbie, I would go by what James says. Save parametric data to EEPROM, it will persist. In fact, very few programs ever mess with flash memory.

So, here is how a typical program will use EEPROM:
  • at startup (think setup(){}), load parameters into variables (RAM) from EEPROM
  • if parameters are uninitialized, set to some default value and store back to EEPROM
  • when parameters are changed, save back to EEPROM
You might ask, how do I know if EEPROM was never set (uninitialized)? EEPROM is a -1 if it's never been written to. Use that. You could create a variable that you load out of EEPROM. If it's -1, it means you never set EEPROM. If you write the params to EEPROM, set the variable to 0.
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 5:59 pm

Re: About Sleep and Prog Mem

Postby JamesC4S » Sat Mar 31, 2012 11:57 am

philba wrote:
JamesC4S wrote:...
It isn't possible for you (or your program) to write variables in "PROGMEM".

Well, in an absolute sense you can write to flash memory, it's what boot loaders do but only on a block basis.


This is why I used the term "program". Code in the boot loader partition has the ability to write to PROGMEM. Code running out of PROGMEM, does not.
User avatar
JamesC4S
 
Posts: 114
Joined: Sun Sep 26, 2010 10:49 pm
Location: Austin, TX

Re: About Sleep and Prog Mem

Postby HooctAwnFonix » Sat Mar 31, 2012 4:02 pm

Oh sorry - I did indeed think that RAM was called PROG MEM! I'm new to this so thanks for the semantic check ;)
HooctAwnFonix
 
Posts: 77
Joined: Tue Sep 13, 2011 10:47 am
Location: Cambridge, MA


Return to Arduino

Who is online

Users browsing this forum: adafruit_support_bill, ArekKindAcere, mibignistinly and 12 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [114]

Raspberry Pi[82]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
Android[6]
 
BeagleBone[23]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]