Advertisement
Corbinhol

Elevator Light Controller

Apr 3rd, 2023 (edited)
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | None | 0 0
  1. --Light Controller
  2. --Version 2.4
  3. local updateCode = "jUbwg75D";
  4.  
  5. local status = "error";
  6.  
  7. function update()
  8.     shell.run("pastebin", "get", updateCode, "download");
  9.     if fs.exists("download") then
  10.         fs.delete("startup.lua");
  11.         fs.copy("download", "startup.lua");
  12.         fs.delete("download");
  13.         return true;
  14.     else
  15.         return false;
  16.     end
  17. end
  18.  
  19. function ElevatorErrorBlink()
  20.     redstone.setOutput("left", false);
  21.     redstone.setOutput("right", false);
  22.     sleep(.1);
  23.     redstone.setOutput("bottom", true);
  24.     sleep(.5);
  25.     redstone.setOutput("bottom", false);
  26.     sleep(.1);
  27.     redstone.setOutput("left", true);
  28.     redstone.setOutput("right", true);
  29.     sleep(.5);
  30. end
  31.  
  32. function elevatorMoveUp()
  33.     redstone.setOutput("left", true);
  34.     sleep(.5);
  35.     redstone.setOutput("left", false);
  36.     sleep(.2);
  37.     redstone.setOutput("bottom", true);
  38.     sleep(.5);
  39.     redstone.setOutput("bottom", false);
  40.     sleep(.2);
  41.     redstone.setOutput("right", true);
  42.     sleep(.5);
  43.     redstone.setOutput("right", false);
  44.     sleep(.2);
  45. end
  46.  
  47. function elevatorMoveDown()
  48.     redstone.setOutput("right", true);
  49.     sleep(.5);
  50.     redstone.setOutput("right", false);
  51.     sleep(.2);
  52.     redstone.setOutput("bottom", true);
  53.     sleep(.5);
  54.     redstone.setOutput("bottom", false);
  55.     sleep(.2);
  56.     redstone.setOutput("left", true);
  57.     sleep(.5);
  58.     redstone.setOutput("left", false);
  59.     sleep(.2);
  60. end
  61.  
  62. function elevatorAtCurrentFloor()
  63.     redstone.setOutput("right", true);
  64.     redstone.setOutput("bottom", true);
  65.     redstone.setOutput("left", true);
  66.     sleep(.3);
  67. end
  68.  
  69. function clearLights()
  70.     redstone.setOutput("left", false);
  71.     redstone.setOutput("bottom", false);
  72.     redstone.setOutput("right", false);
  73.     sleep(.3);
  74. end
  75.  
  76. local elevatorNumberFile = fs.open("elevatorNumber.dat", "r"); --Gets the elevator number from file.
  77. local elevatorNumber = tonumber(elevatorNumberFile.readAll());
  78. elevatorNumberFile.close();
  79.  
  80. local floorNumberFile = fs.open("floorNumber.dat", "r"); --Gets the floor number from file.
  81. local floorNumber = tonumber(floorNumberFile.readAll());
  82. floorNumberFile.close();
  83.  
  84. local protocol = "ELEVATOR" .. elevatorNumber;
  85. local hostname = "light_controller" .. floorNumber;
  86.  
  87. local switch = false;
  88.  
  89. function network()
  90.     rednet.open("top");
  91.     rednet.host(protocol, hostname);
  92.     while true do
  93.         local from, message = rednet.receive(protocol);
  94.         if message == "error" then
  95.             status = "error";
  96.             switch = true;
  97.             rednet.send(from, true, protocol)
  98.         elseif message == "raising" then
  99.             status = "raising";
  100.             switch = true;
  101.             rednet.send(from, true, protocol)
  102.         elseif message == "lowering" then
  103.             status = "lowering";
  104.             switch = true;
  105.             rednet.send(from, true, protocol)
  106.         elseif message == "stopped" then
  107.             status = "stopped";
  108.             switch = true;
  109.             rednet.send(from, true, protocol)
  110.         elseif message == "at floor" then
  111.             status = "at floor"
  112.             switch = true;
  113.             rednet.send(from, true, protocol)
  114.         elseif message == "status" then
  115.             rednet.send(from, true, protocol)
  116.         elseif message == "update" then
  117.             if update() then
  118.                 rednet.send(from, true, protocol);
  119.                 rednet.send(from, update(), protocol);
  120.                 os.reboot();
  121.             end
  122.         elseif message == "restart" then
  123.             rednet.send(from, true, protocol);
  124.             os.reboot();
  125.         end
  126.     end
  127. end
  128.  
  129. function operateLights()
  130.     clearLights();
  131.     while true do
  132.         if status == "error" then
  133.             ElevatorErrorBlink();
  134.         elseif status == "raising" then
  135.             elevatorMoveUp();
  136.         elseif status == "lowering" then
  137.             elevatorMoveDown();
  138.         elseif status == "at floor" then
  139.             elevatorAtCurrentFloor();
  140.         elseif status == "stopped" then
  141.             clearLights();
  142.             sleep(.1);
  143.         end
  144.  
  145.         if switch then
  146.             clearLights();
  147.             switch = false;
  148.         end
  149.     end
  150. end
  151.  
  152. parallel.waitForAll(operateLights, network);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement