Page 1 of 2

Timing Problems

Posted: Wed Feb 15, 2023 8:45 pm
by ctesla75
Hi All,
I am making a program to turn my fish tank light on for a certain amount of time when pressing a button. I have most of the program working except timing. By playing around with values I have accurate minute timing, but if I use the same calculations and multiply by 60 for hours it only runs for minutes, and if i add minutes and hours together for a total time ,it is even quicker. I tried using a user macro running a loop but that stops the scrolling message. Should I use interupts instead, I had a quick look but it only counts to 256.
Thanks in advance for any help
Jason

Re: Timing Problems

Posted: Wed Feb 15, 2023 9:47 pm
by kersing
I can’t look at the code on my phone, but could it be you are calling a macro with a number that is too large? For instance I think the delay macro only accepts numbers between zero and 65535 and anything larger will be changed to the remainder of the delay dividend by 65536. So a 65537 second delay would just last one second.

Re: Timing Problems

Posted: Wed Feb 15, 2023 10:28 pm
by alanwms
Interrupt is better - Simply have the interrupt setup for a specific timer based time, and each time the interrupt is called, increment your variable. In the main loop, simply look for a variable number larger than the time you need, and then set that variable to zero, and turn off your output.
Your interrupt will be hit on a time based on your clock frequency. The interrupt time in Hertz is shown in the interrupt setup box.

Hope that helps.

Re: Timing Problems

Posted: Wed Feb 15, 2023 11:35 pm
by medelec35
Hello.
Take a look at this post as it is intended for the same target device you are using.

Re: Timing Problems

Posted: Thu Feb 16, 2023 8:08 pm
by ctesla75
Thanks for the help everybody, I think the problem lies with what kersing pointed out my numbers were too big ,thats why my hours delay ran for seconds.I have no real experience with interupts, so i will make a program using interupts to turn on a led and see how it goes

Re: Timing Problems

Posted: Thu Feb 16, 2023 9:04 pm
by kersing
To solve the numbers being to large you can use a loop to call delay repeatedly. If you have a minute spot on you can just loop the number of minutes you need.

Re: Timing Problems

Posted: Fri Feb 17, 2023 1:17 am
by alanwms
kersing wrote:
Thu Feb 16, 2023 9:04 pm
To solve the numbers being to large you can use a loop to call delay repeatedly. If you have a minute spot on you can just loop the number of minutes you need.
But then don't use interrupts because a delay action will mess up the stack and the program will crash.

Re: Timing Problems

Posted: Fri Feb 17, 2023 10:47 am
by kersing
Rule of thumb: never use delays in interrupts. Interrupt routines should return as soon as possible. Only if you really know what you are doing and have plenty of experience you can deviate from the rule.

Re: Timing Problems

Posted: Sat Feb 18, 2023 8:12 am
by ctesla75
Hi All ,
Thanks for the replies , I've had no luck yet but have just started trying, hopefully get more time to try now. I have a question about delays and interupts as both kersing and alanwms have said not to use them together . does that include the delays already in my program, as there are many.
I tried using the accurate minute delay in a macro, but my scrolling displays stops until the time is reached

Re: Timing Problems

Posted: Sat Feb 18, 2023 8:19 am
by mnfisher
Delays are fine in your code - just not in the interrupt handlers...

If you add a delay to your code (say delay 1minute) - your code will 'stop' for this time (on the esp32 the OS will do 'other things' - execute other tasks etc) - interrupts will continue to occur at the specified intervals. In fact on the esp32 - you are expected to 'yield' to other tasks either by using delays or certain OS calls to allow other tasks to run and if you don't then you will get the watchdog timer kicking in.

An alternative way to handle this would be to have a separate task handle the scrolling display. See viewtopic.php?f=3&t=793&hilit=Task for a simple example



Martin