Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Light Controller
- --Version 2.4
- local updateCode = "jUbwg75D";
- local status = "error";
- function update()
- shell.run("pastebin", "get", updateCode, "download");
- if fs.exists("download") then
- fs.delete("startup.lua");
- fs.copy("download", "startup.lua");
- fs.delete("download");
- return true;
- else
- return false;
- end
- end
- function ElevatorErrorBlink()
- redstone.setOutput("left", false);
- redstone.setOutput("right", false);
- sleep(.1);
- redstone.setOutput("bottom", true);
- sleep(.5);
- redstone.setOutput("bottom", false);
- sleep(.1);
- redstone.setOutput("left", true);
- redstone.setOutput("right", true);
- sleep(.5);
- end
- function elevatorMoveUp()
- redstone.setOutput("left", true);
- sleep(.5);
- redstone.setOutput("left", false);
- sleep(.2);
- redstone.setOutput("bottom", true);
- sleep(.5);
- redstone.setOutput("bottom", false);
- sleep(.2);
- redstone.setOutput("right", true);
- sleep(.5);
- redstone.setOutput("right", false);
- sleep(.2);
- end
- function elevatorMoveDown()
- redstone.setOutput("right", true);
- sleep(.5);
- redstone.setOutput("right", false);
- sleep(.2);
- redstone.setOutput("bottom", true);
- sleep(.5);
- redstone.setOutput("bottom", false);
- sleep(.2);
- redstone.setOutput("left", true);
- sleep(.5);
- redstone.setOutput("left", false);
- sleep(.2);
- end
- function elevatorAtCurrentFloor()
- redstone.setOutput("right", true);
- redstone.setOutput("bottom", true);
- redstone.setOutput("left", true);
- sleep(.3);
- end
- function clearLights()
- redstone.setOutput("left", false);
- redstone.setOutput("bottom", false);
- redstone.setOutput("right", false);
- sleep(.3);
- end
- local elevatorNumberFile = fs.open("elevatorNumber.dat", "r"); --Gets the elevator number from file.
- local elevatorNumber = tonumber(elevatorNumberFile.readAll());
- elevatorNumberFile.close();
- local floorNumberFile = fs.open("floorNumber.dat", "r"); --Gets the floor number from file.
- local floorNumber = tonumber(floorNumberFile.readAll());
- floorNumberFile.close();
- local protocol = "ELEVATOR" .. elevatorNumber;
- local hostname = "light_controller" .. floorNumber;
- local switch = false;
- function network()
- rednet.open("top");
- rednet.host(protocol, hostname);
- while true do
- local from, message = rednet.receive(protocol);
- if message == "error" then
- status = "error";
- switch = true;
- rednet.send(from, true, protocol)
- elseif message == "raising" then
- status = "raising";
- switch = true;
- rednet.send(from, true, protocol)
- elseif message == "lowering" then
- status = "lowering";
- switch = true;
- rednet.send(from, true, protocol)
- elseif message == "stopped" then
- status = "stopped";
- switch = true;
- rednet.send(from, true, protocol)
- elseif message == "at floor" then
- status = "at floor"
- switch = true;
- rednet.send(from, true, protocol)
- elseif message == "status" then
- rednet.send(from, true, protocol)
- elseif message == "update" then
- if update() then
- rednet.send(from, true, protocol);
- rednet.send(from, update(), protocol);
- os.reboot();
- end
- elseif message == "restart" then
- rednet.send(from, true, protocol);
- os.reboot();
- end
- end
- end
- function operateLights()
- clearLights();
- while true do
- if status == "error" then
- ElevatorErrorBlink();
- elseif status == "raising" then
- elevatorMoveUp();
- elseif status == "lowering" then
- elevatorMoveDown();
- elseif status == "at floor" then
- elevatorAtCurrentFloor();
- elseif status == "stopped" then
- clearLights();
- sleep(.1);
- end
- if switch then
- clearLights();
- switch = false;
- end
- end
- end
- parallel.waitForAll(operateLights, network);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement