Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --VAR
- Clutch = "right"
- Gearshift = "back"
- IsInitializing = false -- lift is in process of initialization
- IsReady = false -- lift is ready, don't moving and await for commands
- IsMoving = false -- lift is currently moving
- CurrentLevel = 0
- TargetLevel = 0
- --initialize
- rs.setOutput(Clutch , true) --set true to off
- rs.setOutput(Gearshift , true) --true to move up
- modem = peripheral.wrap("top")
- modem.open(21)
- ------------------
- ---lift server start up
- function Initialize()
- print("INITIALIZE")
- IsInitializing = true
- TargetLevel = 0
- --send command to reset all clients
- modem.transmit(21, 22, "init")
- --set lift to move down
- rs.setOutput(Gearshift, false)
- sleep(0.5)
- --start lift move down
- rs.setOutput(Clutch, false)
- end
- --
- function ProcessCommand(command)
- write("received=")
- print(command)
- --
- if (command == "request") then
- if (IsReady) then
- modem.transmit(21, 22, "ready")
- return
- end
- if (IsMoving) then
- modem.transmit(21, 22, "moving "..TargetLevel )
- return
- end
- return
- end
- --
- if (IsInitializing and command == "contact 0") then
- CurrentLevel = 0
- rs.setOutput(Clutch , true) --set true to off
- rs.setOutput(Gearshift , false) --true to move up
- IsInitializing = false
- IsReady = true
- modem.transmit(21, 22, "ready")
- return
- end
- --
- if (IsMoving) then
- if (command == "contact 0") then
- ProcessContactCommand(0)
- end
- if (command == "contact 1") then
- ProcessContactCommand(1)
- end
- return
- end
- --
- if (IsReady) then
- if (command == "call 0") then
- CallLiftToLevel(0)
- end
- if (command == "call 1") then
- CallLiftToLevel(1)
- end
- return
- end
- end
- --Call lift moving to specified level
- function CallLiftToLevel(level)
- if (IsReady == false) then
- return
- end
- --
- if (level == CurrentLevel) then
- return
- end
- if (level > CurrentLevel) then
- --move up
- TargetLevel = level
- IsMoving = true
- modem.transmit(21, 22, "lock 1 UP")
- modem.transmit(21, 22, "moving 1")
- rs.setOutput(Gearshift , true) --true to move up
- -- WAIT FOR RESPONSE
- sleep(0.5)
- rs.setOutput(Clutch , false) --set false to on
- end
- if (level < CurrentLevel) then
- --move down
- TargetLevel = level
- IsMoving = true
- modem.transmit(21, 22, "lock 0 DOWN")
- modem.transmit(21, 22, "moving 0")
- rs.setOutput(Gearshift , false) --false to move down
- -- WAIT FOR RESPONSE
- sleep(0.5)
- rs.setOutput(Clutch , false) --set false to on
- end
- end
- --Process contact command while lift is moving
- function ProcessContactCommand(level)
- if (level == TargetLevel) then
- rs.setOutput(Clutch , true) --set true to off
- sleep(0.5)
- CurrentLevel = TargetLevel
- modem.transmit(21, 22, "ready")
- IsMoving = false
- end
- end
- --
- function ProcessRedstone()
- detector = rs.getInput("left")
- if (detector == true) then
- modem.transmit(21, 22, "contact 1")
- ProcessCommand("contact 1")
- end
- end
- --initialize
- Initialize()
- --Main loop
- repeat
- --local myTimer = os.startTimer(1)
- event,p1,p2,p3,p4,p5 = os.pullEvent()
- if (event=="redstone") then
- ProcessRedstone()
- end
- if (event=="modem_message") then
- --event, side, channel, replyChannel, message, distance
- ProcessCommand(p4)
- end
- if event=="timer" then
- print("test")
- end
- until event=="char" and p1==("q")
Add Comment
Please, Sign In to add comment