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;
}
}