How can I keep time in Arduino by C programming

I want a timer to start if any pin of Arduino goes high and if this time is more than 7 seconds then something will happen by condition.

If that pin is high for more than 7 seconds
-> I'll do something

If that pin is high for less than 7 seconds
-> I'll do something

How can i do this by C programming. Please help me🥺

Look at the sample program that come as part of the IDE and see how they use the function millis(), which returns the current processor time in milliseconds.

Presumably those are two different "somethings"? :slightly_smiling_face:

The example called StateChangeDetect in the IDE will show you how to monitor a pin to see when it changes state, and when it goes high in your case, capture the current value of millis() into a variable. Start doing one of the things. Then use loop() to check to see if the ever-increasing new millis() has gone ahead of that captured value by 7 seconds. When it has, stop that thing and start the other one.

Question... does the thing that happens after 7 seconds ever turn off, like if the pin goes low again, or what?

1 Like

@akib_rahman, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Hello akib_rahman
It seems to be a school assigment, isn´t it?
Post your current sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

Did you want to do this for all 20 pins on an Arduino UNO? Or did you want one specific pin?

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