Guest User

tank

a guest
Jan 14th, 2015
2,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 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.     fluidRaw = nil
  25.     fluidName = nil
  26.     fluidAmount = nil
  27.     fluidCapacity = nil
  28.      
  29.     for k,v in pairs(tableInfo) do
  30.       fluidCapacity = v.capacity
  31.  
  32.       if v.contents then
  33.         for i,w in pairs(v.contents) do
  34.           if i == "rawName" then
  35.             fluidRaw = w
  36.           elseif i == "amount" then
  37.             fluidAmount = w
  38.           elseif i == "name" then
  39.             fluidName = w
  40.           end
  41.         end
  42.       end
  43.     end
  44.  
  45.     return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
  46. end
  47.  
  48. -- Main prog loop, never stop
  49. while true do
  50.   mon.clear()
  51.   mon.setCursorPos(1,1)
  52.  
  53.   -- Fill table with data from tank valve
  54.   local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(tank)
  55.  
  56.   if fluidName then
  57.     -- Get values for tank capacity and amount
  58.     fluidName = string.gsub(fluidName, "^%l", string.upper)
  59.     cap = fluidCapacity / 1000
  60.     amount = fluidAmount
  61.    
  62.     -- If tank is empty, to avoid math issues with 0
  63.     if amount == nil then
  64.       amount = 0
  65.       percentfull = 0
  66.     else
  67.       -- Use math.floor to convert to integers
  68.       amount = math.floor(amount / 1000)
  69.       percentfull = math.floor(100 * amount / cap)
  70.     end
  71.  
  72.     -- Self explanatory :)
  73.     mon.write(fluidName)
  74.     mon.setCursorPos(1,2)
  75.     mon.write("Amount: " ..amount .."/"..cap .." B.")
  76.     mon.setCursorPos(1,3)
  77.     mon.write("Amount: " ..percentfull .."%  ")
  78.  
  79.     -- Check for change since last loop  
  80.     if percentfull == lastpercent then
  81.       print("Still " ..percentfull .. "%, nothing sent.")
  82.     else
  83.       -- If value changed, send to main!
  84.       -- First format the left side:
  85.       if percentfull < 10 then
  86.         sendpercent = "  " ..percentfull
  87.       elseif percentfull < 100 then
  88.         sendpercent = " " ..percentfull
  89.       else
  90.         sendpercent = 100
  91.       end
  92.       -- Now format the rest of the text
  93.       if percentfull < warning then
  94.         sendmsg = sendpercent .."% .red." ..fluidName
  95.       else
  96.         sendmsg = sendpercent .."% .green." ..fluidName
  97.       end
  98.       wmod.transmit(sendFreq,0,sendmsg)
  99.       print("Sent: " ..sendmsg)
  100.     end
  101.  
  102.     -- Save for next loop
  103.     lastpercent = percentfull
  104.  
  105.     -- Warning control, local lamp
  106.     mon.setCursorPos(1,5)
  107.    
  108.     if percentfull < warning then
  109.       redstone.setOutput("right", true)
  110.       mon.write("Less than " ..warning .."% full")
  111.       sleep(1)
  112.       redstone.setOutput("right", false)
  113.       sleeptime = 1
  114.     else
  115.       -- Above warning level, sleep longer
  116.       mon.write("More than " ..warning .."% full")
  117.       sleeptime = 10
  118.     end
  119.   else
  120.     -- If no liquid is found in tank..
  121.     mon.write("No liquid in tank")
  122.     term.clear()
  123.     term.setCursorPos(1,1)
  124.     print("No liquid in tank")
  125.     sleeptime = 10
  126.   end
  127.  
  128.     -- Sleep either 1 or 10 seconds
  129.   sleep(sleeptime)    
  130. end
Advertisement
Add Comment
Please, Sign In to add comment