JackMacWindows

Multi-floor Turtle Elevator

Dec 27th, 2020 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. local floor = 1
  2. local pulses = 0
  3. local timer
  4.  
  5. local function goToFloor(n)
  6.     print("Going to floor " .. n)
  7.     turtle.refuel()
  8.     while floor ~= n do
  9.         if n > floor then
  10.             repeat turtle.up() until rs.getInput("back")
  11.             floor = floor + 1
  12.         elseif n < floor then
  13.             repeat turtle.down() until rs.getInput("back")
  14.             floor = floor - 1
  15.         else break end
  16.     end
  17.     redstone.setOutput("left", true)
  18.     local file = fs.open("floor.txt", "w")
  19.     file.writeLine(floor)
  20.     file.close()
  21.     sleep(0.4)
  22.     redstone.setOutput("left", false)
  23. end
  24.  
  25. redstone.setOutput("right", true)
  26. redstone.setOutput("left", false)
  27.  
  28. if fs.exists("floor.txt") then
  29.     local file = fs.open("floor.txt", "r")
  30.     floor = tonumber(file.readLine()) or floor
  31.     file.close()
  32. end
  33.  
  34. while true do
  35.     local ev = {os.pullEvent()}
  36.     if ev[1] == "redstone" and redstone.getInput("front") then
  37.         if timer then os.cancelTimer(timer) end
  38.         timer = os.startTimer(2)
  39.         pulses = pulses + 1
  40.     elseif ev[1] == "timer" and ev[2] == timer then
  41.         timer = nil
  42.         goToFloor(pulses)
  43.         pulses = 0
  44.     end
  45. end
Add Comment
Please, Sign In to add comment