This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Implementation of TON Delay Timer
#1
Hi,

I' d like to share the code I made to simulate a classic TON Delay Timer.


Code:
-- TON DELAY TIMER

-- PARAMETERS
time_delay_in_min = 0.3
timer = 'T001'

-- TIMER ENABLE CONDITIONS
v1 = grp.getvalue('38/1/1')
v2 = true
condition = v1 and v2


tmr_started = storage.get(timer .. '_s', false) -- Read start status of timer flag
tmr_start_time = storage.get(timer .. '_start_time')
tmr_ended = storage.get(timer .. '_e', false) -- Read end status of timer flag


if condition then --A
  if tmr_started then --A1
    if tmr_ended then --A1.1
    -- do nothing
    else --A1.2
  log('Timer ' .. timer .. ' is running:')
      delta = os.time() - tmr_start_time
     
      if delta >= time_delay_in_min * 60 then --A1.2.1
  storage.set(timer .. '_e', true)
  log('Timer ' .. timer .. ' counting ended at : ' .. os.time())
       
        --[[
        actions to implement go here
        ]]--
       
  end
    end
 
  else --A2 (timer did not start -> 1st run)
 
    storage.set(timer ..'_s', true) -- set start status
    storage.set(timer ..'_start_time', os.time()) --get current timestamp
    log('Timer ' .. timer .. ' counting started at : ' .. os.time())
  log('Timer ' .. timer .. ' Ton delay was set to : ' .. time_delay_in_min .. ' min')
 
  end

else --B
 
  if tmr_started then
 
    if not tmr_ended then
    log('Timer ' .. timer .. ' is resetted')
  else
  storage.set(timer ..'_e', false) end
    end
 
  storage.set(timer ..'_s', false)
 
end



I run the following code in resident script with a time interval of 2sec (depends on required precision of course).
As it is a classic TON Delay Timer, some actions take place after the time counting which starts with the enabling of the Timer (condition = true).
If condition gets false before completing the Ton delay time setting, then the Timer is resetted and more generally a "condition = false" resets the timer.
Someone could add code in case "B" (outside "if"'s "else") to take back actions if it is desired.

I would like to ask :
1. In resident script was it necessary to use storage. That is, do variables keep their values among cycles?
2. Does writing to storage wear the SD Card?

Any other suggestions are welcome.

Thank you in advance.
Reply


Messages In This Thread
Implementation of TON Delay Timer - by p_xatzi - 15.03.2024, 10:43

Forum Jump: