local variables retain their values for as long as the model is loaded regardless of switch condition value
Advanced example (save as /SCRIPTS/FUNCTIONS/cntdwn.lua)
The script below is an example of customized countdown announcements. Note that the init function determines which version of OpenTX is running and sets the unit parameter for playNumber() accordingly.
local lstannouncelocal targetlocal running =falselocal duration =120-- two minute countdownlocal announcements = { 120, 105, 90, 75, 60, 55, 50, 45, 40, 35, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}local annIndexlocal minUnitlocalfunctioninit()local version =getVersion()if version <"2.1" then minUnit =16-- unit for minutes in OpenTX 2.0elseif version <"2.2" then minUnit =23-- unit for minutes in OpenTX 2.1else minUnit =25-- unit for minutes in OpenTX 2.2endendlocalfunctionrun()local timenow =getTime() -- 10ms tick countlocal remaininglocal minuteslocal secondsifnot running then running =true target = timenow + (duration *100) annIndex =1end remaining =math.floor(((target - timenow) /100) +.7) -- +.7 adjust for announcement lagif remaining <0then running =false-- we were 'paused' and missed zeroreturnendwhile remaining < announcements[annIndex] do annIndex = annIndex +1-- catch up in case we were pausedendif remaining == announcements[annIndex] then minutes =math.floor(remaining /60) seconds = remaining %60if minutes >0thenplayNumber(minutes, minUnit, 0)endif seconds >0thenplayNumber(seconds, 0, 0)end annIndex = annIndex +1endif remaining <=0thenplayNumber(0,0,0) running =falseendendreturn { init=init, run=run }