Using reset button as a power switch EEPROM

With power down mode, a reset button and a byte in the EEPROM, I am able to use the reset button as a power switch, but is there a way, not to write to the EEPROM for every reset? Although, it has a lifespan of 100,000 write cycles, and probably not a problem here, still I want to try to avoid writing to the EEPROM as much as I could. Last but not least, is the 100,000 cycles included all 512 Bytes in the Atmega8, ie, one byte can be written 100,000 times, and since it has 512 bytes, 51,200,000 times?

#include <avr/sleep.h>
#include <EEPROM.h>
byte digitalSwitch;

//###### Start Up Code
void setup() {

digitalSwitch=EEPROM.read(0);  

if (digitalSwitch==0){
  digitalSwitch=!digitalSwitch;   
  EEPROM.write(0, digitalSwitch);  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
  sleep_enable();     
  sleep_mode(); 
  }

else{
  digitalSwitch=!digitalSwitch;   
  EEPROM.write(0, digitalSwitch);   
  } 
}

//###### Main Program
void loop() {  
  DDRD=0xff;
  PORTD=0xff;
  delay(500);
  DDRD=0;
  delay(500);
}

Well you don’t have a hard power off - while you sleep memory is not lost. The fact you use the reset button is the pb if you don’t want to use the EEPROM. If you had any other button connected to an interrupt you could do the same and wake up without any EEPROM use at all and get back the exact context were you where and still have a usable reset button with its original intent…

J-M-L:
Well you don’t have a hard power off - while you sleep memory is not lost. The fact you use the reset button is the pb if you don’t want to use the EEPROM. If you had any other button connected to an interrupt you could do the same and wake up without any EEPROM use at all and get back the exact context were you where and still have a usable reset button with its original intent…

You are absolutely right, but I am kinda paralyzed now, ie, not able to wake-up from sleep with int0. Reset is kinda my easy way to do things.

Here is the code, I have been working on this for more than 5 hours.

#include <avr/sleep.h>

byte digitalSwitch=0;

//###### Start Up Code
void setup() {
  
DDRD &= ~(1 << PD2); 
PORTD |= (1 << PD2); 
GICR |= (1 << INT0);     
}



//###### Main Program
void loop() { 
  digitalWrite (SCK, 1);
  delay(500);
  digitalWrite (SCK, 0);
  delay(500);
    
}


ISR(INT0_vect){     
digitalSwitch=!digitalSwitch;   
PORTD ^= (1 << PD5); 

set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
cli();

if(digitalSwitch==1){
digitalWrite (SCK, 0); 
sleep_enable();
sei();     
sleep_mode(); 
}

else{   
digitalWrite (SCK, 1);  
sleep_disable();
  }
  
sei();

  }

so you have

set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
cli();

if(digitalSwitch==1){
digitalWrite (SCK, 0); 
sleep_enable();
sei();     
sleep_mode(); 
}

The sleep_mode() automatically sets the sleep enable bit, goes to sleep, and clears the sleep enable bit. the sleep_mode() macro might cause race conditions in some situations.

try with this

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  cli();
  if (digitalSwitch)
  {
    digitalWrite (SCK, 0); 
    sleep_enable();
    sei();
    sleep_cpu();
    sleep_disable();
  }
  sei();

This sequence ensures an atomic test of digitalSwitch with interrupts being disabled. If the condition is met, sleep mode will be prepared, and the SLEEP instruction will be scheduled immediately after an SEI instruction. As the intruction right after the SEI is guaranteed to be executed before an interrupt could trigger, it is sure the device will really be put to sleep. (your challenge might come from the bouncing of your button otherwise)