FriedToes

Elevator Main

Jun 23rd, 2022 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.83 KB | None | 0 0
  1. -- What we want to achieve
  2.  
  3. --  We need an elevator system with a few things
  4. --     - Normal up and down elevator controls on ele
  5. --     - Call feature
  6.  
  7. -- To get the normal up and down feature, we use an alalog lever.
  8. --   The alalog lever emits a signal strength as shown on the tube above (0-15?)
  9. --   The signal will then be our "goto" floor represented by an int
  10. --   Adjacent to the analog lever is a button. Upon pressing the button, the actual "move" signal will be sent.
  11. --   Upon "move" being sent, the elevator will find the direction by comparing the "current" floor to the "goto" floor.
  12. --   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.
  13. --   When the disired floor is reached, a clutch will be activated stopping the system.
  14. --                                          (The system must always be in idle!)
  15.  
  16. term.clear()
  17.  
  18. local id = os.getComputerID()
  19. local modem = peripheral.find("modem") or error("No modem attached", 0)
  20. modem.open(id)
  21.  
  22. function resetTextColor()
  23.     if term.getTextColor() ~= 1 then
  24.         term.setTextColor(colors.white)
  25.     end
  26. end
  27.  
  28.  
  29. function centerText(text)
  30.     x, y = term.getSize()
  31.     currentX, currentY = term.getCursorPos()
  32.     -- length x/2 - text length/2, currentY
  33.     term.setCursorPos( (x/2 - #text/2), (currentY) )
  34.     term.write(text)
  35. end
  36.  
  37. function rightText(text)
  38.     x, y = term.getSize()
  39.     currentX, currentY = term.getCursorPos()
  40.     -- length x/2 - text length, currentY
  41.     term.setCursorPos( (x/2 - #text), (currentY) )
  42.     term.write(text)
  43. end
  44.  
  45. function textThrower(positionY, text, textColor,    center, clear)
  46.     term.setCursorPos(1, positionY)
  47.     if clear == true then
  48.         term.clearLine()
  49.     end
  50.     term.setTextColor(textColor)
  51.     if center == true then
  52.         centerText(text)
  53.     elseif center == false then
  54.         term.write(text)
  55.     end
  56.  
  57.     resetTextColor()
  58. end
  59.  
  60.  
  61. -- debug info
  62. if fs.exists("elevatorMainData.txt") == true then
  63.     textThrower(1, "ID: " .. id .. " || Data = true", colors.orange, true, false)
  64. else
  65.     textThrower(1, "ID: " .. id .. " || Data = false", colors.orange, true, false)
  66. end
  67. textThrower(2, "Elevator Debug Located on Left", colors.orange, true, false)
  68. textThrower(3, "Elevator Done Worse, The Sam Way", colors.orange, true, false)
  69.  
  70.  
  71.  
  72. function readData()
  73.     resetTextColor()
  74.     if fs.exists("elevatorMainData.txt") then
  75.         local file = fs.open("elevatorMainData.txt", "r")
  76.         importantData = {}
  77.  
  78.         while true do
  79.             local line = file.readLine()
  80.  
  81.             if not line then break end
  82.  
  83.             importantData[#importantData + 1] = tonumber(line)
  84.         end
  85.  
  86.         maxFloors = importantData[1]
  87.         currentFloorStartup = importantData[2]
  88.  
  89.         file.close()
  90.  
  91.  
  92.         -- term.setCursorPos(1, 9)
  93.         -- term.write("Total Floors: " .. importantData[1])
  94.  
  95.         -- term.setCursorPos(1, 10)
  96.         -- term.write("Current Floor: " .. importantData[2])
  97.     end
  98. end
  99.  
  100.  
  101. function requestData()
  102.     resetTextColor()
  103.     -- check if a "data" file exists, if not, create one
  104.     while fs.exists("elevatorMainData.txt") ~= true do
  105.         -- set background color to green to show that we're in the data collection process
  106.          --term.setBackgroundColor(colors.green)
  107.  
  108.         -- collect elevator data
  109.         textThrower(4, "Number of Floors: ", colors.lightBlue, false, false)
  110.         maxFloors = tonumber(read())
  111.  
  112.         textThrower(5, "Current Floor: ", colors.lightBlue, false, false)
  113.         currentFloorStartup = tonumber(read())
  114.        
  115.  
  116.  
  117.         -- write data to file which can be called later.
  118.         if tonumber(maxFloors) ~= nil and tonumber(currentFloorStartup) ~= nil then
  119.             -- if no issues, save
  120.             local file = fs.open("elevatorMainData.txt", "w")
  121.  
  122.             file.writeLine(maxFloors)
  123.             file.writeLine(currentFloorStartup)
  124.        
  125.             file.close()
  126.         elseif tonumber(maxFloors) == nil and tonumber(currentFloorStartup) == nil then
  127.             -- if issue is presented, throw error and reboot
  128.             textThrower(19, "ERROR DEFINING DATA, REBOOTING IN 1 SECOND...", colors.red, true, true)
  129.             sleep(1)
  130.             os.reboot()
  131.         end
  132.     end
  133. end
  134.  
  135. -- we also want a way to delete data, so, we'll have a function which is always waiting for
  136. function deleteData()
  137.     resetTextColor()
  138.     --while true do
  139.         -- get keys
  140.         -- local event, key = os.pullEvent("key")
  141.         -- if key == key.d then
  142.         --     term.setBackgroundColor(colors.red)
  143.         --     term.write("Deleting Data... ")
  144.         --     fs.delete("data")
  145.         -- end
  146.     --end
  147.  
  148.     if fs.exists("elevatorMainData.txt") == true then
  149.         fs.delete("elevatorMainData.txt")
  150.         textThrower(19, 'Deleted "data"', colors.red, true, true)
  151.     end
  152.  
  153. end
  154.  
  155. -- 6 - 20
  156.  
  157. function deleteThing()
  158.     local startA = 11
  159.     local endA = 18
  160.  
  161.  
  162.     local event, key = os.pullEvent("key")
  163.     textThrower(19, "Deleting", color.red, true, true)
  164.     if key == key.d then
  165.         while startA ~= endA do
  166.             term.clearLine(1, startA)
  167.             startA = startA + 1
  168.         end
  169.     end
  170. end
  171.  
  172.  
  173. function elevatorMain()
  174.     -- what I want to do here,
  175.     -- get three things,
  176.     --    analog signal ✓
  177.     --    activate elevator signal
  178.     --    floor signal ✓
  179.  
  180.     -- global vars
  181.     splitMessage = {}
  182.     elevatorCurrentFloor = currentFloorStartup
  183.     elevatorAnalogValue = currentFloorStartup
  184.  
  185.  
  186.  
  187.     -- "starting" elevator
  188.  
  189.     textThrower(6, "Elevator Started", colors.green, true, true)
  190.  
  191.     -- Receiving message from floor controllers
  192.     while true do
  193.  
  194.         -- get data, set vars
  195.         readData()
  196.         elevatorCurrentFloor = currentFloorStartup
  197.  
  198.         -- Lock clutch
  199.         redstone.setOutput("left", true)
  200.  
  201.  
  202.         -- general debug message
  203.  
  204.  
  205.         -- event handling
  206.         local eventData = {os.pullEvent()}
  207.         local event = eventData[1]
  208.         local message = eventData[5]
  209.  
  210.         -- get all modem_message's
  211.         if event == "modem_message" then
  212.             textThrower(9, "Received Floor Message: " .. message, colors.pink, false, false)
  213.             -- split data, store in table
  214.  
  215.             for word in (message .. " "):gmatch("(.-)" .. " ") do
  216.                 table.insert(splitMessage, word)
  217.             end
  218.             elevatorCurrentFloor = splitMessage[2]
  219.             -- stores message(floor number) in global var for later use
  220.         end
  221.  
  222.         -- if getting signal, record value
  223.         if event == "redstone" and redstone.getInput("right") and not redstone.getInput("back") then
  224.             -- show debug message
  225.             elevatorAnalogValue = redstone.getAnalogInput("right")
  226.             textThrower(13, "Analog Value: " .. elevatorAnalogValue, colors.yellow , false, false)
  227.         end
  228.  
  229.         -- get call signal
  230.         if event == "modem_message" and splitMessage[1] == "call" then
  231.             textThrower(11, "Call From Floor: " .. splitMessage[2], colors.pink, false, false)
  232.         end
  233.        
  234.         textThrower(14, "eleCur: " .. elevatorCurrentFloor .. " analog: " .. elevatorAnalogValue .. " maxFlr: " .. maxFloors .. "", colors.yellow , false, true)
  235.  
  236.         -- if button press on elevator
  237.         if ( (event == "redstone" and redstone.getInput("back") and tonumber(elevatorAnalogValue) ~= tonumber(elevatorCurrentFloor)) or (event == "modem_message" and splitMessage[1] == "call") ) then
  238.             -- show debug message
  239.             textThrower(14, "Elevator Button Press", colors.white, false, false)
  240.    
  241.  
  242.             -- do elevator things
  243.             localElevatorCurrentFloor = elevatorCurrentFloor
  244.             while true do
  245.  
  246.                 local eventData = {os.pullEvent()}
  247.                 local event = eventData[1]
  248.                 local message = eventData[5]
  249.  
  250.  
  251.                 local cum = {}
  252.  
  253.                 -- get current elevator position locally
  254.                 if event == "modem_message" then
  255.                     textThrower(14, "eleCur: " .. elevatorCurrentFloor .. " analog: " .. elevatorAnalogValue .. " maxFlr: " .. maxFloors .. "", colors.yellow , false, true)
  256.                    
  257.                     for word in (message .. " "):gmatch("(.-)" ..  " ") do
  258.                         table.insert(cum, word)
  259.                     end
  260.  
  261.                     localElevatorCurrentFloor = tonumber(cum[2])
  262.  
  263.                     textThrower(10, "Local Floor Message: " .. localElevatorCurrentFloor, colors.pink , false, true)
  264.  
  265.                 end
  266.  
  267.                 if event == "modem_message" and cum[2] == "call" then
  268.  
  269.                     elevatorAnalogValue = tonumber(cum[2])
  270.                     textThrower(12, "Updated AnalogValue For Call: " .. elevatorAnalogValue, colors.pink, false, true)
  271.                 end
  272.  
  273.                 -- get direction
  274.                 if tonumber(elevatorAnalogValue) < tonumber(localElevatorCurrentFloor) then
  275.                     textThrower(7, "Going Down From: " .. elevatorCurrentFloor .. " To: " .. elevatorAnalogValue, colors.magenta, false, true)
  276.                     redstone.setOutput("front", true)
  277.                 elseif tonumber(elevatorAnalogValue) > tonumber(localElevatorCurrentFloor) then
  278.                     textThrower(7, "Going Up From: " .. elevatorCurrentFloor .. " To: " .. elevatorAnalogValue, colors.magenta, false, true)
  279.                     redstone.setOutput("front", false)
  280.                 else
  281.                     textThrower(19, "Directional Error", colors.red, false, true)
  282.                 end
  283.  
  284.                 -- Unlock clutch
  285.                 redstone.setOutput("left", false)
  286.  
  287.                 -- Stop Elevator
  288.                 textThrower(18, localElevatorCurrentFloor .. " " .. elevatorAnalogValue, colors.yellow, false, true)
  289.  
  290.                 if tonumber(localElevatorCurrentFloor) == tonumber(elevatorAnalogValue) then
  291.                     elevatorCurrentFloor = localElevatorCurrentFloor
  292.  
  293.                     redstone.setOutput("left", true)
  294.                     sleep(0.02)
  295.                     textThrower(17, "Stopping Elevator At Floor: " .. elevatorCurrentFloor, colors.blue, false, true)
  296.  
  297.                     textThrower(8, "Arrived At Floor", colors.green, true, false)
  298.  
  299.                     local file = fs.open("elevatorMainData.txt", "w")
  300.  
  301.                     file.writeLine(importantData[1])
  302.                     file.writeLine(elevatorCurrentFloor)
  303.  
  304.                     file.close()
  305.  
  306.                     break
  307.                 end
  308.             end
  309.         end
  310.         textThrower(19, "Error on Elevator Button Press", colors.red, false, true)
  311.     end
  312. end
  313.  
  314. -- deleteData()  -- manual delete for testing
  315. requestData()
  316. readData()
  317. elevatorMain()
  318.  
  319. parallel.waitForAny(elevatorMain, deleteThing)
Add Comment
Please, Sign In to add comment