Start millis after the microcontroller starts

Hi, I want to start a second counter when a pin is high and stop the counter when the pin is low. Is this possible to do with millis() function?

Hello hareendra

Take a view into the BlinkWithOutDelay example provided by the IDE to gain the knowledge.

Have a nice day and enjoy coding in C++.

1 Like

When do you want to stop the counter - when the pin goes HIGH?

Yes, it is possible.

when the pin goes LOW and then reset it

Hi paul, thankyou so much ill look into that. thanks!

Hi, I did a bit of research but couldn't find a proper solution. Do you know how this could be done?

so basically I need to count how many seconds a pin was HIGH for.

Read & save milllis() in a variable when it goes high.

Read & save again in another variable when it goes low.

Subtract.

...You may need another "flag" variable like "TimerRrunning" with an if statement.

If the Arduino runs continuously for 40-something days millis() will roll-over and subtraction could give a negative value so you'll have to deal with that.

ohh yes, got it. thankyou will do. previously I was trying with nodemcu built-in functions tmr.create, but didn't work properly. should have thought a bit simpler.

It seem to be a typical xy-problem again

You can use pulseIn() function.

1 Like

Hi Golam, thankyou so much so the reply! Yes, I will check on this as well.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.