Advertisement
CaptainResu

Elevator

May 7th, 2022 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. currentLevel = 1
  2. maxLevel = 0
  3. findLevel = 15
  4. currentLevelInput = "back"
  5. goUpInput = "left"
  6. goDownInput = "right"
  7. callInput = "front"
  8.  
  9. clutchOutput = "top"
  10. updownOutput = "bottom"
  11.  
  12. currentLevelData = 0
  13. goUpDataRaw = 0
  14. goUpData = false
  15. goDownDataRaw = 0
  16. goDownData = false
  17. callData = 0
  18.  
  19. clutchOutputData = 15
  20. updownOutputData = 0
  21.  
  22. function updateData()
  23.     currentLevelUpdate()
  24.     currentCalldataUpdate()
  25. end
  26.  
  27. function currentLevelUpdate()
  28.     currentLevelData = redstone.getAnalogInput(currentLevelInput)
  29.     if currentLevelData ~= 0 then
  30.         currentLevel = currentLevelData
  31.     end
  32. end
  33. function currentCalldataUpdate()
  34.     callData = redstone.getAnalogInput(callInput)
  35.     goUpData = (goUpDataRaw > 1)
  36.     goDownData = (goDownDataRaw > 1)
  37.  
  38.     goUpDataRaw = redstone.getAnalogInput(goUpInput)
  39.     goDownDataRaw = redstone.getAnalogInput(goDownInput)
  40.  
  41.     if callData ~= 0 then
  42.         findLevel = callData
  43.         callData = 0
  44.     elseif goUpData and goUpDataRaw == 0 then
  45.         findLevel = findLevel + 1
  46.         goUpData = false
  47.         if findLevel > 15 then
  48.             findLevel = 15
  49.         end
  50.     elseif goDownData and goDownDataRaw == 0 then
  51.         findLevel =  findLevel - 1
  52.         goDownData = false
  53.         if findLevel < 1 then
  54.             findLevel = 1
  55.         end
  56.     end
  57. end
  58.  
  59. function moveStop()
  60.     -- redstone.setAnalogOutput(clutchOutput, 15)
  61.     clutchOutputData = 15
  62. end
  63.  
  64. function moveDown()
  65.     if findLevel ~= currentLevel then
  66.         -- redstone.setAnalogOutput(updownOutput, 0)
  67.         -- redstone.setAnalogOutput(clutchOutput, 0)
  68.         updownOutputData = 0
  69.         clutchOutputData = 0
  70.     end
  71. end
  72. function moveUp()
  73.     if findLevel ~= currentLevel then
  74.         -- redstone.setAnalogOutput(updownOutput, 15)
  75.         -- redstone.setAnalogOutput(clutchOutput, 0)
  76.         updownOutputData = 15
  77.         clutchOutputData = 0
  78.     end
  79. end
  80.  
  81. function calibrate()
  82.     -- currentLevel = 0
  83.     -- maxLevel = 0
  84.  
  85.     -- startTime = os.time()
  86.     -- while startTime + 600 < os.time() do
  87.     --     redstone.setAnalogOutput(updownOutput, 0)
  88.     --     redstone.setAnalogOutput(clutchOutput, 0)
  89.     -- end
  90.  
  91.     -- redstone.setAnalogOutput(updownOutput, 15)
  92.     -- redstone.setAnalogOutput(clutchOutput, 0)
  93.  
  94.     -- while true do
  95.     --     -- Aantal verdiepingen tellen tot max input
  96.     --     currentLevelData = redstone.getAnalogInput(currentLevelInput)
  97.     --     if currentLevelData ~= 0 then
  98.     --         currentLevel = currentLevelData
  99.     --         maxLevel++
  100.     --     end
  101.     -- end
  102.     repeat
  103.         os.queueEvent("randomEvent")
  104.         os.pullEvent()
  105.  
  106.         updateData()
  107.         moveUp()
  108.  
  109.         redstone.setAnalogOutput(updownOutput, updownOutputData)
  110.         redstone.setAnalogOutput(clutchOutput, clutchOutputData)
  111.        
  112.     until currentLevelData == 15
  113.     moveStop()
  114.     -- redstone.setAnalogOutput(updownOutput, 15)
  115.     -- redstone.setAnalogOutput(clutchOutput, 0)
  116.     -- currentLevelUpdate()
  117. end
  118.  
  119. -- Start routine
  120. moveStop()
  121. calibrate()
  122.  
  123. while true do
  124.     -- term.clear()
  125.     os.queueEvent("randomEvent")
  126.     os.pullEvent()
  127.  
  128.     updateData()
  129.     -- Check knop voor hoger of lager
  130.     if findLevel ~= currentLevelData then
  131.         if findLevel > currentLevel then
  132.             moveUp()
  133.         elseif findLevel < currentLevel then
  134.             moveDown()
  135.         else
  136.             moveStop()
  137.         end
  138.     else
  139.         moveStop()
  140.     end
  141.  
  142.    
  143.     redstone.setAnalogOutput(updownOutput, updownOutputData)
  144.     redstone.setAnalogOutput(clutchOutput, clutchOutputData)
  145.  
  146.     term.setCursorPos(1, 1)
  147.     print("Current level is ".. currentLevel ..", and I am going to level ".. findLevel .."                    ")
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement