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
#2
Have you seen this?
https://forum.logicmachine.net/showthrea...0#pid31820
Resident script is constantly running PID so variable will be kept.
Storage is running in memory so you can write as many times you want. It is then saved to SD every 30min.
------------------------------
Ctrl+F5
Reply
#3
(15.03.2024, 11:20)Daniel Wrote: Have you seen this?
https://forum.logicmachine.net/showthrea...0#pid31820
Resident script is constantly running PID so variable will be kept.
Storage is running in memory so you can write as many times you want. It is then saved to SD every 30min.

Thanks for the answer Daniel! Great thread also, I didn't know it existed!

Based on the fact that it is saved per 30min to SD (I think same applies to objects), does it mean that if power cut happens sooner than 30min after last change then these storage (and objects') values are lost?
Reply


Forum Jump: