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
#1
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
Reply
#2
Is the time1 different then time2?
------------------------------
Ctrl+F5
Reply
#3
yes. movement1= 100seconds and each another movement will add 30seconds
Reply
#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


Forum Jump: