Advertisement
TheSgtPunishment

ComputerCraft OpenPeripherals Boiler Monitor

Dec 9th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.88 KB | None | 0 0
  1. -- Firebox, Wireless Modem
  2. local fireb = peripheral.wrap("top")
  3. local mon = peripheral.wrap("front")
  4. local wmod = peripheral.wrap("bottom")
  5.  
  6. local tankName = "Boiler 01" -- Name of the tank
  7. local warning = 20           -- Warning level in %
  8. local wcap                   -- Water Tank capacity
  9. local scap                   -- Steam Tank capacity
  10. local fcap                   -- Fuel Tank capacity
  11. local wamount                -- Amount liquid in tank
  12. local samount                -- Amount liquid in tank
  13. local famount                -- Amount liquid in tank
  14. local wpercentfull           -- Percent liquid in tank
  15. local spercentfull           -- Percent liquid in tank
  16. local fpercentfull           -- Percent liquid in tank
  17. local wlastpercent = 1000    -- Percent last loop
  18. local slastpercent = 1000    -- Percent last loop
  19. local flastpercent = 1000    -- Percent last loop
  20. local sendmsg                -- Message to send
  21. local sleeptime              -- How long to sleep
  22. local wsendFreq = 10           -- Modem Frequency
  23. local ssendFreq = 11          -- Modem Frequency
  24. local fsendFreq = 12           -- Modem Frequency
  25. -- Make sure the frequency matches the main computer
  26.  
  27. -- Main loop
  28. while true do
  29.  
  30.   mon.clear()
  31.   mon.setCursorPos(1,1)
  32.  
  33.   -- Fill table with data from tank valve
  34.   tanksTable = fireb.getTanks("WhatIsThis")
  35.   watertank = tanksTable[1]
  36.   steamtank = tanksTable[2]
  37.   fueltank = tanksTable[3]
  38.  
  39. -- Water
  40.  
  41.   -- Get values for tank capacity and amount
  42.   wcap = watertank.capacity / 1000   -- in buckets
  43.   wamount = watertank.amount    -- in millibuckets
  44.  
  45.   scap = steamtank.capacity / 1000   -- in buckets
  46.   samount = steamtank.amount    -- in millibuckets
  47.  
  48.   fcap = fueltank.capacity / 1000   -- in buckets
  49.   famount = fueltank.amount    -- in millibuckets
  50.  
  51.   -- If Water tank is empty, to avoid math issues with 0
  52.   if wamount == nil then
  53.     wamount = 0
  54.     wpercentfull = 0
  55.   else
  56.     -- Use math.floor to convert to integers
  57.     wamount = math.floor(wamount / 1000)
  58.     wpercentfull = math.floor(100 * wamount / wcap)
  59.   end
  60.  
  61.   -- Self explanatory :)
  62.   mon.write(watertank.name)
  63.   mon.setCursorPos(1,2)
  64.   mon.write("Amount: " ..wamount .."/"..wcap .." B.")
  65.   mon.setCursorPos(1,3)
  66.   mon.write("Amount: " ..wpercentfull .."%  ")
  67.  
  68.   -- Check for change since last loop  
  69.   if wpercentfull == wlastpercent then
  70.     print("Still " ..wpercentfull .. "%, nothing sent.")
  71.   else
  72.     -- If value changed, send to main!
  73.  
  74.     wsendmsg = [[tankName
  75. watertank.name ..": " ..wpercentfull .." %"
  76.  
  77.     wmod.transmit(wsendFreq,0,wsendmsg)
  78.     print("Sent: " ..wsendmsg)
  79.   end
  80.  
  81.   -- Save for next loop
  82.   wlastpercent = wpercentfull
  83.  
  84. -- Steam
  85.  
  86.   -- If steam tank is empty, to avoid math issues with 0
  87.   if samount == nil then
  88.     samount = 0
  89.     spercentfull = 0
  90.   else
  91.     -- Use math.floor to convert to integers
  92.     samount = math.floor(samount / 1000)
  93.     spercentfull = math.floor(100 * samount / scap)
  94.   end
  95.  
  96.   -- Self explanatory :)
  97.   mon.setCursorPos(1,4)
  98.   mon.write(steamtank.name)
  99.   mon.setCursorPos(1,5)
  100.   mon.write("Amount: " ..samount .."/"..scap .." B.")
  101.   mon.setCursorPos(1,6)
  102.   mon.write("Amount: " ..spercentfull .."%  ")
  103.  
  104.   -- Check for change since last loop  
  105.   if spercentfull == slastpercent then
  106.     print("Still " ..spercentfull .. "%, nothing sent.")
  107.   else
  108.     -- If value changed, send to main!
  109.     sendmsg = steamtank.name ..": " ..spercentfull .." %"
  110.     wmod.transmit(ssendFreq,0,sendmsg)
  111.     print("Sent: " ..sendmsg)
  112.   end
  113.  
  114.   -- Save for next loop
  115.   slastpercent = spercentfull
  116.  
  117. -- Fuel
  118.  
  119.   -- If fuel tank is empty, to avoid math issues with 0
  120.   if famount == nil then
  121.     famount = 0
  122.     fpercentfull = 0
  123.   else
  124.     -- Use math.floor to convert to integers
  125.     famount = math.floor(famount / 1000)
  126.     fpercentfull = math.floor(100 * famount / fcap)
  127.   end
  128.  
  129.   -- Self explanatory :)
  130.   mon.setCursorPos(1,7)
  131.   mon.write(fueltank.name)
  132.   mon.setCursorPos(1,8)
  133.   mon.write("Amount: " ..famount .."/"..fcap .." B.")
  134.   mon.setCursorPos(1,9)
  135.   mon.write("Amount: " ..fpercentfull .."%  ")
  136.  
  137.   -- Check for change since last loop  
  138.   if fpercentfull == flastpercent then
  139.     print("Still " ..fpercentfull .. "%, nothing sent.")
  140.   else
  141.     -- If value changed, send to main!
  142.     sendmsg = fueltank.name ..": " ..fpercentfull .." %"
  143.     wmod.transmit(fsendFreq,0,sendmsg)
  144.     print("Sent: " ..sendmsg)
  145.   end
  146.  
  147.   -- Save for next loop
  148.   flastpercent = fpercentfull
  149.  
  150.   -- Warning control, local lamp
  151.   mon.setCursorPos(1,10)
  152.  
  153.   if wpercentfull < warning then
  154.     redstone.setOutput("right", true)
  155.     mon.write("Less than " ..warning .."% full")
  156.     sleep(1)
  157.     redstone.setOutput("right", false)
  158.     sleeptime = 1
  159.   else
  160.     -- Above warning level, sleep longer
  161.     mon.write("More than " ..warning .."% full")
  162.     sleeptime = 10
  163.   end
  164.  
  165.   -- Sleep either 1 or 10 seconds
  166.   sleep(sleeptime)    
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement