How to write values to EEPROM on hourly basis?

Hi all,

I am working on a code that will measure the water level by CapacitiveSensor library and calibrate the min and max levels sensed continuously. After comparing the current level measured with min and max levels, a pump is triggered to top up the tank. All this is working fine. The problem is that as soon as the power is disconnected the calibrated values are lost and the entire setup doesn't know how full the tank is as min and max values are used to map these vague values to more understandable 0-100%.

I was hoping to write the calibrated min and max levels to EEPROM on daily basis, this will help in case of power loss. I understand that i can't use the write function in the main loop as this will write to it continuously and i would run out of write limit of 100k soon.

I have checked online and on the forums and although there are a lot of examples and libraries mentioned they either deal with time span of milliseconds or seconds or are proper time libraries or include RTC with hourly interrupts.

Can someone please advise how can i write to EEPROM on hourly basis from outside the main void loop() may b as some sort of hourly interrupt? (without any external hardware)

Below is a part of the code im talking about

level = round(level);
	
	if (level > level_Max) {			// Calibrate for maximum HIGH
	level_Max = level_rounded;
	}

	if (level < level_Min) {			// Calibrate for maximum LOW
	level_Min = level_rounded;
	}
		
	level_final = map(level, level_Min, level_Max, 0, 100);	// the level is mapped to a scale of 0-100%
	level_final = constrain(level_final, 0, 100);	// in case the sensor value is outside the range seen during calibration

any help/suggestions are appreciated..

Thanks

Can someone please advise how can i write to EEPROM on hourly basis from outside the main void loop() may b as some sort of hourly interrupt? (without any external hardware)

Have a look at the blink without delay example in the IDE.
Remember that there are 3600 seconds in an hour, and 1000 milliseconds in a second.

If you don't want to use external RTC hardware you can use Circadian clock for Arduino. It sets its time by observing dusk and dawn. Needs photoresistor.

http://fgh.si/post/32338612783/circadian-clock-for-arduino

AWOL:

Can someone please advise how can i write to EEPROM on hourly basis from outside the main void loop() may b as some sort of hourly interrupt? (without any external hardware)

Have a look at the blink without delay example in the IDE.
Remember that there are 3600 seconds in an hour, and 1000 milliseconds in a second.

Thanks for the reply, and yes i did. THe problem is that it will delay the entire loop n not just the writing part to EEPROM. I need the loop to be active all the time n only stop once in 24 hrs to make a note of min & max levels. Thanks though

omersiar:
If you don't want to use external RTC hardware you can use Circadian clock for Arduino. It sets its time by observing dusk and dawn. Needs photoresistor.

http://fgh.si/post/32338612783/circadian-clock-for-arduino

Thats a real nice idea, but unfortunately the entire setup is indoors, so that might not be possible.

Still looking for a way to code it in to trigger a write function every 24 hours. Accuracy is not important +/- one hour would do as well, i just need it to be fairly periodic.

Get an RTC and use the TimeAlarms library.... easy-peasy

http://playground.arduino.cc/code/time

THe problem is that it will delay the entire loop

The great thing about blink without delay is it doesn't delay.
There's a clue in the name.

If you're worried about the tiny amount of additional time taken to write values to the EEPROM, that can be mitigated too.

AWOL:

THe problem is that it will delay the entire loop

The great thing about blink without delay is it doesn't delay.
There's a clue in the name.

If you're worried about the tiny amount of additional time taken to write values to the EEPROM, that can be mitigated too.

Yes, i apologise, I misread and assumed that you're talking about the Blink example. This makes sense and seems it would work, only one concern , if i am not wrong, millis has a limit upto around 49.7 days! After it resets, it should still work fine right? It will still follow that same pattern of checking the interval of time elapsed and trigger the write function periodically?

And no i am not concerned about the time taken to write to EEPROM

You are not wrong - millis() will flip-over after 49 days.
This is not a problem for properly-written software.

AWOL:
You are not wrong - millis() will flip-over after 49 days.
This is not a problem for properly-written software.

All right thanks AWOL!! XD
I should be able to take it from here on....

Thanks everyone for your inputs as well :slight_smile: