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
Timing Problems
-
- Posts: 23
- http://meble-kuchenne.info.pl
- Joined: Tue Nov 22, 2022 10:56 am
- Been thanked: 2 times
-
- Valued Contributor
- Posts: 192
- Joined: Wed Dec 02, 2020 7:28 pm
- Has thanked: 77 times
- Been thanked: 64 times
Re: Timing Problems
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
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.
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
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
But then don't use interrupts because a delay action will mess up the stack and the program will crash.
-
- Valued Contributor
- Posts: 192
- Joined: Wed Dec 02, 2020 7:28 pm
- Has thanked: 77 times
- Been thanked: 64 times
Re: Timing Problems
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
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
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
-
- Valued Contributor
- Posts: 1470
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 706 times
Re: Timing Problems
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
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