Guest User

tank

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