Logic Machine Forum
movement detection - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: movement detection (/showthread.php?tid=5700)



movement detection - almoisey - 23.10.2024

Hello. I need that  script for movement detection-if there is movement, the actuator is triggered for a certain time1 . if the movement is repeated(while the actuator is ON), the actuator's operating time is extended for time 2
thanks


RE: movement detection - Daniel - 23.10.2024

Is the time1 different then time2?


RE: movement detection - almoisey - 24.10.2024

yes. movement1= 100seconds and each another movement will add 30seconds


RE: movement detection - admin - 25.10.2024

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