Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Elevator Controller Started....")
- local arg1, arg2, arg3, arg4 = ...
- local bottomFloor = tonumber(arg1)
- local topFloor = tonumber(arg2)
- local currFloor = tonumber(arg3)
- local motorRPM = tonumber(arg4)
- if motorRPM == nil then
- motorRPM = 32
- end
- Motor = peripheral.wrap("bottom")
- Motor.stop()
- rednet.open("back")
- print("rednet port opened on 'back'...")
- local function Up()
- print("elevator moving UP...")
- sleep(Motor.translate(5, motorRPM))
- Motor.stop()
- currFloor = currFloor + 1
- print("elevator is at floor: ",currFloor,"\n")
- end
- local function Down()
- print("elevator moving DOWN...")
- sleep(Motor.translate(-5, motorRPM))
- Motor.stop()
- currFloor = currFloor - 1
- print("elevator is at floor: ",currFloor,"\n")
- end
- local function GoToFloor(x)
- --check if the requested floor is in range
- if x >= bottomFloor and x <= topFloor then
- print("requested floor: ",x,", ... current floor is: ",currFloor)
- --keep moving until the floor is reached
- while currFloor ~= x do
- if x > currFloor then
- Up()
- elseif x < currFloor then
- Down()
- end
- end
- end
- end
- while true do
- --wait for a rednet signal
- local id, msg = rednet.receive("moveOrder")
- print("recieved message: ", msg, ". from ", id)
- if type(msg) == "number" then
- if msg ~= nil and msg >= bottomFloor and msg <= topFloor then
- GoToFloor(msg)
- end
- else
- if msg == "up" then
- --make sure we arent already at the top floor.
- if currFloor < topFloor then
- Up()
- end
- elseif msg == "down" then
- --make sure we arent already at the bottom floor.
- if currFloor > bottomFloor then
- Down()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement