Advertisement
_Jacques

energyController

Aug 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component") -- This whole block is to get the addresses of the "hardware" used and imports the libraries component, sides and term.
  2. local sides = require("sides")
  3. local term = require("term")
  4. local redstone = component.proxy(component.redstone.address)
  5. local energyCell = component.proxy(component.energy_device.address)
  6.  
  7. local redstoneSide = 0
  8. local energySide = 0
  9. local highEnergy = 0
  10. local lowEnergy = 0
  11. local x = 0 -- Used for the if statements in the while statement in the main block.
  12. local y = 2 -- Used for the powerRate() calculations to link it to the refresh rate, os.sleep(y).,, in order to get a bigger sample size to calculate the power difference.
  13.  
  14. local function savedSettings()
  15.  
  16.  
  17.  
  18. local function settings()
  19.    
  20.     io.write("On which side should the redstone be output?\n")
  21.     redstoneSide = io.read()
  22.     io.write("On which side is the energy cell?\n")
  23.     energySide = io.read()
  24.     io.write("What is the desired high energy percentage threshold?\n")
  25.     highEnergy = tonumber(io.read())
  26.     io.write("What is the desired low energy percentage threshold?\n")
  27.     lowEnergy = tonumber(io.read())
  28.    
  29. end
  30.  
  31. local function energyPercent()
  32.   local energy = (energyCell.getEnergyStored())/(energyCell.getMaxEnergyStored())*100
  33.   return energy
  34. end
  35.  
  36. local function powerRate() -- In essence, this takes an approximate derivative of the energy stored in function of time.
  37.   local a = energyCell.getEnergyStored()
  38.   os.sleep(y)
  39.   local b = energyCell.getEnergyStored()
  40.   local c = (b-a)/20*y
  41.   term.setCursor(1,2)
  42.   term.clearLine()
  43. io.write("RF per tick : "..c.."\n")
  44. end
  45.  
  46. local function highEnergy()
  47.   x = 1
  48.   redstone.setOutput(sides[redstoneSide],0)
  49.   term.clear()
  50.   term.setCursor(1,1)
  51.   io.write("Too much power! shutting off generation.\n")  
  52. end
  53.  
  54. local function lowEnergy()
  55.   x = 2
  56.   redstone.setOutput(sides[redstoneSide],15)
  57.   term.clear()
  58.   term.setCursor(1,1)
  59.   io.write("Power is being built up!\n")
  60. end
  61.  
  62.  
  63. term.clear()
  64. io.write("Power Controller v.1.0\n")
  65. os.sleep(1)
  66. term.clear()
  67. askSides()
  68.  
  69. while true do
  70.   if energyPercent() > highEnergy and not (x == 1) then
  71.     highEnergy()
  72.   elseif energyPercent() <= lowEnergy  and not (x == 2) then
  73.     lowEnergy()
  74.   end
  75.   powerRate()
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement