Guest User

watertank

a guest
Sep 8th, 2014
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. -- Valve, Monitor & Wireless Modem
  2. local tank = peripheral.wrap("back")
  3. local mon = peripheral.wrap("top")
  4. local wmod = peripheral.wrap("bottom")
  5.  
  6. -- Make sure channel/frequency match main computer
  7.  
  8. local sendFreq = 2       -- Modem "Frequency"
  9. local warning = 50       -- Warning level in %
  10. local cap                -- Tank capacity
  11. local amount             -- Amount liquid in tank
  12. local percentfull        -- Percent liquid in tank
  13. local lastpercent = 1000 -- Percent last loop
  14. local sendmsg            -- Message to send
  15. local sleeptime          -- How long to sleep
  16. local sendpercent        -- Formatted percentage string
  17.  
  18. -- Set warning lamp to off
  19. redstone.setOutput("right", false)
  20.  
  21. function getTank(tankPeriph)
  22.     local tableInfo = tankPeriph.getTankInfo("unknown") -- Local to the getTank function.
  23.  
  24.     for k,v in pairs(tableInfo) do
  25.         fluidRaw = v.rawName -- local to this for loop
  26.         fluidName = v.name -- local to this for loop
  27.         fluidAmount = v.amount -- local to this for loop
  28.         fluidCapacity = v.capacity -- local to this for loop
  29.     end
  30.  
  31.     return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
  32. end
  33.  
  34. -- Main prog loop, never stop
  35. while true do
  36.   mon.clear()
  37.   mon.setCursorPos(1,1)
  38.  
  39.   -- Fill table with data from tank valve
  40.   local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(tank)
  41.  
  42.   if fluidName then
  43.     -- Get values for tank capacity and amount
  44.     fluidName = string.gsub(fluidName, "^%l", string.upper)
  45.     cap = fluidCapacity / 1000
  46.     amount = fluidAmount
  47.    
  48.     -- If tank is empty, to avoid math issues with 0
  49.     if amount == nil then
  50.       amount = 0
  51.       percentfull = 0
  52.     else
  53.       -- Use math.floor to convert to integers
  54.       amount = math.floor(amount / 1000)
  55.       percentfull = math.floor(100 * amount / cap)
  56.     end
  57.  
  58.     -- Self explanatory :)
  59.     mon.write(fluidName)
  60.     mon.setCursorPos(1,2)
  61.     mon.write("Amount: " ..amount .."/"..cap .." B.")
  62.     mon.setCursorPos(1,3)
  63.     mon.write("Amount: " ..percentfull .."%  ")
  64.  
  65.     -- Check for change since last loop  
  66.     if percentfull == lastpercent then
  67.       print("Still " ..percentfull .. "%, nothing sent.")
  68.     else
  69.       -- If value changed, send to main!
  70.       -- First format the left side:
  71.       if percentfull < 10 then
  72.         sendpercent = "  " ..percentfull
  73.       elseif percentfull < 100 then
  74.         sendpercent = " " ..percentfull
  75.       else
  76.         sendpercent = 100
  77.       end
  78.       -- Now format the rest of the text
  79.       if percentfull < warning then
  80.         sendmsg = sendpercent .."% .red." ..fluidName
  81.       else
  82.         sendmsg = sendpercent .."% .green." ..fluidName
  83.       end
  84.       wmod.transmit(sendFreq,0,sendmsg)
  85.       print("Sent: " ..sendmsg)
  86.     end
  87.  
  88.     -- Save for next loop
  89.     lastpercent = percentfull
  90.  
  91.     -- Warning control, local lamp
  92.     mon.setCursorPos(1,5)
  93.    
  94.     if percentfull < warning then
  95.       redstone.setOutput("right", true)
  96.       mon.write("Less than " ..warning .."% full")
  97.       sleep(1)
  98.       redstone.setOutput("right", false)
  99.       sleeptime = 1
  100.     else
  101.       -- Above warning level, sleep longer
  102.       mon.write("More than " ..warning .."% full")
  103.       sleeptime = 10
  104.     end
  105.   else
  106.     -- If no liquid is found in tank..
  107.     mon.write("No liquid in tank")
  108.     term.clear()
  109.     term.setCursorPos(1,1)
  110.     print("No liquid in tank")
  111.     sleeptime = 10
  112.   end
  113.  
  114.     -- Sleep either 1 or 10 seconds
  115.   sleep(sleeptime)    
  116. end
Advertisement
Add Comment
Please, Sign In to add comment