Advertisement
rhn

Energy cell power monitor v2

rhn
Jul 31st, 2014
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. -- Monitors TE3 Energy cells and EnderIO capacitor banks and output redstone signals once energy storage drops below set limits.
  2. -- Will automatically detect direction of adjacent storage device and (optional) Advanced Monitors. If chosen, monitor format should be 1 high and 2 wide.
  3. -- Redstone signal for the engines will be output out the back of the computer.
  4. --More details: http://forum.feed-the-beast.com/threads/rhns-1-6-monster-build-journal-and-guide-collection.42664/page-15#post-718973
  5.  
  6.  
  7. local upper = 0.90 --Upper limit for computer to stop transmitting redstone signal. 0.90=90% full.
  8. local lower = 0.10 --Lower limit for computer to start transmitting redstone signal.
  9.  
  10.  
  11. --Device detection
  12. isError=0
  13.  
  14. function detectDevice(DeviceName)
  15. DeviceSide="none"
  16. for k,v in pairs(redstone.getSides()) do
  17.  if peripheral.getType(v)==DeviceName then
  18.    DeviceSide = v
  19.    break
  20.  end
  21. end
  22.   return(DeviceSide)
  23. end
  24.  
  25. MonitorSide=detectDevice("monitor")
  26.  
  27. if MonitorSide~="none" then
  28.       monitor=peripheral.wrap(MonitorSide)
  29.    print ("Monitor on the " .. MonitorSide .. " connected.")
  30.    else
  31.    print ("Warning - No Monitor attached, continuing without.")
  32.    
  33. end
  34.  
  35. CellSide=detectDevice("cofh_thermalexpansion_energycell")
  36.  
  37. if CellSide~="none" then
  38.       cell=peripheral.wrap(CellSide)
  39.    print ("TE Energy cell on the " .. CellSide .. " connected.")
  40.    else
  41.     CellSide=detectDevice("tile_enderio_blockcapacitorbank_name")
  42.     if CellSide~="none" then
  43.         cell=peripheral.wrap(CellSide)
  44.         print ("EnderIO capacitorbank on the " .. CellSide .. " connected.")
  45.     else
  46.         print ("*ERROR* - No Energy storage attached, halting code!")
  47.         return
  48.     end
  49. end
  50.  
  51.  
  52. --Main code
  53. redstone.setOutput("back", false) --Defaulting to off
  54.  
  55. --If monitor is attached, write data on monitor
  56. if MonitorSide~="none" then
  57.     monitor.clear()
  58.     monitor.setBackgroundColour((colours.grey))
  59.     monitor.setCursorPos(1,4)
  60.     monitor.write(" ON ")
  61.     monitor.setBackgroundColour((colours.green))
  62.     monitor.setCursorPos(5,4)
  63.     monitor.write(" OFF ")
  64.     monitor.setBackgroundColour((colours.black))
  65. end
  66.  
  67. --Main loop
  68. while true do
  69.     --Get storage values
  70.     eNow = cell.getEnergyStored("left")
  71.     eMax = cell.getMaxEnergyStored("left")
  72.  
  73.     --Compute ratio
  74.     fill = (eNow / eMax)
  75.  
  76. --If monitor is attached, write data on monitor
  77. if MonitorSide~="none" then
  78.     monitor.setCursorPos(11,2)
  79.     monitor.write("Storage:")
  80.     monitor.setCursorPos(11,3)
  81.     monitor.write(eNow)
  82.     monitor.setCursorPos(11,4)
  83.     monitor.write("Of:")
  84.     monitor.setCursorPos(11,5)
  85.     monitor.write(eMax)
  86.  
  87.     monitor.setCursorPos(1,2)
  88.     monitor.write("Engines:")
  89. end
  90.  
  91.     if fill > upper then
  92.         --energylevel is over upper level, turning redstone signal off
  93.         redstone.setOutput("back", false)
  94.  
  95.         if MonitorSide~="none" then
  96.             monitor.setBackgroundColour((colours.grey))
  97.             monitor.setCursorPos(1,4)
  98.             monitor.write(" ON ")
  99.             monitor.setBackgroundColour((colours.green))
  100.             monitor.setCursorPos(5,4)
  101.             monitor.write(" OFF ")
  102.             monitor.setBackgroundColour((colours.black))
  103.         end
  104.  
  105.     elseif fill < lower then
  106.         --energy level is below lower limit, turning redstone signal on
  107.         redstone.setOutput("back", true)
  108.        
  109.         if MonitorSide~="none" then
  110.             monitor.setBackgroundColour((colours.green))
  111.             monitor.setCursorPos(1,4)
  112.             monitor.write(" ON ")
  113.             monitor.setBackgroundColour((colours.grey))
  114.             monitor.setCursorPos(5,4)
  115.             monitor.write(" OFF ")
  116.             monitor.setBackgroundColour((colours.black))
  117.         end
  118.     end
  119.  
  120.    
  121.     sleep(1)
  122. end --while
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement