Advertisement
Fooman

Liquid Level Monitor Adv

May 18th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. --[[
  2. Liquid Level Monitor v1.1
  3. by Fooman
  4.  
  5. http://www.youtube.com/user/Foooman
  6.  
  7. ]]--
  8.  
  9. --Side of Computer with Nuclear Information Reader
  10. local nuclearReaderSide = "right"
  11. --Side of Computer with Howler Alarm
  12. local howlerAlarmSide = "back"
  13. --Side of Computer with Monitor
  14. local monitorSide = "left"
  15. --Time interval (seconds) between updates
  16. local updateInterval = 5
  17. --Amount of Liquid (amount/capacity) to trigger alarm
  18. local alarmTrigger = 0.25
  19. --Number of Tanks to monitor
  20. local tankQuantity = 1
  21.  
  22. local nonKitable = 18000
  23.  
  24. local lCapacity
  25. local lAmount
  26.  
  27. nr = peripheral.wrap(nuclearReaderSide)
  28. m = peripheral.wrap(monitorSide)
  29. m.setTextScale(0.5)
  30. local wm,hm = m.getSize()
  31.  
  32. local function monitorCentered(str,ypos)
  33.     m.setCursorPos(wm/2 - #str/2 + 1, ypos)
  34.     m.write(str)
  35. end
  36. local function monitorLeft(str,ypos)
  37.     m.setCursorPos(1 , ypos)
  38.     m.write(str)
  39. end
  40.  
  41. function getLiquid()
  42.     --Reset Counter to nonKitable base value
  43.     lAmount = 0
  44.     lCapacity = nonKitable
  45.     for i = 1,tankQuantity do
  46.         --get Counter for all BC Tanks
  47.         local a,b,c, info = nr.get(i)
  48.         --Error on bad sensor kit
  49.         if b ~= "OK" then error("bad sensor kit: "..b) end
  50.         for system, status in pairs(info) do
  51.             status = tostring(status)
  52.             if system == "capacity" then
  53.                 lCapacity = lCapacity + status
  54.             elseif system == "amount" then
  55.                 lAmount = lAmount + status
  56.             end
  57.         end
  58.     end
  59.     --Add nonKitable to Amount if nonzero
  60.     if lAmount > 0 then
  61.         lAmount = lAmount + nonKitable
  62.     end
  63. end
  64.  
  65. function alarmTrig()
  66.     if (lAmount/lCapacity) <= alarmTrigger then
  67.         rs.setOutput(howlerAlarmSide, true)
  68.     else
  69.         rs.setOutput(howlerAlarmSide, false)
  70.     end
  71. end
  72.  
  73. function updateMonitor()
  74.     m.clear()
  75.     monitorCentered("Fuel Amount",2)
  76.     monitorCentered(tostring(lAmount).." mB", 7)
  77. end
  78.  
  79.  
  80. --Operation
  81. term.clear()
  82. local updateTimer = os.startTimer(0.05)
  83.  
  84. while true do
  85.     event, param1, param2, param3 = os.pullEvent()
  86.     if event == "timer" then
  87.         getLiquid()
  88.         alarmTrig()
  89.         updateMonitor()
  90.         updateTimer = os.startTimer(updateInterval)
  91.     end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement