Advertisement
Sv443

ComputerCraft redstone rising & falling edge delayer

Dec 27th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.59 KB | Gaming | 0 0
  1. -- Redstone signal input side
  2. INPUT_SIDE = "left"
  3. -- Redstone signal output side
  4. OUTPUT_SIDE = "right"
  5.  
  6. -- Delay in seconds when going from low to high
  7. -- Has to be a multiple of 0.05 (1 game tick)
  8. -- 0 or nil to instantly go high
  9. HIGH_DELAY = 2
  10. -- Delay in seconds when going from high to low
  11. -- Has to be a multiple of 0.05 (1 game tick)
  12. -- 0 or nil to instantly go low
  13. LOW_DELAY = 0
  14.  
  15. -- Set to false to invert the output signal
  16. OUTPUT_ACTIVE_HIGH = true
  17.  
  18. -- Initial state of the input signal
  19. -- true = high, false = low
  20. INITIAL_STATE = false
  21.  
  22. -- Checking interval in seconds
  23. -- Has to be a multiple of 0.05 (1 game tick)
  24. CHECK_INTERVAL = 0.05
  25.  
  26. function run()
  27.     local prevState = INITIAL_STATE
  28.     print("\n| Delayer by Sv443")
  29.     print("| https://github.com/Sv443/ComputerCraft-Projects\n")
  30.  
  31.     while true do
  32.         state = redstone.getInput(INPUT_SIDE)
  33.         if state ~= prevState then
  34.             prevState = state
  35.  
  36.             if state then
  37.                 print("> State changed to HIGH, delaying "..tostring(HIGH_DELAY or 0).."s")
  38.                 if HIGH_DELAY ~= nil then
  39.                     os.sleep(HIGH_DELAY)
  40.                 end
  41.                 redstone.setOutput(OUTPUT_SIDE, OUTPUT_ACTIVE_HIGH)
  42.             else
  43.                 print("> State changed to LOW, delaying "..tostring(LOW_DELAY or 0).."s")
  44.                 if LOW_DELAY ~= nil then
  45.                     os.sleep(LOW_DELAY)
  46.                 end
  47.                 redstone.setOutput(OUTPUT_SIDE, not OUTPUT_ACTIVE_HIGH)
  48.             end
  49.         end
  50.  
  51.         os.sleep(CHECK_INTERVAL)
  52.     end
  53. end
  54.  
  55. run()
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement