Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local monitor = peripheral.find("monitor")
- local useMonitor = false
- -- Check if a monitor is connected
- if monitor then
- write("A monitor is detected. Would you like to use it? (y/n): ")
- useMonitor = (read() or "n"):lower() == "y"
- end
- -- Ask for redstone default output on or off
- write("Should the redstone output be ON by default? (y/n): ")
- local defaultOutput = (read() or "n"):lower() == "y" -- Store the user's choice
- -- More variables
- local running = true
- local paused = true -- Initially paused
- local direction = "bottom"
- local sides = {
- top = { delay = 2, active = false },
- bottom = { delay = 2, active = false },
- left = { delay = 2, active = false },
- right = { delay = 2, active = false },
- front = { delay = 2, active = false },
- back = { delay = 2, active = false }
- }
- -- Sync delay flag
- local syncDelay = false
- local cycles = 0 -- Counter for the number of cycles completed
- local targetCycles = nil -- Target number of cycles before shutdown (optional)
- local idleMessage1 = "- IDLE-"
- local idleMessage2 = "-IDLE -"
- local runningMessage = " ---[ RUNNING ]---"
- -- Shutdown function
- local function shutdown()
- redstone.setOutput(direction, defaultOutput)
- if useMonitor then monitor.clear() end -- Clear the monitor if it was used
- print("System shutting down. Thank you for using the Auto Clock v1.0.")
- print("Total cycles completed this session: " .. cycles) -- Display the final cycle count
- end
- local function printStatus()
- print("------ STATUS ------")
- print("DEFAULT STATE: " .. (defaultOutput and "ON" or "OFF"))
- for direction, side in pairs(sides) do
- print(string.upper(direction) .. " DIRECTION:")
- print(" ACTIVE: " .. (side.active and "ON" or "OFF"))
- print(" DELAY: " .. string.format("%.2f SECONDS", side.delay))
- end
- print("TARGET CYCLES: " .. (targetCycles and tostring(math.floor(targetCycles)) or "UNDEFINED"))
- print("CYCLES COMPLETED: " .. tostring(math.floor(cycles)))
- print("--------------------")
- end
- local function printStatusMonitor()
- if not monitor then return end -- Exit if the monitor is not available
- local frame = 1
- while true do
- monitor.clear()
- monitor.setCursorPos(1, 1)
- if targetCycles then
- monitor.write("CYCLES:")
- monitor.setCursorPos(1, 2)
- monitor.write(tostring(math.floor(cycles)) .. "/" .. tostring(math.floor(targetCycles))) -- Remove decimal places
- else
- monitor.write("CYCLES:")
- monitor.setCursorPos(1, 2)
- monitor.write(tostring(math.floor(cycles))) -- Remove decimal places
- end
- monitor.setCursorPos(1, 3)
- if paused then
- -- Idle Animation
- if frame == 1 then
- monitor.write(" \\ ")
- monitor.setCursorPos(1, 4)
- monitor.write(idleMessage1)
- monitor.setCursorPos(1, 5)
- monitor.write(" \\ ")
- elseif frame == 2 then
- monitor.write(" | ")
- monitor.setCursorPos(1, 4)
- monitor.write(idleMessage1)
- monitor.setCursorPos(1, 5)
- monitor.write(" | ")
- elseif frame == 3 then
- monitor.write(" / ")
- monitor.setCursorPos(1, 4)
- monitor.write(idleMessage2)
- monitor.setCursorPos(1, 5)
- monitor.write(" / ")
- elseif frame == 4 then
- monitor.write(" | ")
- monitor.setCursorPos(1, 4)
- monitor.write(idleMessage2)
- monitor.setCursorPos(1, 5)
- monitor.write(" | ")
- end
- else
- monitor.setCursorPos(1, 4)
- -- Animation for run Mode (Scrolling Message)
- local displayMessage = runningMessage:sub(frame, frame + 15)
- monitor.write(displayMessage)
- end
- -- Increment the frame and loop back to the start if necessary
- frame = frame + 1
- if paused and frame > 4 then
- frame = 1
- elseif not paused and frame > #runningMessage then
- frame = 1
- end
- sleep(0.2) -- Wait for 0.2 seconds
- end
- end
- function handleExitCommand()
- running = false
- print("Exiting program. Goodbye!")
- shutdown()
- end
- function handleTargetCommand()
- write("Enter the target number of cycles to run (or press Enter to skip): ")
- local newTarget = tonumber(read())
- if newTarget then
- targetCycles = newTarget -- Fixed typo here
- print("Target cycles changed to " .. targetCycles .. ".")
- printStatus()
- else
- print("INVALID TARGET. PLEASE TRY AGAIN.")
- end
- end
- -- Functions for Command Handling
- function handleStartCommand()
- write("Enter the target number of cycles to run (or press Enter to skip): ")
- targetCycles = tonumber(read()) or targetCycles
- paused = false
- print("Clock initiated. The Auto Redstone Clock is now running.")
- printStatus()
- end
- function handlePauseCommand()
- paused = true
- print("Clock paused. The Auto Redstone Clock is now idle.")
- printStatus()
- end
- function handleResumeCommand()
- paused = false
- print("Clock resumed. The Auto Redstone Clock is now running.")
- printStatus()
- end
- function handleExitCommand()
- running = false
- print("Exiting program. Goodbye!")
- end
- function handleResetCommand()
- paused = true
- cycles = 0
- print("Clock stopped. The Auto Redstone Clock is now idle.")
- printStatus()
- end
- function handleDirectionCommand()
- write("Enter the directions to output the redstone signal (top, bottom, left, right, front, back, all): ")
- local inputDirections = read()
- local directionsList = {}
- if inputDirections == "all" then
- for direction, _ in pairs(sides) do
- directionsList[direction] = true
- end
- else
- for direction in string.gmatch(inputDirections, "%w+") do
- if sides[direction] then
- directionsList[direction] = true
- else
- print("INVALID DIRECTION: " .. direction .. ". PLEASE TRY AGAIN.")
- return
- end
- end
- end
- for direction, side in pairs(sides) do
- side.active = directionsList[direction] or false
- end
- print("Directions updated.")
- printStatus()
- end
- function handleDelayCommand()
- write("Enter the direction (top, bottom, left, right, front, back, all) to set delay, or 'all' for all directions: ")
- local selectedDirection = read()
- write("Enter the delay in seconds for " .. selectedDirection .. ": ")
- local newDelay = tonumber(read())
- if newDelay then
- if selectedDirection == "all" then
- for direction, side in pairs(sides) do
- side.delay = newDelay
- end
- print("Delay for all sides changed to " .. newDelay .. " seconds.")
- elseif sides[selectedDirection] then
- sides[selectedDirection].delay = newDelay
- print("Delay for " .. string.upper(selectedDirection) .. " changed to " .. newDelay .. " seconds.")
- else
- print("INVALID DIRECTION. PLEASE TRY AGAIN.")
- end
- else
- print("INVALID DELAY. PLEASE TRY AGAIN.")
- end
- printStatus()
- end
- function handleDefaultOutputCommand()
- write("Should the redstone output be ON by default? (y/n): ")
- local newDefaultOutput = (read() or "n"):lower() == "y"
- defaultOutput = newDefaultOutput
- print("Default output changed to " .. (defaultOutput and "ON" or "OFF") .. ".")
- printStatus()
- end
- function handlecyclesCommand()
- print("Clock cycles: " .. cycles)
- end
- function handleHelpCommand()
- print("AVAILABLE COMMANDS:")
- print(" START - OPTIONAL SET TARGET CYCLES")
- print(" PAUSE - SUSPEND CYCLE")
- print(" RESUME - ACTIVATE CYCKE")
- print(" RESET - RESET CYCLES")
- print(" STATUS - DISPLAY CURRENT STATUS")
- print(" DIRECTION - MODIFY SIGNAL DIRECTION")
- print(" DELAY - ADJUST TIME BETWEEN CYCLES")
- print(" DEFAULT - SET DEFAULT STATE OF SIGNAL")
- print(" TARGET - SET TARGET NUMBER OF CYCLES")
- print(" CYCLES - DISPLAY NUMBER OF CYCLES PASSED")
- print(" HELP - DISPLAY THIS HELP MESSAGE")
- print(" EXIT - TERMINATE PROGRAM")
- end
- -- Function to handle redstone signal
- local function redstoneSignal()
- while running do
- if not paused then
- for direction, side in pairs(sides) do
- if side.active then
- redstone.setOutput(direction, not defaultOutput)
- sleep(side.delay)
- redstone.setOutput(direction, defaultOutput)
- sleep(side.delay)
- end
- end
- cycles = cycles + 1
- -- Check if target cycles reached
- if targetCycles and cycles >= targetCycles then
- print("TARGET CYCLES REACHED. TERMINATING PROGRAM.")
- running = false
- end
- else
- for direction, _ in pairs(sides) do
- redstone.setOutput(direction, defaultOutput)
- end
- sleep(1)
- end
- end
- end
- -- Welcome message
- print("==========================================")
- print("= WELCOME TO THE AUTO CLOCK v1.0 =")
- print("= RARE Corp. =")
- print("==========================================")
- print("This program operates like a redstone clock.")
- print("Type 'start' to begin Clock.")
- print("Type 'pause' to pause the clock.")
- print("Type 'help' to see all other commands.")
- print("------------------------------------------")
- printStatus()
- print("------------------------------------------")
- -- Start the redstone signal function in a separate thread
- parallel.waitForAll(
- redstoneSignal,
- printStatusMonitor,
- function()
- while running do
- write("ENTER COMMAND: ")
- local command = read()
- -- Command Handling
- if command == "start" then handleStartCommand()
- elseif command == "reset" then handleResetCommand()
- elseif command == "pause" then handlePauseCommand()
- elseif command == "resume" then handleResumeCommand()
- elseif command == "status" then printStatus()
- elseif command == "direction" then handleDirectionCommand()
- elseif command == "default" then handleDefaultOutputCommand()
- elseif command == "delay" then handleDelayCommand()
- elseif command == "target" then handleTargetCommand()
- elseif command == "cycles" then handlecyclesCommand()
- elseif command == "exit" then handleExitCommand()
- elseif command == "help" then handleHelpCommand()
- else print("UNKNOWN COMMAND. PLEASE TRY AGAIN.") end
- end
- end
- )
- -- Clean up and ensure redstone signal is off
- shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement