Advertisement
Niverton

Lift-Call

Sep 12th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. --Calls lift
  2.  
  3. --CONST
  4.     POS = 0 --Number of blocks between position 0 and the level we're at (if negative, means we're below level 0)
  5.     M_SIDE = "back" --Modem side
  6.     R_SIDE = "bottom"   --Side we get lift signal from
  7.     B_SIDE = "left" --Side we get button signal from
  8.     CHANNEL = 1 --Channel to send calls to
  9. --
  10.  
  11. --FUNC
  12.     function waitForAnswer() --Waits for the lift to answer
  13.         local event
  14.         local timer = os.startTimer(1)  --Time to wait for an answer
  15.         while true do
  16.             event = {os.pullEvent()}
  17.             if event[1] == "modem_message" and event[5] == "pong" then
  18.                 return true --The lift got our message
  19.             elseif event[1] == "timer" and event[2] == timer then
  20.                 return false --The lift did not have time to answer (we're assuming it didn't get our message)
  21.             end
  22.         end
  23.     end
  24. --
  25.  
  26. --MAIN
  27. --Wrapping and opening modem
  28. local modem = peripheral.wrap(M_SIDE)
  29. modem.open(CHANNEL)
  30.  
  31. --Main loop
  32. while true do
  33.     os.pullEvent("redstone") --Waiting for a redstone state change (there is no parameter)
  34.     if rs.getInput(R_SIDE) then rs.setOutput(B_SIDE, false) end --Got signal from lift, we turn the lamp off
  35.    
  36.     if rs.getInput(B_SIDE) and not rs.getInput(R_SIDE) then --Got signal from button, and if the lift is not already there
  37.         --We send position to lift
  38.         print("Calling elevator to position "..POS)
  39.         modem.transmit(CHANNEL, CHANNEL, POS)
  40.         if waitForAnswer() then --If the lift got the message
  41.             print("Elevator incomming")
  42.             rs.setOutput(B_SIDE, true) --We keep light on to say it's comming
  43.         else    --Otherwise we do nothing, the lamp will turn off
  44.             print("Failed to call elevator")
  45.         end
  46.     end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement