Jump to content

Multiple timers calling a function?


Recommended Posts

Hello, I'm having a problem with a function which uses a timer for an input. I've got an application that uses multiple timers which all do relatively similar things. Basically, I have a display which shows the timers progression, so each timer needs to pass it's own preset and current values to the common variables I linked to the meter in the screen. Note that only one timer is active at a time, and the next timer starts when the previous timer ends, so I only need the one meter. I created all of the timers with the correct preset times, and I created a function with a timer input so that in my code I can call this function, give it the correct timer object, and have the function run the timer AND update the display without needing to repeat a bunch of code. But there seems to be something wrong with accessing timers this way. Everything works the first time around, but on subsequent passes, the timers only run for their preset time minus the previous timers preset time. So if I have four timers set to 5, 10, 12, and 6 seconds respectively, after the first round, the timers will run for 5, 5, 2, and 0 seconds respectively. The last timer doesn't even run! I've tried every which way to fix this, but nothing has worked. So before I have to go and add all the repeated elements back in to dozens of timers, I figured I'd ask here to see if anyone has a solution.

One other interesting tidbit which might help figure this out, when I view the values from inside the offending function, I can see that the "preset" value is correct, which tells me the timer is being passed correctly. I can also see that the "current" value is correct for the timer I want to run (it starts off at the preset value until I call the function). But this is where it gets interesting. As soon as I call the function, the "current" value for the active timer drops to the wrong number. I'm not directly setting it anywhere, which tells me I may have stumbled into a bug. If the timers are not meant to be passed like this, then why else would there be the option to pass in a timer?

I've attached a sample program to demonstrate the problem. In it, I've included two versions of the setup, "Test - Not Working" which is the same as described above, and "Test - Working" which is exactly the same, except instead of calling a function, all of the blocks from the function are repeated in each rung. You can select which method to use with the checkbox on the main screen. Just push the "Run Sequence" button, and whichever function is selected will run.

Timer Test.ulpr

Link to comment
Share on other sites

  • MVP 2023

Haven't looked at your program.

I often say on the forum that people are far too focussed on "Timers".   Counts often perform  much better, by simply using compares on a rolling count that increases, and finally resets/restarts, by whatever means you decide is applicable.  From what you say, with only one timer active at a time, you would likely be far better off using this method.

Counts are the forgotten simple tech, Timers can tie you in all sorts of knots if you aren't careful.  Counts let you see what is going on very easily, interacting timers not as much.  Blah Blah.

cheers, Aus

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...