Advertisement
Mojokojo69

clock

Aug 23rd, 2023 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. -- Variables
  2. local monitor = peripheral.find("monitor")
  3. local useMonitor = false
  4.  
  5. -- Check if a monitor is connected
  6. if monitor then
  7. write("A monitor is detected. Would you like to use it? (y/n): ")
  8. useMonitor = (read() or "n"):lower() == "y"
  9. end
  10.  
  11. -- Ask for redstone default output on or off
  12. write("Should the redstone output be ON by default? (y/n): ")
  13. local defaultOutput = (read() or "n"):lower() == "y" -- Store the user's choice
  14.  
  15. -- More variables
  16. local running = true
  17. local paused = true -- Initially paused
  18. local direction = "bottom"
  19. local sides = {
  20. top = { delay = 2, active = false },
  21. bottom = { delay = 2, active = false },
  22. left = { delay = 2, active = false },
  23. right = { delay = 2, active = false },
  24. front = { delay = 2, active = false },
  25. back = { delay = 2, active = false }
  26. }
  27. -- Sync delay flag
  28. local syncDelay = false
  29. local cycles = 0 -- Counter for the number of cycles completed
  30. local targetCycles = nil -- Target number of cycles before shutdown (optional)
  31. local idleMessage1 = "- IDLE-"
  32. local idleMessage2 = "-IDLE -"
  33. local runningMessage = " ---[ RUNNING ]---"
  34.  
  35. -- Shutdown function
  36. local function shutdown()
  37. redstone.setOutput(direction, defaultOutput)
  38. if useMonitor then monitor.clear() end -- Clear the monitor if it was used
  39. print("System shutting down. Thank you for using the Auto Clock v1.0.")
  40. print("Total cycles completed this session: " .. cycles) -- Display the final cycle count
  41. end
  42.  
  43. local function printStatus()
  44. print("------ STATUS ------")
  45. print("DEFAULT STATE: " .. (defaultOutput and "ON" or "OFF"))
  46. for direction, side in pairs(sides) do
  47. print(string.upper(direction) .. " DIRECTION:")
  48. print(" ACTIVE: " .. (side.active and "ON" or "OFF"))
  49. print(" DELAY: " .. string.format("%.2f SECONDS", side.delay))
  50. end
  51. print("TARGET CYCLES: " .. (targetCycles and tostring(math.floor(targetCycles)) or "UNDEFINED"))
  52. print("CYCLES COMPLETED: " .. tostring(math.floor(cycles)))
  53. print("--------------------")
  54. end
  55.  
  56. local function printStatusMonitor()
  57. if not monitor then return end -- Exit if the monitor is not available
  58. local frame = 1
  59. while true do
  60. monitor.clear()
  61. monitor.setCursorPos(1, 1)
  62. if targetCycles then
  63. monitor.write("CYCLES:")
  64. monitor.setCursorPos(1, 2)
  65. monitor.write(tostring(math.floor(cycles)) .. "/" .. tostring(math.floor(targetCycles))) -- Remove decimal places
  66. else
  67. monitor.write("CYCLES:")
  68. monitor.setCursorPos(1, 2)
  69. monitor.write(tostring(math.floor(cycles))) -- Remove decimal places
  70. end
  71.  
  72. monitor.setCursorPos(1, 3)
  73. if paused then
  74. -- Idle Animation
  75. if frame == 1 then
  76. monitor.write(" \\ ")
  77. monitor.setCursorPos(1, 4)
  78. monitor.write(idleMessage1)
  79. monitor.setCursorPos(1, 5)
  80. monitor.write(" \\ ")
  81. elseif frame == 2 then
  82. monitor.write(" | ")
  83. monitor.setCursorPos(1, 4)
  84. monitor.write(idleMessage1)
  85. monitor.setCursorPos(1, 5)
  86. monitor.write(" | ")
  87. elseif frame == 3 then
  88. monitor.write(" / ")
  89. monitor.setCursorPos(1, 4)
  90. monitor.write(idleMessage2)
  91. monitor.setCursorPos(1, 5)
  92. monitor.write(" / ")
  93. elseif frame == 4 then
  94. monitor.write(" | ")
  95. monitor.setCursorPos(1, 4)
  96. monitor.write(idleMessage2)
  97. monitor.setCursorPos(1, 5)
  98. monitor.write(" | ")
  99. end
  100. else
  101. monitor.setCursorPos(1, 4)
  102. -- Animation for run Mode (Scrolling Message)
  103. local displayMessage = runningMessage:sub(frame, frame + 15)
  104. monitor.write(displayMessage)
  105. end
  106.  
  107. -- Increment the frame and loop back to the start if necessary
  108. frame = frame + 1
  109. if paused and frame > 4 then
  110. frame = 1
  111. elseif not paused and frame > #runningMessage then
  112. frame = 1
  113. end
  114.  
  115. sleep(0.2) -- Wait for 0.2 seconds
  116. end
  117. end
  118.  
  119. function handleExitCommand()
  120. running = false
  121. print("Exiting program. Goodbye!")
  122. shutdown()
  123. end
  124.  
  125. function handleTargetCommand()
  126. write("Enter the target number of cycles to run (or press Enter to skip): ")
  127. local newTarget = tonumber(read())
  128. if newTarget then
  129. targetCycles = newTarget -- Fixed typo here
  130. print("Target cycles changed to " .. targetCycles .. ".")
  131. printStatus()
  132. else
  133. print("INVALID TARGET. PLEASE TRY AGAIN.")
  134. end
  135. end
  136. -- Functions for Command Handling
  137. function handleStartCommand()
  138. write("Enter the target number of cycles to run (or press Enter to skip): ")
  139. targetCycles = tonumber(read()) or targetCycles
  140. paused = false
  141. print("Clock initiated. The Auto Redstone Clock is now running.")
  142. printStatus()
  143. end
  144.  
  145. function handlePauseCommand()
  146. paused = true
  147. print("Clock paused. The Auto Redstone Clock is now idle.")
  148. printStatus()
  149. end
  150.  
  151. function handleResumeCommand()
  152. paused = false
  153. print("Clock resumed. The Auto Redstone Clock is now running.")
  154. printStatus()
  155. end
  156.  
  157. function handleExitCommand()
  158. running = false
  159. print("Exiting program. Goodbye!")
  160. end
  161.  
  162. function handleResetCommand()
  163. paused = true
  164. cycles = 0
  165. print("Clock stopped. The Auto Redstone Clock is now idle.")
  166. printStatus()
  167. end
  168.  
  169. function handleDirectionCommand()
  170. write("Enter the directions to output the redstone signal (top, bottom, left, right, front, back, all): ")
  171. local inputDirections = read()
  172. local directionsList = {}
  173. if inputDirections == "all" then
  174. for direction, _ in pairs(sides) do
  175. directionsList[direction] = true
  176. end
  177. else
  178. for direction in string.gmatch(inputDirections, "%w+") do
  179. if sides[direction] then
  180. directionsList[direction] = true
  181. else
  182. print("INVALID DIRECTION: " .. direction .. ". PLEASE TRY AGAIN.")
  183. return
  184. end
  185. end
  186. end
  187.  
  188. for direction, side in pairs(sides) do
  189. side.active = directionsList[direction] or false
  190. end
  191.  
  192. print("Directions updated.")
  193. printStatus()
  194. end
  195.  
  196. function handleDelayCommand()
  197. write("Enter the direction (top, bottom, left, right, front, back, all) to set delay, or 'all' for all directions: ")
  198. local selectedDirection = read()
  199. write("Enter the delay in seconds for " .. selectedDirection .. ": ")
  200. local newDelay = tonumber(read())
  201. if newDelay then
  202. if selectedDirection == "all" then
  203. for direction, side in pairs(sides) do
  204. side.delay = newDelay
  205. end
  206. print("Delay for all sides changed to " .. newDelay .. " seconds.")
  207. elseif sides[selectedDirection] then
  208. sides[selectedDirection].delay = newDelay
  209. print("Delay for " .. string.upper(selectedDirection) .. " changed to " .. newDelay .. " seconds.")
  210. else
  211. print("INVALID DIRECTION. PLEASE TRY AGAIN.")
  212. end
  213. else
  214. print("INVALID DELAY. PLEASE TRY AGAIN.")
  215. end
  216. printStatus()
  217. end
  218.  
  219. function handleDefaultOutputCommand()
  220. write("Should the redstone output be ON by default? (y/n): ")
  221. local newDefaultOutput = (read() or "n"):lower() == "y"
  222. defaultOutput = newDefaultOutput
  223. print("Default output changed to " .. (defaultOutput and "ON" or "OFF") .. ".")
  224. printStatus()
  225. end
  226.  
  227. function handlecyclesCommand()
  228. print("Clock cycles: " .. cycles)
  229. end
  230.  
  231. function handleHelpCommand()
  232. print("AVAILABLE COMMANDS:")
  233. print(" START - OPTIONAL SET TARGET CYCLES")
  234. print(" PAUSE - SUSPEND CYCLE")
  235. print(" RESUME - ACTIVATE CYCKE")
  236. print(" RESET - RESET CYCLES")
  237. print(" STATUS - DISPLAY CURRENT STATUS")
  238. print(" DIRECTION - MODIFY SIGNAL DIRECTION")
  239. print(" DELAY - ADJUST TIME BETWEEN CYCLES")
  240. print(" DEFAULT - SET DEFAULT STATE OF SIGNAL")
  241. print(" TARGET - SET TARGET NUMBER OF CYCLES")
  242. print(" CYCLES - DISPLAY NUMBER OF CYCLES PASSED")
  243. print(" HELP - DISPLAY THIS HELP MESSAGE")
  244. print(" EXIT - TERMINATE PROGRAM")
  245. end
  246.  
  247. -- Function to handle redstone signal
  248. local function redstoneSignal()
  249. while running do
  250. if not paused then
  251. for direction, side in pairs(sides) do
  252. if side.active then
  253. redstone.setOutput(direction, not defaultOutput)
  254. sleep(side.delay)
  255. redstone.setOutput(direction, defaultOutput)
  256. sleep(side.delay)
  257. end
  258. end
  259. cycles = cycles + 1
  260. -- Check if target cycles reached
  261. if targetCycles and cycles >= targetCycles then
  262. print("TARGET CYCLES REACHED. TERMINATING PROGRAM.")
  263. running = false
  264. end
  265. else
  266. for direction, _ in pairs(sides) do
  267. redstone.setOutput(direction, defaultOutput)
  268. end
  269. sleep(1)
  270. end
  271. end
  272. end
  273.  
  274. -- Welcome message
  275. print("==========================================")
  276. print("= WELCOME TO THE AUTO CLOCK v1.0 =")
  277. print("= RARE Corp. =")
  278. print("==========================================")
  279. print("This program operates like a redstone clock.")
  280. print("Type 'start' to begin Clock.")
  281. print("Type 'pause' to pause the clock.")
  282. print("Type 'help' to see all other commands.")
  283. print("------------------------------------------")
  284. printStatus()
  285. print("------------------------------------------")
  286.  
  287. -- Start the redstone signal function in a separate thread
  288. parallel.waitForAll(
  289. redstoneSignal,
  290. printStatusMonitor,
  291. function()
  292. while running do
  293. write("ENTER COMMAND: ")
  294. local command = read()
  295.  
  296. -- Command Handling
  297. if command == "start" then handleStartCommand()
  298. elseif command == "reset" then handleResetCommand()
  299. elseif command == "pause" then handlePauseCommand()
  300. elseif command == "resume" then handleResumeCommand()
  301. elseif command == "status" then printStatus()
  302. elseif command == "direction" then handleDirectionCommand()
  303. elseif command == "default" then handleDefaultOutputCommand()
  304. elseif command == "delay" then handleDelayCommand()
  305. elseif command == "target" then handleTargetCommand()
  306. elseif command == "cycles" then handlecyclesCommand()
  307. elseif command == "exit" then handleExitCommand()
  308. elseif command == "help" then handleHelpCommand()
  309. else print("UNKNOWN COMMAND. PLEASE TRY AGAIN.") end
  310. end
  311. end
  312. )
  313.  
  314. -- Clean up and ensure redstone signal is off
  315. shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement