Advertisement
zamoth

energy alarm

Jul 5th, 2025 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.40 KB | None | 0 0
  1. -- Peripheral setup
  2. local modemSide   = "bottom"
  3. local speakerSide = "top"
  4. local lightSide   = "front"
  5.  
  6. local thresholdPct   = 3     -- Set your threshold here (percent, 0-100)
  7. local flickerTime    = 0.5   -- seconds for lamp flicker
  8. local alarmRepeat    = 2     -- seconds between alarm beats
  9.  
  10. local speaker = peripheral.wrap(speakerSide)
  11. if not speaker then error("Speaker not found on top!") end
  12.  
  13. rednet.open(modemSide)
  14.  
  15. redstone.setOutput(lightSide, false)
  16. local alarmActive   = false
  17. local lastFlicker   = os.clock()
  18. local lampOn        = false
  19. local lastAlarmBeat = -1
  20. local lastPct       = nil
  21.  
  22. term.clear()
  23. term.setCursorPos(1,1)
  24. print("Battery alarm script running!")
  25. term.setCursorPos(1,2)
  26. io.write("Battery: ")
  27.  
  28. while true do
  29.   local tickTimer = os.startTimer(0.1)
  30.   local accPct = nil
  31.   local event
  32.  
  33.   repeat
  34.     event = { os.pullEvent() }
  35.     if event[1] == "rednet_message"
  36.       and event[4] == "generator_status"
  37.       and type(event[3]) == "table"
  38.       and type(event[3].accPct) == "number"
  39.     then
  40.       accPct = event[3].accPct
  41.     end
  42.   until event[1] == "timer" or accPct
  43.  
  44.   -- Display and refresh battery percentage on line 2
  45.   if accPct then
  46.     -- Avoid flicker: only redraw if changed or at first
  47.     if lastPct == nil or math.abs(accPct - lastPct) > 0.01 then
  48.       term.setCursorPos(9,2)
  49.       io.write(string.format("%6.2f%%   ", accPct))
  50.       lastPct = accPct
  51.     end
  52.   end
  53.  
  54.   if not accPct then goto continue end
  55.  
  56.   if accPct < thresholdPct then
  57.     if not alarmActive then
  58.       term.setCursorPos(1,3)
  59.       print("ALARM: Battery below threshold!")
  60.       alarmActive   = true
  61.       lampOn        = false
  62.       lastFlicker   = os.clock()
  63.       lastAlarmBeat = -1
  64.     end
  65.  
  66.     -- Flicker the lamp
  67.     if os.clock() - lastFlicker > flickerTime then
  68.       lampOn = not lampOn
  69.       redstone.setOutput(lightSide, lampOn)
  70.       lastFlicker = os.clock()
  71.     end
  72.  
  73.     -- Synced alarm using in-game os.time()
  74.     local now  = os.time()
  75.     local beat = math.floor(now * 50 / alarmRepeat)
  76.     if beat ~= lastAlarmBeat then
  77.       for i = 1, 40 do
  78.         speaker.playNote("bass", 3, 8)
  79.       end
  80.       lastAlarmBeat = beat
  81.     end
  82.  
  83.   else
  84.     if alarmActive then
  85.       term.setCursorPos(1,3)
  86.       print("Battery restored.             ")
  87.       alarmActive = false
  88.       redstone.setOutput(lightSide, false)
  89.     end
  90.   end
  91.  
  92.   ::continue::
  93. end
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement