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.

Delay script
#15
Create user library named timers:
Code:
return function(timers)
  if not client then
    grp.sender = 'tm'

    function set(timer)
      grp.checkwrite(timer.output, timer.state == 'on')

      timer.state = nil
      timer.ticks = nil
    end

    client = require('localbus').new(0.1)
    client:sethandler('groupwrite', function(event)
      local timer = timers[ event.dst ]
      
      if timer and event.sender ~= grp.sender then
        local value = tonumber(event.datahex, 16) or 0
        local state = value > 0 and 'on' or 'off'
        
        if timer.state ~= state then
          timer.state = state

          local ticks = timer['delay' .. state]
          
          if ticks > 0 then
            timer.ticks = ticks
          else
            set(timer)
          end
        end
      end
    end)
  end

  client:loop(1)

  for _, timer in pairs(timers) do
    if timer.ticks then
      timer.ticks = timer.ticks - 1

      if timer.ticks == 0 then
        set(timer)
      end
    end
  end
end

Resident script:
Code:
if not timers then
  timers = {
    ['1/1/1'] = {
      output = '1/1/2', -- binary output
      delayon = 10, -- on delay time (seconds)
      delayoff = 0, -- off delay time (seconds)
    }
  }
end

require('user.timers')(timers)
Reply


Messages In This Thread
Delay script - by josdegroot - 08.12.2022, 10:41
RE: Delay script - by admin - 08.12.2022, 10:59
RE: Delay script - by josdegroot - 08.12.2022, 11:29
RE: Delay script - by admin - 08.12.2022, 13:02
RE: Delay script - by KoBra - 15.08.2023, 16:32
RE: Delay script - by josdegroot - 08.12.2022, 15:49
RE: Delay script - by josdegroot - 10.12.2022, 07:56
RE: Delay script - by admin - 10.12.2022, 11:27
RE: Delay script - by josdegroot - 11.12.2022, 07:42
RE: Delay script - by admin - 16.08.2023, 06:19
RE: Delay script - by KoBra - 17.08.2023, 13:43
RE: Delay script - by myg - 17.08.2023, 10:27
RE: Delay script - by admin - 17.08.2023, 10:31
RE: Delay script - by sjfp - 15.11.2023, 12:02
RE: Delay script - by admin - 15.11.2023, 14:26
RE: Delay script - by ErkinVural - 23.01.2024, 09:26
RE: Delay script - by sjfp - 15.11.2023, 14:35
RE: Delay script - by Daniel - 17.11.2023, 13:34
RE: Delay script - by sjfp - 17.11.2023, 13:48
RE: Delay script - by admin - 22.11.2023, 09:52
RE: Delay script - by sjfp - 22.11.2023, 11:27

Forum Jump: