Measure the time usage of a device using current sensor SCT 013 030

Good day, I'm having a project to measure the time usage of a device. I use the current sensor SCT 013 030 and node MCU to detect when the device is working. The reading from the sensor is sent to Thingspeak and more importance is I want to build the timer to measure how long the device has been working.

At first, I use the millis () to get the time difference when the device is switched on and off. I also used interrupt, because my idea was that when the device started working, the interrupt would be executed and the millis () would start.

However I find my mistake here, because the millis () begins when the the Arduino board began running the current program.

Could anyone please help or give me idea how to measure the time usage of a device. If any part of this coding can be improved, please guide me. And please excuse my bad English. Thanks a lot for your time.

You can use a variable for comparison.

Save the millis () value, then subtract the current value.

You should know that the millis () counter is reset periodically, you may want to use a 1 second timer to generate interruption, and a counting variable may be sufficient.

However I find my mistake here, because the millis () begins when the the Arduino board began running the current program.

why don't you reset (set to zero) your counter and millis() value whenever there is insufficient current, but first check if the counter is non-zero and send your message to thingspeak

rtek1000:
You can use a variable for comparison.

Save the millis () value, then subtract the current value.

You should know that the millis () counter is reset periodically, you may want to use a 1 second timer to generate interruption, and a counting variable may be sufficient.

ya, i use “previous” variable to save the millis ()value : previous = millis ();
when the device switched off, i use “now” variable : now = millis ();
then i find the time usage by take now - previous.

my concern is that if the millis () starts to count when the device starts working, or when the arduino board begins running

gcjr: why don't you reset (set to zero) your counter and millis() value whenever there is insufficient current, but first check if the counter is non-zero and send your message to thingspeak

gcjr: could you pls guide me how to reset millis ()

DANGKN:

gcjr: could you pls guide me how to reset millis ()

The quick answer to “How do you reset millis()” is: You Don’t! And here’s why: if you did, it would potentially break most libraries and functions that rely on it. Generally the reason people want to reset it, is that they are concerned about rollover. Instead of focusing on resetting millis(), here is how to use it correctly.

https://www.baldengineer.com/arduino-how-do-you-reset-millis.html