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