Task Timer

Code tips and tricks for beginners
Post Reply
peridot
Posts: 46
Joined: Mon Mar 08, 2021 4:54 am
Has thanked: 7 times
Been thanked: 93 times

Task Timer

Post by peridot »

I wrote this bit of code that I have found very useful, it triggers various routines or tasks based on a timer array refreshed by a 1 sec tick.
I have shown a few examples including one for "Run Once, On Startup" and also a simple "@ midnight" trigger.

Code: [Local Link Removed for Guests]

'
' ' *******************************
'    Date: Nov 2022
'    Filename:Task_Timer.bas
'    Function:  Annex Task Timer
'    Device: ESP32/8266
'
'    References:
'
'  *******************************
' v0.05 initial
'
  dim tt(10)  'Task Timer array
'Task "Tick"  Timer List
'tt(0)=Expired, tt(-1)=Disabled
  tt(0)=0' seconds  , [u]set to run once on startup[/u]
  tt(1)=10' seconds
  tt(2)=30' seconds
  tt(3)=60' seconds
  tt(4)=86400-TIMEUNIX(time$) ' [u]set to trigger at mid night , then 24 hrs thereafter[/u]
  tt(5)=-1' seconds  'wifi sleep
  tt(6)=-1' seconds  'wifi wake
'
' Timers & Interupts
  timer0 1000 ,T1   'establish seconds "Tick Timer"
  
  tt(5)=10  'set up trigger task 5 wifi sleep
  
'main program loop
  do
    it=millis
    option.WDTreset
    if tt(0)=0 then
      tt(0)=-1  'dont run again
      gosub onStartup
    end if
    
    If tt(1)=0 Then
      tt(1)=10
'eg. 10 second report
'wlog "The Temperature DS18B20 is ";ds_temp ; "C"
      
    EndIf
    If tt(2)=0 Then
      tt(2)=30
      if pin(12)=0 then gosub concheck 'only if wifi enabled
    EndIf
    
'wifi sleep
    if tt(5)=0 then
      tt(5)=-1  'dont run again
      wlog "******************************** sleep wifi"
      wifi.sleep
      tt(6)=10  'wake wifi trigger
    end if
    
'wifi wake
    if tt(6)=0 then
      tt(6)=-1  'dont run again
      wlog "********************************** wake wifi"
      wifi.awake
    end if
    
'check for midnight
'wlog tt(4)
    If tt(4)=0 Then
      tt(4)=86400
      mmrain=0  'reset rain
    EndIf
    
  loop
'***** End main program loop
end
  
'sub routines
T1: 'Tick Timer
'update the task timer array tt()
  for x= 0 to 10
    y=tt(x)
    if y>0 then
      y=y-1
      tt(x)=y
    end if
  next x
  return
peridot
Posts: 46
Joined: Mon Mar 08, 2021 4:54 am
Has thanked: 7 times
Been thanked: 93 times

Re: Task Timer update

Post by peridot »

Had another go at this "tasks" routine, it works a bit like cron in linux. Have now added Start/Finish time function, missed Start/Finish times, daily repeat along with normal repeat tasks. The " task 23,0,60,"","" 'active force repeat task every 60 secs" ensures a task is run at least every 60 secs to make sure the task_Flay is set and the available tasks are run. This can be used to check for missed start/finish task times, (maybe program started after a start time should have run or perhaps a remote relay manually turned off during for some reason would be corrected by the tasks), this only occurs if there is a start and finish time set.
Many task times can be set, further qualification on if a task should actually be ran can also be built into the task scripting (say odd/even days , rain sensors, say if the task is running an irrigation system.

Code: [Local Link Removed for Guests]

  '    Filename: tasks.bas
  '    Date:
  '    Initial Version:0.5
  '     Written by: M.Ogden
  '    Function: Task Timer
  '   Device:
  '    Initial source:
  '
  'Change log:
  '0.6 rebuild with task routine
  '0.7 add start/finish function, add missed tasks start / finish
  '
  '
  'task Var
  task_res=1000  'task resolution _default 1000ms
  task_max=30 'max tasks
  task_flag=0 'set any time a task goes to zero to run task
  load_flag=1 'set any time to allow tasks to load withour tasks running
  dim tt(task_max)  'Task Timer array
  tx=0:ty=0
  
  'setup initial tasks
  task 0,0,1,"","" ' usually run once on startup
  timer0 task_res ,T1   'establish seconds "Task Timer"
  
  'var
  option.WDT 20000    ' set the WDT
  it=0:rt=0
  
  
  do
    it=millis
    option.WDTreset
    'Tasks are run when expired tasks are ready
    
    'if 24Hr time is loaded then the Task time will be calculated
    'for today or next day depending on time.
    if task_flag=1 or load_flag then
      task 0,0,-1,"",""
      task 1,11,0 ,"10:00","15:45" 'start
      task 11,0,0 ,"15:45","" 'stop
      task 22,0,0,"12:15","" 'active
      task 23,0,60,"","" 'active force repeat task every 60 secs
      task 30,0,0,"00:00",""' trigger @ midnight
      task_flag=0 'reset flag
      load_flag=0 'reset flag
    endif
    
    '********************************************
    ' insert your main code here....
    '********************************************
    
    rt=(millis-it)/1000
    
  loop
  '***** End main program loop
end
  
T1:'Task Timer
  'dlog temp$
  for tx= 0 to task_max
    ty=tt(tx)
    if ty>0 then
      ty=ty-1
      if ty=0 then task_flag=1
      tt(tx)=ty
    end if
  next tx
return
  
  
  
  'task run tsk=Task#, atsk=associated task,lsk=Task time, Ts24$=24hrTime/"",Tf24$=24hrTime/""
sub task(tsk,atsk,lsk,ts24$,tf24$)
  local ret$,st,ft
  wlog "Task";str$(tsk)
  if tsk>task_max then exit sub 'if > than Max tasks available
  if tt(tsk)<0 then exit sub  'if task time has ended
  
 'check for associated task (finish time)
   'check if task has start and finish
  'and if start time has passed allow task to run
  if (atsk>0) and (ts24$<>"") and (tf24$<>"")   then
    ''has start time passed
    if (timeunix(time$)>= timeunix(ts24$)) and (timeunix(time$)<=timeunix(tf24$)) then
      tt(tsk)=0
      wlog "Just Missed start time, so run start task"
    else
      tsk=atsk
      tt(atsk)=0
      wlog "Missed start & finish time , so run finish task"
    end if
  end if
  
  if tt(tsk)>0 then exit sub  'if task time is still running
  if load_flag=0  then 'OK to Run Tasks
    select case tsk 'task number
        case 0 :wlog "run" :tt(0)=-1'reserved as run once task
        case 1 : wlog "On ":WGETASYNC("http://192.168.0.42/tools?cmd=GPIO,12,1")
      case 2
      case 3
      case 4
      case 9
      case 5
      case 6
      case 7
      case 8
      case 9
      case 10
        case 11:wlog "Off ": WGETASYNC("http://192.168.0.42/tools?cmd=GPIO,12,0")
      case 12
      case 13
      case 14
      case 15
      case 16
      case 17
      case 18
      case 19
      case 20
        
      case 21
        case 22 :wlog "11:45 Task"
      case 23
      case 24
      case 25
      case 26
      case 27
      case 28
      case 29
      case 30
        '
      case else
        tt(tsk)=-1 'stop the task
        'dlog"No Task! - stop the task"
        
    end select
  end if
  ' now set up new task time
  if lsk>0 then
    tt(tsk)=lsk 'reload task time
  else
    if ts24$<>"" then
      tt(tsk)=timeunix(ts24$)
      if tt(tsk)<= timeunix(time$) then
        tt(tsk)=tt(tsk)+(86400)
      else
        tt(tsk)=tt(tsk)-timeunix(time$)
      end if
    end if
  end if
  wlog "new task time to run in secs for Task "+str$(tsk)+" is: "+str$(tt(tsk))
end sub
User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1271 times
Contact:

Re: Task Timer

Post by cicciocb »

Thanks for your contribution, good idea and also code well written
Post Reply