Advertisement
Corbinhol

Elevator System

Sep 13th, 2022 (edited)
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. --Elevator Program
  2.  
  3. local elevatorPosition;
  4. term.clear();
  5. term.setCursorPos(1,1);
  6.  
  7. local canLower = true;
  8. local elevatorStatus = "Idle";
  9. local protocol = "API";
  10.  
  11. local chat = peripheral.wrap("left");
  12. rednet.host(protocol, "elevator");
  13.  
  14. rednet.open("top");
  15.  
  16. function lowerElevator()
  17.     redstone.setOutput("right", true)
  18.     elevatorPosition = 0;
  19. end
  20.  
  21. function raiseElevator()
  22.     redstone.setOutput("right", false)
  23.     elevatorPosition = 1;
  24. end
  25.  
  26. function checkInput()
  27.     return redstone.getInput("bottom");
  28. end
  29.  
  30. print("Starting Elevator System.");
  31. print("Raising Elevator...")
  32. raiseElevator();
  33. sleep(15)
  34.  
  35.  
  36.  
  37. function checkForMessage()
  38.     while true do
  39.         local from, message = rednet.receive(protocol);
  40.         if message == "lowerElevator" and canLower then
  41.             canLower = false;
  42.             print("Recieved Signal");
  43.             print("Elevator Currently on Upper Level. Lowering...")
  44.             elevatorStatus = "Lowering...";
  45.             lowerElevator()
  46.             sleep(13);
  47.             print("Elevator Lowered to bottom level. Will Raise in 5 Seconds...")
  48.             elevatorStatus = "Waiting...";
  49.             sleep(5);
  50.             elevatorStatus = "Raising...";
  51.             print("Raising Elevator.")
  52.             raiseElevator();
  53.             sleep(13);
  54.             canLower = true;
  55.             elevatorStatus = "Idle";
  56.         end
  57.     end
  58.  
  59. end
  60.  
  61. function api()
  62.     while true do
  63.         local from, message = rednet.receive(protocol);
  64.         if message == "getData" then
  65.             rednet.send(from, elevatorStatus, protocol);
  66.         end
  67.     end
  68. end
  69.  
  70. function checkForChat()
  71.     while true do
  72.         local _, name, chat = os.pullEvent("chat");
  73.         if chat == "elevator" and canLower then
  74.             canLower = false;
  75.             print("Recieved Signal");
  76.             print("Elevator Currently on Upper Level. Lowering...")
  77.             elevatorStatus = "Lowering...";
  78.             lowerElevator()
  79.             sleep(13);
  80.             print("Elevator Lowered to bottom level. Will Raise in 5 Seconds...")
  81.             elevatorStatus = "Waiting...";
  82.             sleep(5);
  83.             print("Raising Elevator.")
  84.             elevatorStatus = "Raising...";
  85.             raiseElevator();
  86.             sleep(13);
  87.             canLower = true;
  88.             elevatorStatus = "Idle";
  89.         end
  90.     end
  91. end
  92.  
  93. function checkForRedstone()
  94.     while true do
  95.         if checkInput() == true and canLower then
  96.             if elevatorPosition == 1 then
  97.                 canLower = false;
  98.                 print("Recieved Signal");
  99.                 print("Elevator Currently on Upper Level. Lowering...")
  100.                 elevatorStatus = "Lowering...";
  101.                 lowerElevator()
  102.                 sleep(13);
  103.                 print("Elevator Lowered to bottom level. Will Raise in 5 Seconds...")
  104.                 elevatorStatus = "Waiting...";
  105.                 sleep(5);
  106.                 print("Raising Elevator.")
  107.                 elevatorStatus = "Raising...";
  108.                 raiseElevator();
  109.                 sleep(13);
  110.                 canLower = true;
  111.                 elevatorStatus = "Idle";
  112.             end
  113.         end
  114.         sleep(0);
  115.     end
  116. end
  117.  
  118. parallel.waitForAny(checkForChat, checkForRedstone, api, checkForMessage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement