Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- What we want to achieve
- -- We need an elevator system with a few things
- -- - Normal up and down elevator controls on ele
- -- - Call feature
- -- To get the normal up and down feature, we use an alalog lever.
- -- The alalog lever emits a signal strength as shown on the tube above (0-15?)
- -- The signal will then be our "goto" floor represented by an int
- -- Adjacent to the analog lever is a button. Upon pressing the button, the actual "move" signal will be sent.
- -- Upon "move" being sent, the elevator will find the direction by comparing the "current" floor to the "goto" floor.
- -- After the direction is found, the elevator will move to this position until a signal(emitted wirelessly from a modem), which contains the "current", and the "goto" floor are the same.
- -- When the disired floor is reached, a clutch will be activated stopping the system.
- -- (The system must always be in idle!)
- term.clear()
- local id = os.getComputerID()
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- modem.open(id)
- function resetTextColor()
- if term.getTextColor() ~= 1 then
- term.setTextColor(colors.white)
- end
- end
- function centerText(text)
- x, y = term.getSize()
- currentX, currentY = term.getCursorPos()
- -- length x/2 - text length/2, currentY
- term.setCursorPos( (x/2 - #text/2), (currentY) )
- term.write(text)
- end
- function rightText(text)
- x, y = term.getSize()
- currentX, currentY = term.getCursorPos()
- -- length x/2 - text length, currentY
- term.setCursorPos( (x/2 - #text), (currentY) )
- term.write(text)
- end
- function textThrower(positionY, text, textColor, center, clear)
- term.setCursorPos(1, positionY)
- if clear == true then
- term.clearLine()
- end
- term.setTextColor(textColor)
- if center == true then
- centerText(text)
- elseif center == false then
- term.write(text)
- end
- resetTextColor()
- end
- -- debug info
- if fs.exists("elevatorMainData.txt") == true then
- textThrower(1, "ID: " .. id .. " || Data = true", colors.orange, true, false)
- else
- textThrower(1, "ID: " .. id .. " || Data = false", colors.orange, true, false)
- end
- textThrower(2, "Elevator Debug Located on Left", colors.orange, true, false)
- textThrower(3, "Elevator Done Worse, The Sam Way", colors.orange, true, false)
- function readData()
- resetTextColor()
- if fs.exists("elevatorMainData.txt") then
- local file = fs.open("elevatorMainData.txt", "r")
- importantData = {}
- while true do
- local line = file.readLine()
- if not line then break end
- importantData[#importantData + 1] = tonumber(line)
- end
- maxFloors = importantData[1]
- currentFloorStartup = importantData[2]
- file.close()
- -- term.setCursorPos(1, 9)
- -- term.write("Total Floors: " .. importantData[1])
- -- term.setCursorPos(1, 10)
- -- term.write("Current Floor: " .. importantData[2])
- end
- end
- function requestData()
- resetTextColor()
- -- check if a "data" file exists, if not, create one
- while fs.exists("elevatorMainData.txt") ~= true do
- -- set background color to green to show that we're in the data collection process
- --term.setBackgroundColor(colors.green)
- -- collect elevator data
- textThrower(4, "Number of Floors: ", colors.lightBlue, false, false)
- maxFloors = tonumber(read())
- textThrower(5, "Current Floor: ", colors.lightBlue, false, false)
- currentFloorStartup = tonumber(read())
- -- write data to file which can be called later.
- if tonumber(maxFloors) ~= nil and tonumber(currentFloorStartup) ~= nil then
- -- if no issues, save
- local file = fs.open("elevatorMainData.txt", "w")
- file.writeLine(maxFloors)
- file.writeLine(currentFloorStartup)
- file.close()
- elseif tonumber(maxFloors) == nil and tonumber(currentFloorStartup) == nil then
- -- if issue is presented, throw error and reboot
- textThrower(19, "ERROR DEFINING DATA, REBOOTING IN 1 SECOND...", colors.red, true, true)
- sleep(1)
- os.reboot()
- end
- end
- end
- -- we also want a way to delete data, so, we'll have a function which is always waiting for
- function deleteData()
- resetTextColor()
- --while true do
- -- get keys
- -- local event, key = os.pullEvent("key")
- -- if key == key.d then
- -- term.setBackgroundColor(colors.red)
- -- term.write("Deleting Data... ")
- -- fs.delete("data")
- -- end
- --end
- if fs.exists("elevatorMainData.txt") == true then
- fs.delete("elevatorMainData.txt")
- textThrower(19, 'Deleted "data"', colors.red, true, true)
- end
- end
- -- 6 - 20
- function deleteThing()
- local startA = 11
- local endA = 18
- local event, key = os.pullEvent("key")
- textThrower(19, "Deleting", color.red, true, true)
- if key == key.d then
- while startA ~= endA do
- term.clearLine(1, startA)
- startA = startA + 1
- end
- end
- end
- function elevatorMain()
- -- what I want to do here,
- -- get three things,
- -- analog signal ✓
- -- activate elevator signal
- -- floor signal ✓
- -- global vars
- splitMessage = {}
- elevatorCurrentFloor = currentFloorStartup
- elevatorAnalogValue = currentFloorStartup
- -- "starting" elevator
- textThrower(6, "Elevator Started", colors.green, true, true)
- -- Receiving message from floor controllers
- while true do
- -- get data, set vars
- readData()
- elevatorCurrentFloor = currentFloorStartup
- -- Lock clutch
- redstone.setOutput("left", true)
- -- general debug message
- -- event handling
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- local message = eventData[5]
- -- get all modem_message's
- if event == "modem_message" then
- textThrower(9, "Received Floor Message: " .. message, colors.pink, false, false)
- -- split data, store in table
- for word in (message .. " "):gmatch("(.-)" .. " ") do
- table.insert(splitMessage, word)
- end
- elevatorCurrentFloor = splitMessage[2]
- -- stores message(floor number) in global var for later use
- end
- -- if getting signal, record value
- if event == "redstone" and redstone.getInput("right") and not redstone.getInput("back") then
- -- show debug message
- elevatorAnalogValue = redstone.getAnalogInput("right")
- textThrower(13, "Analog Value: " .. elevatorAnalogValue, colors.yellow , false, false)
- end
- -- get call signal
- if event == "modem_message" and splitMessage[1] == "call" then
- textThrower(11, "Call From Floor: " .. splitMessage[2], colors.pink, false, false)
- end
- textThrower(14, "eleCur: " .. elevatorCurrentFloor .. " analog: " .. elevatorAnalogValue .. " maxFlr: " .. maxFloors, colors.yellow , false, true)
- -- if button press on elevator
- if ( (event == "redstone" and redstone.getInput("back") and tonumber(elevatorAnalogValue) ~= tonumber(elevatorCurrentFloor)) or (event == "modem_message" and splitMessage[1] == "call") ) then
- -- show debug message
- textThrower(15, "Elevator Button Press", colors.pink, false, false)
- -- do elevator things
- localElevatorCurrentFloor = elevatorCurrentFloor
- while true do
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- local message = eventData[5]
- local cum = {}
- -- get current elevator position locally
- if event == "modem_message" then
- textThrower(14, "eleCur: " .. elevatorCurrentFloor .. " analog: " .. elevatorAnalogValue .. " maxFlr: " .. maxFloors .. " localEleFlr: " .. localElevatorCurrentFloor, colors.yellow , false, true)
- for word in (message .. " "):gmatch("(.-)" .. " ") do
- table.insert(cum, word)
- end
- localElevatorCurrentFloor = tonumber(cum[2])
- textThrower(10, "Local Floor Message: " .. localElevatorCurrentFloor, colors.pink , false, true)
- elseif event == "modem_message" and cum[2] == "call" then
- elevatorAnalogValue = tonumber(cum[2])
- textThrower(12, "Updated AnalogValue For Call: " .. elevatorAnalogValue, colors.pink, false, true)
- end
- -- get direction
- if tonumber(elevatorAnalogValue) < tonumber(localElevatorCurrentFloor) then
- textThrower(7, "Going Down From: " .. elevatorCurrentFloor .. " To: " .. elevatorAnalogValue, colors.magenta, false, true)
- redstone.setOutput("front", true)
- elseif tonumber(elevatorAnalogValue) > tonumber(localElevatorCurrentFloor) then
- textThrower(7, "Going Up From: " .. elevatorCurrentFloor .. " To: " .. elevatorAnalogValue, colors.magenta, false, true)
- redstone.setOutput("front", false)
- else
- textThrower(19, "Directional Error", colors.red, false, true)
- end
- -- Unlock clutch
- redstone.setOutput("left", false)
- -- Stop Elevator
- textThrower(18, localElevatorCurrentFloor .. " " .. elevatorAnalogValue, colors.yellow, false, true)
- if tonumber(localElevatorCurrentFloor) == tonumber(elevatorAnalogValue) then
- textThrower(14, "eleCur: " .. elevatorCurrentFloor .. " analog: " .. elevatorAnalogValue .. " maxFlr: " .. maxFloors .. " localEleFlr: " .. localElevatorCurrentFloor, colors.yellow , false, true)
- elevatorCurrentFloor = localElevatorCurrentFloor
- redstone.setOutput("left", true)
- sleep(0.02)
- textThrower(17, "Stopping Elevator At Floor: " .. elevatorCurrentFloor, colors.blue, false, true)
- textThrower(8, "Arrived At Floor", colors.green, true, false)
- local file = fs.open("elevatorMainData.txt", "w")
- file.writeLine(importantData[1])
- file.writeLine(elevatorCurrentFloor)
- file.close()
- break
- end
- end
- end
- textThrower(19, "Error on Elevator Button Press", colors.red, false, true)
- end
- end
- -- deleteData() -- manual delete for testing
- requestData()
- readData()
- elevatorMain()
- parallel.waitForAny(elevatorMain, deleteThing)
Add Comment
Please, Sign In to add comment