Advertisement
natie3

EnergyManager

Nov 16th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. modem = peripheral.wrap("left")
  2. treeoil = peripheral.wrap("back")
  3. rosin = peripheral.wrap("right")
  4.  
  5. modem.open(3)
  6. os.startTimer(10)
  7.  
  8. treeoil_on = true
  9. rosin_on = false
  10. steam_percentage = 0
  11.  
  12. rs.setOutput("back", treeoil_on)
  13. rs.setOutput("right", not treeoil_on)
  14.  
  15. function toString(bool)
  16.   return bool and "on" or "off"
  17. end
  18.  
  19. function set(treeoil, rosin)
  20.   print(toString(treeoil) .. " " .. toString(rosin))
  21.   treeoil_on = treeoil
  22.   rosin_on = rosin
  23.   rs.setOutput("back", treeoil_on)
  24.   rs.setOutput("right", rosin_on)
  25. end
  26.  
  27. function getPercentage(device)
  28.   return math.floor(100 * (device.getEnergyStored() or 0) / device.getRFCapacity())
  29. end
  30.  
  31. set(true, false)
  32.  
  33. while true do
  34.   e, r1, r2, r3, r4 = os.pullEvent()
  35.   if e == "modem_message" or e == "timer" then
  36.     if e == "modem_message" then
  37.       steam_percentage = tonumber(r4)
  38.     elseif e == "timer" then
  39.       os.startTimer(11)
  40.     end
  41.  
  42.     percentage = getPercentage(rosin)
  43.     if steam_percentage > 90 and (treeoil_on or rosin_on) then
  44.       steam_percentage = tonumber(r4)
  45.       set(false, false)
  46.     elseif (steam_percentage < 10 and not (treeoil_on or rosin_on)) or treeoil_on or rosin_on then
  47.       if percentage > 60 then
  48.         set(false, true)
  49.       else
  50.         set(true, false)
  51.       end
  52.     end
  53.   end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement