Recording total time an external device has been powered up?

I am really new at this but have been able to get some of what I need working. My goal is to send an interrupt to start a timer that runs until a second interrupt is received. I want to take that value and carry it forward to the next time it records time so it appears to just keep the total time it has recorded. It would just do this for the life of the device to record the total amount of time an external device has been powered up and present it in hours and minutes- HH:MM. Here is the code I have been working with, it does count when triggered and each time it adds on to the last time. Once I disconnect the serial monitor it starts back at zero and I can only figure out how to display it in seconds or minutes by dividing the milles differently. I need it to continue to increment even if it is rebooted etc and display in hours and minutes- HH:MM if at all possible. I would also like to be able to put it in a deep sleep when it is not recording. If anyone has any direction for me it would be greatly appreciated!

const unsigned int firstPin = 2;
const unsigned int secondPin = 3;
const unsigned int builtInLED = 13;

enum timeManagementStatus { configuringInterrupts, waitingForFirstInterrupt, receivedFirstInterrupt, waitingForSecondInterrupt, receivedSecondInterrupt };

volatile timeManagementStatus myState;

unsigned long int startTime, duration; //would need to be volatile if moved into the interrupt code


void setup() {
  Serial.begin(9600);
  pinMode(firstPin, INPUT_PULLUP);   // when HIGH means no signal, when LOW received interrupt
  pinMode(secondPin, INPUT_PULLUP);  // when HIGH means no signal, when LOW received interrupt

  pinMode(builtInLED, OUTPUT); // the built in LED
  digitalWrite(builtInLED, LOW);

  myState = configuringInterrupts; // this way if an interrupt is triggered within the next two lines, we ignore it
  attachInterrupt(digitalPinToInterrupt(firstPin), firstPinLow, LOW); // triggers on LOW
  attachInterrupt(digitalPinToInterrupt(secondPin), secondPinLow, LOW);
  myState = waitingForFirstInterrupt; // doing this after defining interrupts so we are ready to go 

}

void firstPinLow()
{
  if (myState == waitingForFirstInterrupt)   myState = receivedFirstInterrupt;
}


void secondPinLow()
{
  if (myState == waitingForSecondInterrupt) myState = receivedSecondInterrupt;
}

void loop() {

  if (myState == receivedFirstInterrupt) {
    // As we don't care about sub millisecond precision I chose to keep the interrupt to a minimum
    // but if the loop were to be super long to execute that would be a pb and the 2 next lines
    // could be in the interrupt code if we wanted to be super precise.
    // startTime and duration would need to be volatile if moved into the interrupt code
    startTime = millis();  // could be in the interrupt subroutine for first pin
    myState = waitingForSecondInterrupt; // could be in the interrupt for first pin
    PORTB |= B00100000; // builtInLED (pin 13) to HIGH // keep here, user won't see a 1ms delay
  } else if (myState == receivedSecondInterrupt) {
   // As we don't care about sub millisecond precision I chose to keep the interrupt to a minimum
    // but if the loop were to be super long to execute that would be a pb and the next line
    // could be in the second interrupt code if we wanted to be super precise
    //startTime and duration would need to be volatile if moved into the interrupt code

    duration  = millis() - startTime + duration++; // could be in the interrupt for second pin

    Serial.print("Runtime = "); // keep the display here though. Don't use Serial in interrupts!
    Serial.print( (int)(duration / 1000L));
    Serial.println(" Seconds");
    

    PORTB &= B11011111; // builtInLED (pin 13) to LOW
    myState = waitingForFirstInterrupt;
  }
}

Nice state machine. But why not set startTime=millis() inside the interrupt? Then it doesn't have to wait for some unknown amount of time for the main loop to detect the state-change and note the time.

The second interrupt could even add to the duration accumulator itself. No need for the main loop to do that either.

Then the main loop can print the result once per second or once per minute instead of on every pulse.

To store the value between resets (connecting the Arduino to the Serial Monitor is a reset) then you need to store it on some non-volatile storage. The EEPROM is easiest. How many pulses is this expected to see in its lifetime? Something under 100,000? Then just store the value directly in the EEPROM. If you are expecting more pulses then make the main loop take a 'backup' of the duration into the EEPROM once every 5 minutes or something.

MorganS:
Nice state machine. But why not set startTime=millis() inside the interrupt? Then it doesn't have to wait for some unknown amount of time for the main loop to detect the state-change and note the time.

The second interrupt could even add to the duration accumulator itself. No need for the main loop to do that either.

Then the main loop can print the result once per second or once per minute instead of on every pulse.

To store the value between resets (connecting the Arduino to the Serial Monitor is a reset) then you need to store it on some non-volatile storage. The EEPROM is easiest. How many pulses is this expected to see in its lifetime? Something under 100,000? Then just store the value directly in the EEPROM. If you are expecting more pulses then make the main loop take a 'backup' of the duration into the EEPROM once every 5 minutes or something.

Thank you very much for the input! Putting the startTime in the interrupt does make sense, I will give that a shot today;-) As far as storing it, I have looked at EEPROMwrite and EEPROMread and they seem to make sense so far I just need to play with it a little to make sure I get it. I am not clear on what you mean by pulses? Would that be how many times we introduce the interrupt and initiate timing or how many seconds it actually records total? Also, is there any way to display this in hours and minutes HH:MM instead of seconds?

MorganS- I did move the startTime and it works great:) I also see why you were asking about the pulses with the 100,000 writes so that does make sense, it would never get anywhere close to that! If you have any thoughts on how to present this in hours and minutes please let me know;-)

I used the word "pulses" because you usually use interrupts when you are measuring short pulses. Things which are so short that microseconds matter.

For an hour meter, then you really don't need interrupts at all. But it's a good method to learn so you're ready for your next project.

Got it, thank you. I am making some progress here;-)