good afternoon everyone, i need some advise please i want to control my geyser to have it on for a specific amount of time every day, (quite a simple task) however in south Africa we have power outages several times a week and although the code works it does not store the last on state, is there any way to do this with out EEPROM as i fear it will use up all its writes rather quickly
#include <EEPROM.h>
#include <avr/wdt.h>
#include<Wire.h>
#include <DS3231.h>
const int OnHour = 16;
const int OnMin = 01;
const int OffHour = 20;
const int OffMin = 30;
int geyser = 7;
DS3231 rtc(SDA, SCL);
Time ti;
void setup() {
Serial.begin(9600);
wdt_enable(WDTO_8S);
rtc.begin();
pinMode (geyser, OUTPUT);
}
void loop () {
Serial.print(rtc.getTimeStr());
wdt_reset();
static int currentMinute = -1;
ti = rtc.getTime();
if (ti.min != currentMinute)
{
currentMinute = ti.min;
Serial.print(ti.hour);
Serial.print(" hour(s), ");
Serial.print(ti.min);
Serial.println(" minute(s)");
}
if (ti.hour == OnHour && ti.min == OnMin)
{ digitalWrite(geyser, HIGH);
Serial.println("GEYSER ON");
}
else if (ti.hour == OffHour && ti.min == OffMin) {
digitalWrite(geyser, LOW);
Serial.print("GEYSER OFF");
}
delay (1000);
}
To store the last on state you'll need a way to detect that power has been lost, then have enough power stored to save the last known state before the power really runs out. Could you show the circuit that you have to detect loss of power ? It may be that the circuit that you have to detect loss of power is not sending a signal to indicate a loss of power has occured.
I think that you can get away with using the EEPROM. If you write the date and time once only when you turn the geyser on, you will only be writing a few times a day I would expect so the chip will outlast the plumbing by many years.
Note though that currently, you're turning the system on repeatedly. That's not ok for the EEPROM.
if you are still concerned though, take a look at a FRAM.
You should get a minimum of 100,000 writes to the EEPROM.
Another possibility is to upgrade your ds3231 RTC to a ds3232. This model has a small amount of ram memory which is backed up the coin cell, like the clock time is. Because it is ram memory rather than eeprom, you have unlimited writes.
You can play around with where the data goes in the EEPROM too. Use the first byte to tell you where to write the timestamp and change it periodically (monthly?)
With 136 years to play with though, it's not really worth adding the complexity.
Correct. The main danger is if you don't write your code correctly, it could perform a large number of writes in a short time. If it performed 100 writes per second, and you did not realise that was happening, the eeprom would last only a few minutes.
Actually, I'm not sure you care about detecting the power outage. At startup, you can check when the geyser was last heated and for how long, assuming you store on & off time & date in EEPROM.
Then you can decide whether there's a need to heat off-cycle to make up for heating that should have occurred but didn't.
Hi Guys, sorry for the late reply, i will definitely play around with this, i have almost zero experience with eeprom but i am sure i will come right with it thanks for the advice