Timing Problems

For general Flowcode discussion that does not belong in the other sections.
ctesla75
Posts: 23
http://meble-kuchenne.info.pl
Joined: Tue Nov 22, 2022 10:56 am
Been thanked: 2 times

Timing Problems

Post 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
Attachments
fishy.fcfx
(73.09 KiB) Downloaded 38 times

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Timing Problems

Post 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.

alanwms
Posts: 117
Joined: Fri Dec 04, 2020 2:29 pm
Has thanked: 24 times
Been thanked: 7 times

Re: Timing Problems

Post 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.

medelec35
Matrix Staff
Posts: 1451
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 512 times
Been thanked: 472 times

Re: Timing Problems

Post by medelec35 »

Hello.
Take a look at this post as it is intended for the same target device you are using.
Martin

ctesla75
Posts: 23
Joined: Tue Nov 22, 2022 10:56 am
Been thanked: 2 times

Re: Timing Problems

Post 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

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Timing Problems

Post 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.

alanwms
Posts: 117
Joined: Fri Dec 04, 2020 2:29 pm
Has thanked: 24 times
Been thanked: 7 times

Re: Timing Problems

Post 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.

kersing
Valued Contributor
Posts: 157
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 64 times
Been thanked: 58 times

Re: Timing Problems

Post 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.

ctesla75
Posts: 23
Joined: Tue Nov 22, 2022 10:56 am
Been thanked: 2 times

Re: Timing Problems

Post 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

mnfisher
Valued Contributor
Posts: 955
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 508 times

Re: Timing Problems

Post 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

Post Reply