Guest User

watertank

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