Coping with power outages

Hi, I am building some relay controls for our heating system and the following code is working fine providing the Nano is running prior to all on/off events. When I reset the the nano as a power outage would, if something was running before the outage I would like to to immediately start up again when the power returns.

I understand the fail-safe approach is to not do that, but we are out in the boonies and power outages are very common and overnight heating is kinda important even after a short or long outage.

Do I have to write and read EEPROM every Loop-cycle or what? Seems to me like great overkill. There seems to be no way to capture the time at the sudden loss of power to be tested on restart. I do not want to have to buy a UPS just for this.

How do people program around this?

void loop() 
{ if (RTC.read(tm)) 
  { int currMin = tm.Minute;
    int tempMins = (tm.Hour * 60) + tm.Minute;

    if (tempMins == officeStart) relayOn(idxOffice);
    if (tempMins >= officeStop && officeRunning) relayOff(idxOffice);

    if (tempMins == loungeStart) relayOn(idxLounge);
    if (tempMins >= loungeStop && loungeRunning) relayOff(idxLounge);

    if (tempMins == bedroomStart) relayOn(idxBedroom);
    if (tempMins >= bedroomStop && bedroomRunning) relayOff(idxBedroom);

    while (currMin == tm.Minute)  // Delay until the next minute
    { RTC.read(tm);
      delay(150);
    }
  } else 
  { if (RTC.chipPresent()) 
    { Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println();
    } else 
    { Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

Do I have to write and read EEPROM every Loop-cycle or what?

You need to what.

You would write to EEPROM only when there is a change that you want to preserve.

You would read from EEPROM in setup() to restore things to the way that they were when the power went out.

Your heating example leaves a bit to be desired, though. It seems to me like you would want to turn the heat on if it was cold, regardless of whether or not the heater had been running when the power went out.

I do not want to have to buy a UPS just for this.

4 AA batteries are not likely to break the bank. Are they?

PaulS: You would write to EEPROM only when there is a change that you want to preserve. You would read from EEPROM in setup() to restore things to the way that they were when the power went out.

I understand all that, but it means a LOT of writes to the EEPROM with a rated life of around 100,000 writes and then it trashes the entire Nano. Not a big cost, but I am from an industrial background where we design for longevity and not just to see something working for a while.

PaulS: 4 AA batteries are not likely to break the bank. Are they?

At the risk of repeating myself. I am from an industrial background where we design for longevity and not just to see something working for a while. Too bad if we are away from the house for a week or two in Winter and the batteries die and we come home to burst frozen water pipes in the house that are now freely gushing water having thawed in the warmth of the day. You need to think BIG-picture, Paul.

Man, how often do you turn on your heater?

if you turn it on 10 times a day, that is still 10,000 days or 27 years.

PS: You only use the batteries when the power is off to power the arduino.

KeithRB: PS: You only use the batteries when the power is off to power the arduino.

And charge them when you have power.

Too bad if we are away from the house for a week or two in Winter and the batteries die and we come home to burst frozen water pipes in the house that are now freely gushing water having thawed in the warmth of the day. You need to think BIG-picture, Paul.

And you need to consider that just maybe 4 AA batteries will power the Arduino while the power is out for more than 2 weeks OR you might consider a larger capacity battery that could power the Arduino for 6 months.

If you put the Arduino into sleep when power is out and remove the power LED you can run an Arduino for a long time with a set of 4 AA's...

But I don't think you need EEPROM or batteries. The problem with the code is (apart form not being all the code...) is it only turns on the heater only at the set time. I think the variable "officeRunning" is set when you call relayOn(idxOffice)? (That's why it's important to post ALL code!)

void loop()
{ if (RTC.read(tm))
  { int currMin = tm.Minute;
    int tempMins = (tm.Hour * 60) + tm.Minute;

    if (tempMins >= officeStop && officeRunning){
      relayOff(idxOffice);
    }
    else if (tempMins => officeStart && !officeRunning){
      relayOn(idxOffice);
    }
    
    if (tempMins >= loungeStop && loungeRunning){
      relayOff(idxLounge);
    }
    else if (tempMins >= loungeStart && !loungeRunning){
      relayOn(idxLounge);
    }
    
    if (tempMins >= bedroomStop && bedroomRunning){
      relayOff(idxBedroom);
    }
    else if (tempMins >= bedroomStart && !bedroomRunning){
      relayOn(idxBedroom);
    }
    
    
    while (currMin == tm.Minute){  // Delay until the next minute
      RTC.read(tm);
    }
  }
  else if (RTC.chipPresent()){
    Serial.println("The DS1307 is stopped.  Please run the SetTime");
    Serial.println();
    delay(1000);
  } 
  else{ 
    Serial.println("DS1307 read error!  Please check the circuitry.");
    Serial.println();
    delay(1000);
  }
}

Did some tidying up as well ;)