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.

movement detection
#4
Resident script with sleep time set to 0. In this example 0/0/1 is movement input (1 bit) and 0/0/6 is actuator output control (1 bit). Multiple timers can be added to the timers table if needed.
Code:
if not client then
  timers = {
    ['0/0/1'] = {
      output = '0/0/6', -- binary output control (0/1)
      timeoutinit = 100, -- initial timeout in seconds
      timeoutadd = 30, -- additional timeout in seconds
    },
  }

  function timerset(timer, value, ticks)
    timer.on = value
    timer.ticks = ticks
    grp.write(timer.output, value, dt.bool)
  end

  function timerstart(timer)
    if timer.on then
      timer.ticks = timer.ticks + timer.timeoutadd
    else
      timerset(timer, true, timer.timeoutinit)
    end
  end

  grp.sender = 'tm'

  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
      if value == 1 then
        timerstart(timer)
      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
      timerset(timer, false)
    end
  end
end
Reply


Messages In This Thread
movement detection - by almoisey - 23.10.2024, 15:02
RE: movement detection - by Daniel - 23.10.2024, 15:05
RE: movement detection - by almoisey - 24.10.2024, 04:31
RE: movement detection - by admin - 25.10.2024, 07:37

Forum Jump: