jonassvensson4

Tank reader

Apr 11th, 2014
91
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 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 = 3       -- Modem Frequency
  14. local content = "Water"  -- What is in tank?
  15. -- Make sure frequency match the main computer
  16.  
  17. -- Set warning lamp to off
  18. redstone.setOutput("right", false)
  19.  
  20.  
  21. -- Loop
  22. while true do
  23.   mon.clear()
  24.   mon.setCursorPos(1,1)
  25.  
  26.   -- Valve data
  27.   results = val.getTankInfo("back")
  28.   tanksTable = results
  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.   -- Monitor stuffs
  46.   mon.write(content)
  47.   mon.setCursorPos(1,2)
  48.   mon.write("Amount: " ..amount .."/"..cap .." B.")
  49.   mon.setCursorPos(1,3)
  50.   mon.write("Amount: " ..percentfull .."%  ")
  51.  
  52.   -- Check for change since last loop  
  53.   if percentfull ~= lastpercent then
  54.     sendmsg = content ..": " ..percentfull .." %"
  55.     wmod.transmit(sendFreq,0,sendmsg)
  56.   end
  57.  
  58.   -- Save for next loop
  59.   lastpercent = percentfull
  60.  
  61.   -- Warning control, local lamp
  62.   mon.setCursorPos(1,5)
  63.  
  64.   if percentfull < warning then
  65.     redstone.setOutput("right", true)
  66.     mon.write("Less than " ..warning .."% full")
  67.     sleep(1)
  68.     redstone.setOutput("right", false)
  69.     sleeptime = 1
  70.   else
  71.     -- Above warning level, sleep longer
  72.     mon.write("More than " ..warning .."% full")
  73.     sleeptime = 10
  74.   end
  75.  
  76.   sleep(sleeptime)    
  77. end
Advertisement
Add Comment
Please, Sign In to add comment