how to remind the arduino that it performed a specific activity after reboot?

i just finished making my arduino project and its working as expected. its an automatic plant watering system. however in the event of a power failure, the arduino re waters the plant coz it has no memory of the action after rebooting.

one workaround would be to power the arduino with a battery pack instead of a wall wart which is what is the case currently.
there are other ways like pulling the date and time from the internet(wifi/gsm sheild) or using a DS3231 or a humidity sensor or.....

but is there any other way i.e. any modification to the code that can be made to cause the arduino to remember that it has already watered the plant at the stipulated time that i have programmed it for. (AFAIK the millis() counter is reset at boot) so what can be done?

RTCs have battery backed SRAM also, you could store the time there of the last watering, so the Arduino can pull the last time watered there after a restart and thus not be dependent on millis at all, just check the time periodically.

Use EEPROM. You could have it constantly store the last millis() time.

Isaac96:
Use EEPROM. You could have it constantly store the last millis() time.

thanks isaac96, can i store the millis() time every 5 seconds or so directly on to the eeprom that is already on the arduino(its only 1024 bytes though). for example: store millis to variable "a". then after 5 secs, erase and rewrite with the new value.(also, now that i think about it, how does arduino store the compiled sketches in 1024 bytes. this might be a dumb thing to ask....but am kind of a newbie)

Compiled sketches are not stored in EEPROM, they are stored in the 32x1024 bytes of flash.

An EEPROM location is only good for so many writes:

Write/Erase Cycles: 10,000 Flash/100,000 EEPROM

It takes 3.3mS to write to a location.
So how often and how many times are you planning to store to a location?

Battery backed SRAM has no limits.

CrossRoads:
An EEPROM location is only good for so many writes:

Write/Erase Cycles: 10,000 Flash/100,000 EEPROM

It takes 3.3mS to write to a location.
So how often and how many times are you planning to store to a location?

Battery backed SRAM has no limits.

thanks crossroads, did not know that. if i am to use it the way i intend to, the EEPROM will wear out pretty soon. will use the battery powered SRAM.

you said compiled sketches are stored on 32x1024 bytes of flash. and, like you mentioned, that has only 10k write/erase cycles(less than EEPROM). So in practice, it would be possible to use it, but would end up killing the arduino soon. right?

also, assuming the arduino is battery powered, how can it be made to sense, any power failure (am talking about household AC power cut). there must be some very simple way to do this. all that is needed to be done is to check any power outlet in the house. a wall wart can be connected to the outlet and the arduino can check to see if its going dead. any ideas on how to implement this, anyone?

CrossRoads:
It takes 3.3mS to write to a location.
So how often and how many times are you planning to store to a location?

I never knew that. Why does it take so long?

The RTC is battery backed, and has SRAM. Use that.
3.3mS - just the way EEPROM works. See Table 8-2 in the datasheet.

For Arduino to detect loss of AC power, you need some external circuitry.
Simple way is to have a 5V wallwart, and use an analog input to monitor it.
Any USB output cell phone charger would do.

It should be noted that it will be highly unlikely for you to reach the EEPROM's write-cycle limitation - mainly because you need to realize that the numbers from Atmel's datasheets are very conservative. You are more likely to not see an issue (then again, you might get unlucky and have problems much sooner).

People have tried to find out what the real limits are for the Arduino:

The best things to do - if you want the maximum life:

  1. Avoid writing to the same memory location every time - find a way to vary this.

  2. Write your data, and to another location write a checksum value for the data; when you read the data, re-compute the checksum and compare it to the stored checksum. If they don't match, go into an error mode to alert you of the fault (or log the error and continue as best as possible).

Basically, item 1 will act as a "wear leveling" function, while item 2 will act as a way to detect when the limit is reached for a memory location (you could also write the value and the checksum, then immediately read the value, recompute the checksum, and if it fails, mark that memory location as "bad" in a lookup table and store your value(s) elsewhere).

If you handle things right, the worst case will be that you have to replace the ATMega328 (cheap to do so) - provided you have coded things properly to handle such error conditions and failure modalities - otherwise, your plants could suffer as well. You should design your system such that, in the worst case scenario (ie - the one you have already described) - there is some way to determine that the plant(s) has already been watered - and skip them or something.

Another option might be to incorporate an SD Card into the project - and use that to store a log of actions, measurements, etc - then, if the power goes out, read the last entry in the log and go from there. Such cards typically have a greater lifespan, plus you'll only be writing to them once (and given the size of today's SD cards, you'll die before you run out of space, most likely).

You might also figure out a way to measure that a plant or plants have already been watered recently - perhaps with a soil moisture probe, or a strain gauge to measure the weight of the plant (and from there infer if/when you need to water as the water evaporates).

CrossRoads:
For Arduino to detect loss of AC power, you need some external circuitry.
Simple way is to have a 5V wallwart, and use an analog input to monitor it.
Any USB output cell phone charger would do.

thanks crossroads

cr0sh:
It should be noted that it will be highly unlikely for you to reach the EEPROM's write-cycle limitation - mainly because you need to realize that the numbers from Atmel's datasheets are very conservative. You are more likely to not see an issue (then again, you might get unlucky and have problems much sooner).

People have tried to find out what the real limits are for the Arduino:

Destroying An Arduino’s EEPROM | Hackaday

» Arduino misconceptions 5: you’ll wear out the flash memory

The best things to do - if you want the maximum life:

  1. Avoid writing to the same memory location every time - find a way to vary this.

  2. Write your data, and to another location write a checksum value for the data; when you read the data, re-compute the checksum and compare it to the stored checksum. If they don't match, go into an error mode to alert you of the fault (or log the error and continue as best as possible).

Another option might be to incorporate an SD Card into the project - and use that to store a log of actions, measurements, etc - then, if the power goes out, read the last entry in the log and go from there. Such cards typically have a greater lifespan, plus you'll only be writing to them once (and given the size of today's SD cards, you'll die before you run out of space, most likely).

You might also figure out a way to measure that a plant or plants have already been watered recently - perhaps with a soil moisture probe, or a strain gauge to measure the weight of the plant (and from there infer if/when you need to water as the water evaporates).

thanks cr0sh, very handy tips. while using the arduino's EEPROM is not reliable over long time periods(more concerned about the health of my plants than the arduino wearing out), it could still come in very handy at times when there's no other alternative and a quick patch work is desired for a short duration of time.