ToLazyToThink

ComparatorWatcher v0

Jul 28th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. local args = {...}
  4.  
  5. if args[3] == nil then
  6.   print("usage: " .. shell.getRunningProgram() .. " <comaparator side> <low> <high>")
  7.   error()
  8. end
  9.  
  10. local side = args[1]
  11. local low  = tonumber(args[2])
  12. local high = tonumber(args[3])
  13.  
  14.  
  15. local on = redstone.getAnalogInput(side) <= low
  16.  
  17. function checkSwitch()
  18.   if on then
  19.     if redstone.getAnalogInput(side) >= high then
  20.       on = false
  21.       return true
  22.     end
  23.   else
  24.     if redstone.getAnalogInput(side) <= low then
  25.       on = true
  26.       return true
  27.     end
  28.   end
  29.   return false
  30. end
  31.  
  32. function stateStr(state)
  33.   if state then
  34.     return "on"
  35.   else
  36.     return "off"
  37.   end
  38. end
  39.  
  40. print("startup " .. stateStr(on))
  41. rednet.broadcast(stateStr(on))
  42.  
  43. while true do
  44.   local e, p1, p2 = os.pullEvent()
  45.   print(e)
  46.   if e == "rednet_message" then
  47.     if p2 == "stateplease" then
  48.       rednet.send(p1, stateStr(on))
  49.       print(os.clock() .. " sent " .. stateStr(on) .. " to " .. p1)
  50.     end
  51.   end
  52.  
  53.   if e == "redstone" then
  54.     print(os.clock() .. " " .. redstone.getAnalogInput(side))
  55.     if checkSwitch() then
  56.       rednet.broadcast(stateStr(on))
  57.       print(os.clock() .. " switch " .. stateStr(on))
  58.     end
  59.   end
  60. end
Add Comment
Please, Sign In to add comment