Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Lift motor
- --###########################
- --controller.move(<dir>, <move?>, <Don'tMoveController?>) -> Bools: Are we just testing if we can move? | Should the controller stay where it is? (only the controller, not the computer)
- --CONST
- M_SIDE = "left" --Modem side
- C_SIDE = "back" --Controller side
- CHANNEL = 1 --Channel to receive orders from
- DIR = { -- Directions codes used by RIM
- ["down"] = 0,
- ["up"] = 1,
- ["north"] = 2,
- ["south"] = 3,
- ["west"] = 4,
- ["east"] = 5
- }
- --
- --FUNC
- function move(dir, ctrl) --Moves in the given direction using given controller handle (prevents bugs and lift being stuck)
- while true do
- if ctrl.move(dir, true, false) then --We can move
- ctrl.move(dir, false, false) --Moving
- break --Stopping the loop
- else
- print("I'm stuck ! D:")
- os.sleep(5) -- Wait 5 seconds
- end
- end
- os.reboot() --Just in case
- end
- --
- --MAIN
- --Wrapping controller and modem
- local controller = peripheral.wrap(C_SIDE)
- local modem = peripheral.wrap(M_SIDE)
- --Open channel
- modem.open(CHANNEL)
- local pos, dest
- --If the config file does not exists, we assume that's the first start and that we are at level 0
- if not fs.exists("data/motor") then
- fs.makeDir("data")
- print("File not found, starting at level 0")
- pos = 0
- dest = 0
- --Creating the file
- local f = fs.open("data/motor", 'w')
- f.writeLine(pos)
- f.writeLine(dest)
- f.close()
- else --If it exists, we read position and destination
- local file = fs.open("data/motor", 'r')
- pos = tonumber(file.readLine())
- dest = tonumber(file.readLine())
- file.close()
- print("Data found: current = "..pos.."; destination = "..dest)
- end
- --Moving
- if pos ~= dest then --Position is different from destination, we should move
- print("Position not matching destination")
- if pos > dest then --If we're above, we go down
- print("Moving down")
- pos = pos-1
- --Writing position before moving, because it will shut down the computer
- local f = fs.open("data/motor", 'w')
- f.writeLine(pos)
- f.writeLine(dest)
- f.close()
- move(DIR.down, controller)
- else --Otherwise we go up
- print("Moving up")
- pos = pos+1
- --
- local f = fs.open("data/motor", 'w')
- f.writeLine(pos)
- f.writeLine(dest)
- f.close()
- move(DIR.up, controller)
- end
- os.reboot()--
- end
- -- Waiting loop
- while true do
- --Getting messages
- local event = {os.pullEvent("modem_message")}
- if tonumber(event[5]) ~= nil then --The message is a number
- --Say you got the message (used in the call buttons)
- modem.transmit(CHANNEL, CHANNEL, "pong")
- --Writing down the destination before rebooting after the loop
- dest = tonumber(event[5])
- local f = fs.open("data/motor", 'w')
- f.writeLine(pos)
- f.writeLine(dest)
- f.close()
- break --Stopping the loop
- end
- end
- os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement