0

I would like to set a stopwatch timer that will determine how long an input is in a certain state before changing. I want to set it so that, depending on the output my code executes one of 2 switch cases. But my trouble comes in on setting a timer. Is there a function I could use? Or a method that somebody knows? The time that the input will be for each case is not fixed so I cannot use a delay.

  • 2
    As I suggested in the comments to your other question, you can also use millis() here to measure time differences (use micros() if the time to measure is smaller than a few milliseconds). – chrisl Jan 20 at 12:10
0

Even though you are not running an actual (complex) Operating System, you should adhere to common practices. For an Arduino, you should, in many cases, avoid directly controlling the hardware so as to be compatible with as many existing libraries for your particular Arduino platform as possible.

Setting the timer directly (if you are using an official Arduino Uno which contains an Atmel328P processor the processor's timers are covered in section 14 of the Atmel328P Specifications) may cause unexpected results should you use a library which expects the timer to run without being altered.

Instead, consider using the millis() function built into the Arduino IDE. The function returns the current number of milliseconds since the Arduino was powered up. Record this value in your code. Then, if you want to know if One Second has elapsed, add 1000 to this number and repeatedly check to see if millis() returns a number which exceeds this value. When it has, One Second has elapsed.

A bit more advanced: Once you have debugged your program, go back and consider what would happen if the value millis() reported started over from zero because it has exceeded the size of the variable type millis() returns. This does not happen very often. And it may not be important to you should your program behave unexpectedly on an infrequent bases. But if it is important, consider added the code to detect this roll over and act accordingly.

  • 2
    Re “consider added the code to detect this roll over”: better to just write rollover-safe code, like if (millis() - start_time >= duration). “check to see if millis() returns a number which exceeds this value” is not rollover-safe. – Edgar Bonet Jan 20 at 14:54
  • True Edgar. @Damon Swart is brand new to the site and I like trying, testing & vetting 1 idea at a time (using millis()) - then, after the 1st idea is working, adding to it (roll-over). Also, I rather not include code. It takes longer to post answers w/code and the code may not be correct or complete. Besides, describing the solution makes the Original Poster think through the problem. – st2000 Jan 21 at 12:50
  • @Damon Swart, if you try the answer and it works for you, accept the answer as correct so that others can also find and use the answer. If it doesn't work for you, post a comment here and we can try to change the answer so that it does work for you. – st2000 Jan 21 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.