Corbinhol

Elevator Floor Controller

Apr 1st, 2023 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. --Floor Controller
  2. --Version 2.5
  3. local updateCode = "4pCENLV4";
  4.  
  5. local elevatorNumberFile = fs.open("elevatorNumber.dat", "r"); --Gets the elevator number from file.
  6. local elevatorNumber = tonumber(elevatorNumberFile.readAll());
  7. elevatorNumberFile.close();
  8.  
  9. local floorNumberFile = fs.open("floorNumber.dat", "r"); --Gets the floor number from file.
  10. local floorNumber = tonumber(floorNumberFile.readAll());
  11. floorNumberFile.close();
  12.  
  13. local hostname = "floor_controller" .. floorNumber; --Ex: elevator1_controller1;
  14. os.setComputerLabel(hostname);
  15. local protocol = "ELEVATOR" .. elevatorNumber;
  16.  
  17. local stopOnThisFloor = false;
  18. local elevatorControllerAddress;
  19.  
  20. term.clear();
  21. term.setCursorPos(1,1);
  22. local uptime = 0;
  23.  
  24. local doorStatus = true;
  25.  
  26. function formatTime()
  27.     local days = math.floor(uptime/86400)
  28.     local hours = math.floor((uptime % 86400)/3600)
  29.     local minutes = math.floor((uptime % 3600)/60)
  30.     local seconds = math.floor((uptime % 60))
  31.     return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
  32. end
  33.  
  34. function log(inputText)
  35.     print("[" .. formatTime(uptime) .. "] " .. inputText);
  36. end
  37.  
  38. function update()
  39.     shell.run("pastebin", "get", updateCode, "download");
  40.     if fs.exists("download") then
  41.         fs.delete("startup.lua");
  42.         fs.copy("download", "startup.lua");
  43.         fs.delete("download");
  44.         return true;
  45.     else
  46.         return false;
  47.     end
  48. end
  49.  
  50. function send(address, message)
  51.     rednet.send(address, message, protocol);
  52. end
  53.  
  54. --Controller Functions
  55. function openDoor()
  56.     redstone.setOutput("front", false);
  57. end
  58.  
  59. function closeDoor()
  60.     redstone.setOutput("front", true);
  61. end
  62.  
  63. function ding() --Ding the bell
  64.     redstone.setOutput("back", true);
  65.     sleep(0.3);
  66.     redstone.setOutput("back", false)
  67. end
  68.  
  69. function checkElevator() --Check to see if elevator is on floor.
  70.     return redstone.getInput("left");
  71. end
  72.  
  73. --Looping Functions
  74. function countTime() --Function keeping track of uptime.
  75.     while true do
  76.         uptime = uptime + 1;
  77.         sleep(1);
  78.     end
  79. end
  80.  
  81. function clean()
  82.     log("Resetting Floor.")
  83.     if checkElevator() then
  84.         stopOnThisFloor = true;
  85.         lock();
  86.     else
  87.         stopOnThisFloor = false;
  88.         unlock();
  89.     end
  90. end
  91.  
  92. function lock()
  93.     log("Locking Elevator.");
  94.     redstone.setOutput("right", true);
  95.     sleep(0.5);
  96.     ding();
  97.     sleep(0.3);
  98.     openDoor();
  99.     if elevatorControllerAddress ~= nil then
  100.         send(elevatorControllerAddress, "locked");
  101.     end
  102. end
  103.  
  104. function unlock()
  105.     log("Unlocking Elevator.")
  106.     ding();
  107.     sleep(0.3);
  108.     closeDoor();    
  109.     sleep(1);
  110.     redstone.setOutput("right", false);
  111. end
  112.  
  113.  
  114. function network()
  115.     rednet.open("top");
  116.     rednet.host(protocol, hostname);
  117.     if fs.exists("elevatorControllerAddress.dat") then --gets the elevator controller address from file.
  118.         local file = fs.open("elevatorControllerAddress.dat", "r");
  119.         elevatorControllerAddress = tonumber(file.readAll());
  120.         file.close();
  121.         log("Loaded Elevator Controller Address.")
  122.     else
  123.         while true do
  124.             log("Unable to find Elevator Controller Address Saved...Searching...");
  125.             while true do
  126.                 elevatorControllerAddress = rednet.lookup(protocol, "elevator_controller");
  127.                 if elevatorControllerAddress ~= nil then
  128.                     log("Found.");
  129.                     local file = fs.open("elevatorControllerAddress.dat", "w");
  130.                     file.write(elevatorControllerAddress);
  131.                     file.close();
  132.                     break;
  133.                 end
  134.             end
  135.         end
  136.     end
  137.     while true do
  138.         local from, message = rednet.receive(protocol);
  139.         if message == "status" then
  140.             send(from, true);
  141.         elseif message == "doorStatus" then
  142.             send(from, doorStatus);
  143.         elseif message == "stop on floor" then
  144.             log("Requested to stop elevator on floor. Accepting.")
  145.             ding();
  146.             send(from, true);
  147.             stopOnThisFloor = true;
  148.         elseif message == "no longer on floor" then
  149.             log("Requested to release elevator. Accepting.")
  150.             send(from, true);
  151.             stopOnThisFloor = false;
  152.         elseif message == "is on floor?" then
  153.             log("Requested information on floor. Sending.")
  154.             send(from, checkElevator());
  155.         elseif message == "update" then
  156.             send(from, true);
  157.             log("Recieved command to update. Updating.");
  158.             if update() then
  159.                 send(from, true);
  160.                 os.reboot();
  161.             else
  162.                 send(from, false);
  163.             end
  164.         elseif message == "reset" then
  165.             send(from, true);
  166.             clean();
  167.         elseif message == "get door status" then
  168.             send(from, stopOnThisFloor);
  169.         elseif message == "restart" then
  170.             send(from, true);
  171.             os.reboot();
  172.         end
  173.     end
  174. end
  175.  
  176. function main()
  177.     clean();
  178.     while true do
  179.         if stopOnThisFloor then
  180.             if checkElevator() then --Checks to see if elevator is on this floor
  181.                 if redstone.getOutput("right") == false then
  182.                     log("Elevator On Floor. Locking");
  183.                     lock(); --Lock the elevator.
  184.                 end
  185.             else
  186.                 if redstone.getOutput("right") == true then
  187.                     log("Unlocking Elevator because not on floor yet.")
  188.                     unlock();
  189.                 end
  190.             end
  191.         else
  192.             if redstone.getOutput("right") == true then
  193.                 log("Elevator set to move. Unlocking");
  194.                 unlock(); --If Elevator is Not on this floor, but it needs to be on this floor, unlock it.
  195.             end
  196.         end
  197.         sleep(0);
  198.     end
  199. end
  200.  
  201. parallel.waitForAll(network, main, countTime);
Advertisement
Add Comment
Please, Sign In to add comment