losing eeprom value

One of the many little hassles today, is with a sketch where I store any incoming data in eeprom, with the idea of displaying this value after a power outage,

I have whittled it down to this

#include <EEPROM.h>    
int digA ;   

void setup()
  Serial.begin(9600);	// Debugging only
  int digA = EEPROM.read (0);  Serial.print("eeprom 0  =   ");  Serial.println(digA); 
  Serial.print("digA read from eeprom =   ");  Serial.println(digA);
 void loop () {
  showtime ();
  delay ( 10000);
 void showtime () 
  Serial.print (" in showtime digA = ");
  Serial.println (int ( digA)); 

and I already have the value 4 in eeprom (0)

when I run it I get on the serial monitor :-

eeprom 0 = 4
digA read from eeprom = 4
in showtime dig A = 0

What happens to the value when I go into the showtime routine ?

Instead of Serial.println (int ( digA)); try Serial.println (digA);

The error is the variable scoping of two different instances of digA

You have declared it in global scope above the setup function and also again inside the setup

So inside setup your code is using the locally declared copy of digA which is not the same as the variable declared globally

You could fix this by removing the int. from I front of its declaration in setup()

So that you are only using the global instance.

I'm also a bit curious why you don't just read from the EEPROM when you need to get the digA value, its a waste of global variable space, unless you intend to do a lot of high speed processing.

Thanks for the replies guys, you got it there Roger, when I removed the int from in setup, all is well.

I just want to get caught up with my 22 projects, and then read a bit more about all this stuff, ( and even update to V1 :-) ) but at the moment its just juggling balls in the air, but it is great to have you guys for advice.

And I did just think about reading the eeprom for the display, but can't remember why I didnt, - oh yes, I was going to check if reading an eeprom had a life limit, but I dont think so ...

Boffin1: oh yes, I was going to check if reading an eeprom had a life limit, but I dont think so ...

Indeed it does, 100K write/erase cycles.

@Jack Christensen

Just curious, does reading the EEProm shorten its life? I suspect not.

i.e if @Boffin1 did multiple reads, but only occasional writes would it be an issue ?

It wouldnt be an issue in my application anyway, the data will probably only be changed once a month anyway, but I need to read the data after a power failure ( quite a lot here in Africa :-)

rogerClark: Just curious, does reading the EEProm shorten its life? I suspect not.

No, it does not.

Jack & Boffin1 im searching forums about "power failure detection" & "storing data to eeprom" for last 3 days..and i found your threads most helpfull :) http://forum.arduino.cc/index.php?&topic=68944.0 & http://forum.arduino.cc/index.php?topic=116044.0 respectively...

Boffin1 can you please give me schematic for your circuit which keeps (to use your own words) 5v steady for 310mS after power is cut.. (was about to PM both u guys when found this thread)

thanks a lot :)

as far as reading eeprom goes it won't harm eeprom..just one point i like to mention..writing eeprom @ same address (starting 0) may wear out those bytes..keep changing the addresses..by say keeping a counter..

update..links :)