function to store the number of hours a digital pin is high

Hello Arduino gurus

I need to write a code in Arduino that counts the time when a specific digital pin goes high, and stop counting when it goes low, and the Arduino must keep the number of hours even when it is switched off, also the number of hours can be rest by triggering a specific digital pin.

Thanks

Seems like a cool project. What does your code looks like?

What kind of resolution and accuracy do you need?

Off the top of my head you have two options: RTC and EEPROM

The EEPROM will involve nothing but the Arduino and the millis() timer. Simply use the internal EEPROM to store whether this is On or off and the length of time. However, you can naively do this everytime you go through loop() since you can wear out the EEPROM. If you can live with knowing the time to the minute, you can update the EEPROM every 60 seconds for a long time. Also, the millis() timer is not very accurate over long periods.

My preferred solution is to get a DS3234 RTC. It has a very accurate clock and can use a battery backup to maintain the time for years. Also it has 256 bytes of battery backed RAM you can use to store your state.

Remember the EEPROM has a limit on its write cycles.

Electronic1Dude: Remember the EEPROM has a limit on its write cycles.

Hence the suggestion to write to it every 60 seconds. It would also be possible to implement some form of wear levelling too to vastly extend the number of writes possible.

Writing to the RTC scratch ram would avoid the issue - the RTC holds its RAM for ten years or so off the button cell. You'd just write the current state of the pin and the time to the RTC RAM whenever it changes, and read back on power up (although you might miss changes while powered down)

You'll need to protect the Arduino pin in question by adding a 10k series resistor or similar to prevent back-powering too.

MarkT:
Writing to the RTC scratch ram would avoid the issue -

You’ll need to protect the Arduino pin in question by adding a 10k series resistor or similar to prevent
back-powering too.

I already said that, though I suppose “storing the state” was not that obvious.

i have been given the following code. but it is not counting the hours when the pin is high, any ideas

[­code]
#include <EEPROM.h>

#define survey_pin 4 // Input pin to survey
unsigned long count_in; // Start time of count
boolean counting = false; // Counting status
byte nb_hours; // Nb hours in integer type : 0 to 255 hours

#define read_pin 5 // Pin to read and/or reset the current count

void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("** HOURS COUNTER **");
pinMode (survey_pin, INPUT);
pinMode (read_pin, INPUT);
}

void loop() {

if (survey_pin && !counting) { // Starting counter if it does’nt run
count_in = millis();
counting = true;
}
else if (!survey_pin && counting) { // Ending counter and storing in EEPROM (non volatile memory)
nb_hours = (millis() - count_in) / 3600000L; // Integer nb_hours ; 0 to 255 hours
EEPROM.write(0, nb_hours); // Write nb_hours in EEPROM adress 0
counting = false;
}

if (read_pin) {
Serial.print("The hours count is : ");
Serial.println(EEPROM.read(0)); // Display the counter value
EEPROM.write(0, 0); // Reset the counter if needed
delay(1000);
}

}
[­/code]

any ideas

Your first idea should be to edit your post and insert [­code] before the code and [­/code] after it to make it easier to read and copy for examination