Aarondmorgan

Tank Level

May 14th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 max = 99           -- Warning level in %
  7. local min = 10       -- Warning level in %
  8. local cap                -- Tank capacity
  9. local amount             -- Amount liquid in tank
  10. local percentfull        -- Percent liquid in tank
  11. local lastpercent = 1000 -- Percent last loop
  12. local sendmsg            -- Message to send
  13. local sleeptime          -- How long to sleep
  14. local sendFreq = 3       -- Modem Frequency
  15. local content = "Water"  -- What is in tank?
  16. -- Make sure frequency match the main computer
  17.  
  18. -- Main prog loop, never stop
  19. while true do
  20.   -- Fill table with data from tank valve
  21.   tanksTable = val.getTankInfo("WhatIsThis")
  22.   maintank = tanksTable[1]
  23.  
  24.   -- Get values for tank capacity and amount
  25.   cap = maintank.capacity / 1000   -- in buckets
  26.   amount = maintank.amount    -- in millibuckets
  27.  
  28.   -- If tank is empty, to avoid math issues with 0
  29.   if amount == nil then
  30.     amount = 0
  31.     percentfull = 0
  32.   else
  33.     -- Use math.floor to convert to integers
  34.     amount = math.floor(amount / 1000)
  35.     percentfull = math.floor(100 * amount / cap)
  36.   end
  37.  
  38.   -- Check for change since last loop  
  39.   if percentfull == lastpercent then
  40.     sleep(1)
  41.   else
  42.     -- If value changed, send to main!
  43.     sendmsg = content ..": " ..percentfull .." %"
  44.     wmod.transmit(sendFreq,0,sendmsg)
  45.   end
  46.  
  47.   -- Save for next loop
  48.   lastpercent = percentfull
  49.  
  50.   if percentfull < min then
  51.     redstone.setOutput("right", true)
  52.   end
  53.  
  54.   if percentfull > max then
  55.     redstone.setOutput("right", false)
  56.   end
  57.  
  58.   sleep(10)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment