AndyE7

Elevator Computer

Jul 11th, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. os.loadAPI("button")
  2.  
  3. function setupMonitor()
  4.  
  5. local monitor = peripheral.find("monitor")
  6.  
  7. monitor.setBackgroundColor(colours.grey)
  8. monitor.clear()
  9. monitor.setCursorPos(1,1)
  10.  
  11. return monitor
  12.  
  13. end
  14.  
  15. function monitorWrite(monitor, numOfButtons, openModem, currentFloor)
  16.  
  17. button.setMonitor(monitor)
  18.  
  19. local buttonList = createButtonList(monitor, numOfButtons)
  20.  
  21. while true do
  22. local value = button.await(buttonList)
  23.  
  24. if value ~= nil then
  25. print("sending to " .. value)
  26.  
  27. openModem.transmit(6968, 0, value)
  28. end
  29.  
  30. end
  31.  
  32. end
  33.  
  34.  
  35. function createButtonList(monitor, numOfButtons)
  36.  
  37. button.setMonitor(monitor)
  38.  
  39. local buttonList = {}
  40. local pos = {0, 1}
  41.  
  42. for i = 1, numOfButtons do
  43. local buttonMessage
  44.  
  45. if pos[1] > 0 then
  46. pos[1] = pos[1] + 10
  47. else
  48. pos[1] = 6
  49. end
  50.  
  51. if i < 10 then
  52. buttonMessage = ("Floor 0" .. i)
  53. else
  54. buttonMessage = ("Floor " .. i)
  55. end
  56.  
  57. local newButton = button.create(buttonMessage)
  58. newButton.setPos(pos[1], pos[2])
  59. newButton.setColor(colours.lightGrey)
  60. newButton.setBlinkColor(colours.blue)
  61. newButton.onClickReturn(
  62. i
  63. )
  64.  
  65. table.insert(buttonList, newButton)
  66.  
  67. if i % 2 == 0 then
  68.  
  69. pos = {0, pos[2]+2}
  70.  
  71. end
  72.  
  73. end
  74.  
  75. return buttonList
  76.  
  77. end
  78.  
  79. --Start of script
  80.  
  81. local numOfFloors
  82.  
  83. if fs.exists("numOfFloors") then
  84. local file = fs.open("numOfFloors", "r")
  85. numOfFloors = textutils.unserialize(file.readAll())
  86. else
  87.  
  88. print("Enter Number Of Floors")
  89. numOfFloors = read()
  90.  
  91. local file = fs.open("numOfFloors", "w")
  92. file.write(textutils.serialize(numOfFloors))
  93. file.close()
  94.  
  95. end
  96.  
  97.  
  98.  
  99. local monitor = setupMonitor()
  100. local openModem = peripheral.find("modem") or error("No modem attached", 0)
  101.  
  102. monitorWrite(monitor, numOfFloors, openModem)
Advertisement
Add Comment
Please, Sign In to add comment