Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mekanism Induction Matrix Monitor + Redstone Hysteresis
- -- CC:Tweaked 1.21.1
- -- Auto-detects matrix, monitor, and redstone relay
- -- Redstone ON below 10%, OFF above 90%
- -- === Detect Peripherals ===
- local modem = peripheral.find("modem")
- if not modem then
- print("❌ No wired modem found on this computer.")
- return
- end
- local peripherals = peripheral.getNames()
- local matrixName, redstoneName = nil, nil
- for _, name in ipairs(peripherals) do
- if name:find("inductionPort") or name:find("inductionMatrix") then
- matrixName = name
- elseif name:find("redstoneIntegrator") or name:find("redstoneRelay") then
- redstoneName = name
- end
- end
- if not matrixName then
- print("❌ No Mekanism Induction Matrix found on the network.")
- return
- end
- local matrix = peripheral.wrap(matrixName)
- local redstone = redstoneName and peripheral.wrap(redstoneName) or nil
- local monitor = peripheral.find("monitor")
- if not monitor then
- print("❌ No monitor found.")
- return
- end
- monitor.setTextScale(1)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- === Helper Functions ===
- local function drawBar(mon, y, percent, width)
- local filled = math.floor(width * percent / 100)
- local color = colors.green
- if percent < 25 then color = colors.red
- elseif percent < 60 then color = colors.yellow end
- mon.setCursorPos(1, y)
- mon.setBackgroundColor(colors.gray)
- mon.write(string.rep(" ", width))
- mon.setCursorPos(1, y)
- mon.setBackgroundColor(color)
- mon.write(string.rep(" ", filled))
- mon.setBackgroundColor(colors.black)
- end
- local monX, monY = monitor.getSize()
- local function centerText(y, text)
- local x = math.floor((monX - #text) / 2)
- if x < 1 then x = 1 end
- monitor.setCursorPos(x, y)
- monitor.write(text)
- end
- -- === Configurable Thresholds ===
- local lowThreshold = 10 -- % to turn ON redstone
- local highThreshold = 90 -- % to turn OFF redstone
- local redstoneSide = "back" -- side of the redstone integrator output
- local redstoneState = false
- -- === Main Loop ===
- while true do
- local energy = matrix.getEnergy() or 0
- local maxEnergy = matrix.getMaxEnergy() or 1
- local input = matrix.getLastInput and matrix.getLastInput() or 0
- local output = matrix.getLastOutput and matrix.getLastOutput() or 0
- local percent = (energy / maxEnergy) * 100
- -- === Hysteresis Control Logic ===
- if redstone then
- if percent < lowThreshold and not redstoneState then
- redstone.setOutput(redstoneSide, true)
- redstoneState = true
- elseif percent > highThreshold and redstoneState then
- redstone.setOutput(redstoneSide, false)
- redstoneState = false
- end
- end
- -- === Display ===
- monitor.clear()
- monitor.setTextColor(colors.white)
- centerText(1, "⚡ Mekanism Induction Matrix ⚡")
- centerText(2, string.rep("-", monX))
- monitor.setCursorPos(2, 4)
- monitor.write(string.format("Stored: %.2f MFE", energy / 1e6))
- monitor.setCursorPos(2, 5)
- monitor.write(string.format("Capacity: %.2f MFE", maxEnergy / 1e6))
- monitor.setCursorPos(2, 6)
- monitor.write(string.format("Charge: %.1f%%", percent))
- monitor.setCursorPos(2, 7)
- monitor.write(string.format("Input: %.2f kFE/t", input / 1e3))
- monitor.setCursorPos(2, 8)
- monitor.write(string.format("Output: %.2f kFE/t", output / 1e3))
- drawBar(monitor, 10, percent, monX)
- if redstone then
- monitor.setCursorPos(2, 12)
- if redstoneState then
- monitor.setTextColor(colors.red)
- monitor.write(string.format("⚠️ LOW POWER (%.1f%%) — Redstone ON ⚠️", percent))
- else
- monitor.setTextColor(colors.green)
- monitor.write(string.format("Power Stable (%.1f%%) — Redstone OFF", percent))
- end
- else
- monitor.setCursorPos(2, 12)
- monitor.setTextColor(colors.yellow)
- monitor.write("No redstone relay found on network.")
- end
- monitor.setTextColor(colors.white)
- centerText(monY, "⟳ Updating every 2s...")
- sleep(2)
- end
Advertisement
Add Comment
Please, Sign In to add comment