April_The_Sergal

Simple monitor controller

Jan 28th, 2026
101
0
Never
10
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | Gaming | 0 0
  1. -- ============================================
  2. -- CONFIGURATION - Edit these values as needed
  3. -- ============================================
  4. local onText = "ACTIVE"
  5. local offText = "INACTIVE"
  6. local onForeground = colors.white
  7. local onBackground = colors.green
  8. local offForeground = colors.white
  9. local offBackground = colors.red
  10.  
  11. -- Input behavior flags
  12. local inputIsLatch = false  -- true: toggles state on each pulse (for buttons)
  13.                             -- false: follows redstone state directly (for levers)
  14. local isInverted = false    -- true: redstone ON = off display, redstone OFF = on display
  15.                             -- false: redstone ON = on display, redstone OFF = off display
  16.  
  17. -- Persistence settings
  18. local persistenceFile = ".latch_state"  -- File to store latch state (hidden file)
  19.  
  20. -- ============================================
  21. -- PROGRAM CODE - No need to edit below
  22. -- ============================================
  23.  
  24. -- Wrap the monitor on the back
  25. local monitor = peripheral.wrap("back")
  26. if not monitor then
  27.     error("No monitor found on back of computer!")
  28. end
  29.  
  30. -- Track latch state
  31. local latchState = false
  32.  
  33. -- Save latch state to file
  34. local function saveState()
  35.     if not inputIsLatch then return end
  36.     local file = fs.open(persistenceFile, "w")
  37.     if file then
  38.         file.write(latchState and "true" or "false")
  39.         file.close()
  40.     end
  41. end
  42.  
  43. -- Load latch state from file
  44. local function loadState()
  45.     if not inputIsLatch then return end
  46.     if fs.exists(persistenceFile) then
  47.         local file = fs.open(persistenceFile, "r")
  48.         if file then
  49.             local content = file.readAll()
  50.             file.close()
  51.             latchState = (content == "true")
  52.         end
  53.     end
  54. end
  55.  
  56. -- Center text horizontally and vertically on monitor
  57. local function centerText(mon, text)
  58.     local w, h = mon.getSize()
  59.     local x = math.floor((w - #text) / 2) + 1
  60.     local y = math.floor(h / 2) + 1
  61.     mon.setCursorPos(x, y)
  62.     mon.write(text)
  63. end
  64.  
  65. -- Update the monitor display based on state
  66. local function updateDisplay(isOn)
  67.     if isOn then
  68.         monitor.setBackgroundColor(onBackground)
  69.         monitor.setTextColor(onForeground)
  70.     else
  71.         monitor.setBackgroundColor(offBackground)
  72.         monitor.setTextColor(offForeground)
  73.     end
  74.    
  75.     monitor.clear()
  76.     centerText(monitor, isOn and onText or offText)
  77. end
  78.  
  79. -- Get the current display state based on input and flags
  80. local function getDisplayState()
  81.     if inputIsLatch then
  82.         return isInverted and not latchState or latchState
  83.     else
  84.         local rsInput = redstone.getInput("front")
  85.         return isInverted and not rsInput or rsInput
  86.     end
  87. end
  88.  
  89. -- Load previous state and set initial display
  90. loadState()
  91. updateDisplay(getDisplayState())
  92.  
  93. -- Main loop - listen for redstone changes
  94. while true do
  95.     os.pullEvent("redstone")
  96.    
  97.     if inputIsLatch then
  98.         -- Toggle latch on rising edge only
  99.         if redstone.getInput("front") then
  100.             latchState = not latchState
  101.             saveState()  -- Persist the new state
  102.             updateDisplay(getDisplayState())
  103.         end
  104.     else
  105.         -- Direct mode - follow redstone state
  106.         updateDisplay(getDisplayState())
  107.     end
  108. end
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment