Advertisement
XDemonic

ComputerCraft - WaterTank

Mar 25th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. -- Valve, Monitor & Wireless Modem
  2.         local val = peripheral.wrap("back")
  3.         local mon = peripheral.wrap("top")
  4.         local wmod = peripheral.wrap("bottom")
  5.          
  6.         local warning = 20       -- Warning level in %
  7.         local cap                -- Tank capacity
  8.         local amount             -- Amount liquid in tank
  9.         local percentfull        -- Percent liquid in tank
  10.         local lastpercent = 1000 -- Percent last loop
  11.         local sendmsg            -- Message to send
  12.         local sleeptime          -- How long to sleep
  13.         local sendFreq = 1       -- Modem Frequency
  14.         local warnFreq = 2       -- Modem Frequency
  15.         local content = "Water[1]"  -- What is in tank?
  16.         local WarningLast = false
  17.         -- Make sure frequency match the main computer
  18.          
  19.         -- Set warning lamp to off
  20.          
  21.          
  22.         -- Main prog loop, never stop
  23.         while true do
  24.           --mon.clear()
  25.           --mon.setCursorPos(1,1)
  26.          
  27.           -- Fill table with data from tank valve
  28.           tanksTable = val.getTankInfo("unknown")
  29.           maintank = tanksTable[1]
  30.          
  31.           -- Get values for tank capacity and amount
  32.           cap = maintank.capacity / 1000   -- in buckets
  33.           amount = maintank.amount    -- in millibuckets
  34.          
  35.           -- If tank is empty, to avoid math issues with 0
  36.           if amount == nil then
  37.             amount = 0
  38.             percentfull = 0
  39.           else
  40.             -- Use math.floor to convert to integers
  41.             amount = math.floor(amount / 1000)
  42.             percentfull = math.floor(100 * amount / cap)
  43.           end
  44.          
  45.           --mon.write(content)
  46.           --mon.setCursorPos(1,2)
  47.           --mon.write("Amount: " ..amount .."/"..cap .." B.")
  48.           --mon.setCursorPos(1,3)
  49.           --mon.write("Amount: " ..percentfull .."%  ")
  50.          
  51.           -- Check for change since last loop  
  52.           if percentfull == lastpercent and percentfull > warning then
  53.             print("Still " ..percentfull .. "%, nothing sent.")
  54.           else
  55.           sendmsg = content ..": " ..percentfull .."%"
  56.           if percentfull < warning then
  57.             if WarningLast == false then
  58.             WarningLast = true
  59.             sendmsg = sendmsg .. " [WARNING]"
  60.             else
  61.             WarningLast = false
  62.             end
  63.             wmod.transmit(warnFreq,0,sendmsg)
  64.           else
  65.             wmod.transmit(sendFreq,0,sendmsg)
  66.           end
  67.             print("Sent: " ..sendmsg)
  68.           end
  69.          
  70.           -- Save for next loop
  71.           lastpercent = percentfull
  72.           sleeptime = 1
  73.  
  74.          
  75.           -- Sleep either 1 or 10 seconds
  76.           sleep(sleeptime)    
  77.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement