Advertisement
blueman7700

motorController

Jan 27th, 2022 (edited)
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. print("Elevator Controller Started....")
  2.  
  3. local arg1, arg2, arg3, arg4 = ...
  4.  
  5. local bottomFloor = tonumber(arg1)
  6. local topFloor = tonumber(arg2)
  7. local currFloor = tonumber(arg3)
  8. local motorRPM = tonumber(arg4)
  9.  
  10. if motorRPM == nil then
  11.     motorRPM = 32
  12. end
  13.  
  14. Motor = peripheral.wrap("bottom")
  15. Motor.stop()
  16.  
  17. rednet.open("back")
  18. print("rednet port opened on 'back'...")
  19.  
  20. local function Up()
  21.     print("elevator moving UP...")
  22.     sleep(Motor.translate(5, motorRPM))
  23.     Motor.stop()
  24.     currFloor = currFloor + 1
  25.     print("elevator is at floor: ",currFloor,"\n")
  26. end
  27.  
  28. local function Down()
  29.     print("elevator moving DOWN...")
  30.     sleep(Motor.translate(-5, motorRPM))
  31.     Motor.stop()
  32.     currFloor = currFloor - 1
  33.     print("elevator is at floor: ",currFloor,"\n")
  34. end
  35.  
  36. local function GoToFloor(x)
  37.     --check if the requested floor is in range
  38.     if x >= bottomFloor and x <= topFloor  then
  39.         print("requested floor: ",x,", ... current floor is: ",currFloor)
  40.         --keep moving until the floor is reached
  41.         while currFloor ~= x do
  42.             if x > currFloor then
  43.                 Up()
  44.             elseif x < currFloor then
  45.                 Down()
  46.             end
  47.         end
  48.     end
  49. end
  50.  
  51. while true do
  52.  
  53.     --wait for a rednet signal
  54.     local id, msg = rednet.receive("moveOrder")
  55.     print("recieved message: ", msg, ". from ", id)
  56.  
  57.  
  58.     if type(msg) == "number" then
  59.         if msg ~= nil and msg >= bottomFloor and msg <= topFloor then
  60.             GoToFloor(msg)
  61.         end
  62.     else
  63.         if msg == "up" then
  64.             --make sure we arent already at the top floor.
  65.             if currFloor < topFloor then
  66.                 Up()
  67.             end
  68.         elseif msg == "down" then
  69.             --make sure we arent already at the bottom floor.
  70.             if currFloor > bottomFloor then
  71.                 Down()
  72.             end  
  73.         end
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement