Advertisement
Sv443

ComputerCraft Redstone Sequencer

Nov 28th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | Gaming | 0 0
  1. -- Sequence of signals to emit
  2. -- Items are in the format {side, duration}
  3. --   Duration is in seconds and has to be a multiple of 0.1 (1 redstone tick)
  4. -- If side is set to nil, it will be treated as a pause
  5. -- If side is a table of sides, all given sides will be enabled
  6. --
  7. -- For example: The following sequence will turn the right side on for 5 redstone ticks,
  8. --   pause for 10, turn the left *and* right side on for 5, pause for 10, and repeat:
  9. SEQUENCE = {
  10.     { "top", 0.5 },
  11.     { nil, 1 },
  12.     { { "left", "right" }, 0.5 },
  13.     { nil, 1 },
  14. }
  15.  
  16. -- If this is set to a side, a redstone signal there will be required to enable the sequence
  17. -- If set to nil, the sequence will run immediately and continuously
  18. -- If a sequence is already running, it will finish first before checking for a new signal
  19. ENABLE_SIDE = "front"
  20. -- If set to true, the sequence will only be run once until the
  21. -- enable signal is toggled off and back on again
  22. ENABLE_ONCE = false
  23.  
  24. -- Set to false to invert the output signals
  25. -- Before the very first sequence has finished, the initial output state will
  26. -- be low (off) no matter this setting, but afterwards the signal will be remembered
  27. -- even after a reboot
  28. OUTPUT_ACTIVE_HIGH = true
  29.  
  30. -- Delay before starting the program *for the very first time* after a reboot
  31. -- This is useful for SP world rejoins or MP server restarts as the
  32. -- program will start before the enable signal can be read properly
  33. -- Set to nil to disable
  34. STARTUP_DELAY = 1
  35.  
  36.  
  37. local lastEnabled = false
  38. local prevEnabled = nil
  39.  
  40. function run()
  41.     print("\n| Sequencer by Sv443\n| https://github.com/Sv443/ComputerCraft-Projects\n")
  42.     if STARTUP_DELAY ~= nil then
  43.         os.sleep(STARTUP_DELAY)
  44.     end
  45.     while true do
  46.         local enabled = ENABLE_SIDE == nil and true or redstone.getInput(ENABLE_SIDE)
  47.         if prevEnabled == nil then
  48.             prevEnabled = enabled
  49.         end
  50.         if enabled ~= prevEnabled then
  51.             print("> "..(enabled and "Enabled" or "Disabled") .. " sequencer")
  52.             prevEnabled = enabled
  53.         end
  54.         if not enabled and lastEnabled then
  55.             lastEnabled = false
  56.         end
  57.         if ENABLE_SIDE == nil or enabled then
  58.             if not lastEnabled then
  59.                 if ENABLE_ONCE then
  60.                     lastEnabled = true
  61.                 end
  62.                 for _, item in pairs(SEQUENCE) do
  63.                     if item[1] ~= nil then
  64.                         if type(item[1]) == "table" then
  65.                             for _, side in pairs(item[1]) do
  66.                                 redstone.setOutput(side, OUTPUT_ACTIVE_HIGH)
  67.                             end
  68.                         else
  69.                             redstone.setOutput(item[1], OUTPUT_ACTIVE_HIGH)
  70.                         end
  71.                     end
  72.                     os.sleep(item[2])
  73.                     if item[1] ~= nil then
  74.                         if type(item[1]) == "table" then
  75.                             for _, side in pairs(item[1]) do
  76.                                 redstone.setOutput(side, not OUTPUT_ACTIVE_HIGH)
  77.                             end
  78.                         else
  79.                             redstone.setOutput(item[1], not OUTPUT_ACTIVE_HIGH)
  80.                         end
  81.                     end
  82.                 end
  83.             end
  84.         else
  85.             os.sleep(0.05)
  86.         end
  87.     end
  88. end
  89.  
  90. run()
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement