Multiple Delay Logic in IDE

Hi - I am trying to create a function that has 3 parts, each part to be completed on timers, 1 second after the other. I have tried to utilise the delay examples found in the forums but they don’t seem to work. The structure needs to be:

Tset 1,1
Tset 2,2
Tset 3,3
Ontimer 1, “firstfunction”
Ontimer 2, “secondfunction”
Ontimer 1, “thirdfunction”

However this doesn’t seem to work - instead of processing each timer one after the other, the order is mixed up. This function would then need to run every 10 seconds but not sure how to implement this either?

Could anyone advise on a simple example so this set of functions can be repeated every 10 seconds in this order?

For reference, this is because we have tag values that require a 1 second delay before changing them.

Thank you, Oliver

Hi Oliver,

The issue here is that TSET/ONTIMER do not set delays, but rather a repeating timer that happens every n seconds. So TSET 1, 5 sets the first timer to run every 5 seconds, and ONTIMER 1, "@firstFn" sets the behavior of the first timer to run the first function, leading to @firstFn being called every 5 seconds.

You can stop a timer by setting its interval to 0: TSET 1, 0

There are a couple of different ways you could approach this. First, you could set a timer that runs every second and use IF statements to determine which block of code is executed at each iteration. In this example, we first set a timer that runs every 10 seconds, at which point it calls RunEverySecond. RunEverySecond sets an index variable to 0, then calls the next section every second. You would place your code at the PRINT statements.

TSET 1, 10
ONTIMER 1, "GOTO RunEverySecond"

RunEverySecond:
  Index% = 0
  TSET 2, 1
  ONTIMER 2, "GOTO TimedForNext"
END

TimedForNext:
  Index% = Index% + 1
  IF Index% = 1 THEN
    PRINT "Routine #1"
  ENDIF
  IF Index% = 2 THEN
    PRINT "Routine #2"
  ENDIF
  IF Index% = 3 THEN
    PRINT "Routine #3"
  ENDIF
  IF Index% >= 3 THEN
    TSET 2, 0
  ENDIF
END

Alternatively, we could use TSET and ONTIMER as a messy sort of delay by setting new timers and stopping them right after they’re used. In this example, we chain three functions together. The first timer is set for 10 seconds, at which point it calls firstFn. firstFn then sets timer 2 to run after one second, calling secondFn when it does. secondFn clears this timer, executes its code, and then does the same thing to call thirdFn.

TSET 1, 10
ONTIMER 1, "@firstFn"

FUNCTION firstFn
  PRINT "Function 1"
  TSET 2, 1
  ONTIMER 2, "@secondFn"
ENDFN

FUNCTION secondFn
  TSET 2, 0
  PRINT "Function 2"
  TSET 2, 1
  ONTIMER 2, "@thirdFn"
ENDFN

FUNCTION thirdFn
  TSET 2, 0
  PRINT "Function 3"
ENDFN

Hi Hugh - that is all great advice thank you. We’ll get this checked and try it out. Thanks again