Advertisement
tsargothruneclaw

Power Station

Sep 15th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 KB | None | 0 0
  1. -- Peripheral Wrapping
  2. local solarGenerator=peripheral.wrap("back")
  3.  
  4. -- Global Variables
  5. local Discharging=0
  6. local count=0
  7. local oldBat=0
  8.  
  9. -- Functions
  10. function isDaylight()
  11.     return redstone.getAnalogInput("left")
  12. end
  13.  
  14. function checkBattery()
  15.     return ((solarGenerator.getEnergyStored() / solarGenerator.getMaxEnergyStored())*100)
  16. end
  17.  
  18. function Discharge()
  19.     redstone.setAnalogOutput("back",15)
  20.     Discharging=true
  21. end
  22.  
  23. function Charge()
  24.     redstone.setAnalogOutput("back",0)
  25.     Discharging=false
  26.     count=0
  27. end
  28.  
  29. -- Main Loop
  30. Charge()
  31.  
  32. while true do  
  33.     if (isDaylight() > 0) then
  34.         if (checkBattery() > 99 and Discharging==false) then
  35.             print("Battery is full. Discharging")
  36.             Discharge()
  37.         end
  38.     else
  39.         print("No daylight detected. Discharging.")
  40.         Discharge()
  41.     end
  42.         print("Waiting... ("..checkBattery().."%)")
  43.     if (checkBattery()<1) then
  44.         print ("Internal battery discharged. Charging")
  45.         Charge()
  46.     end
  47.     if (checkBattery()==oldBat) then
  48.         count=count+1
  49.     else
  50.         oldBat=checkBattery()
  51.     end
  52.     if (count>5) then
  53.         Charge()
  54.     end
  55.     sleep(10)
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement