Guest User

watertank

a guest
Oct 5th, 2013
3,580
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 1 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 = 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. -- Main prog loop, never stop
  22. while true do
  23.   mon.clear()
  24.   mon.setCursorPos(1,1)
  25.  
  26.   -- Fill table with data from tank valve
  27.   tanksTable = val.getTanks("WhatIsThis")
  28.   maintank = tanksTable[1]
  29.  
  30.   -- Get values for tank capacity and amount
  31.   cap = maintank.capacity / 1000   -- in buckets
  32.   amount = maintank.amount    -- in millibuckets
  33.  
  34.   -- If tank is empty, to avoid math issues with 0
  35.   if amount == nil then
  36.     amount = 0
  37.     percentfull = 0
  38.   else
  39.     -- Use math.floor to convert to integers
  40.     amount = math.floor(amount / 1000)
  41.     percentfull = math.floor(100 * amount / cap)
  42.   end
  43.  
  44.   -- Self explanatory :)
  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 then
  53.     print("Still " ..percentfull .. "%, nothing sent.")
  54.   else
  55.     -- If value changed, send to main!
  56.     sendmsg = content ..": " ..percentfull .." %"
  57.     wmod.transmit(sendFreq,0,sendmsg)
  58.     print("Sent: " ..sendmsg)
  59.   end
  60.  
  61.   -- Save for next loop
  62.   lastpercent = percentfull
  63.  
  64.   -- Warning control, local lamp
  65.   mon.setCursorPos(1,5)
  66.  
  67.   if percentfull < warning then
  68.     redstone.setOutput("right", true)
  69.     mon.write("Less than " ..warning .."% full")
  70.     sleep(1)
  71.     redstone.setOutput("right", false)
  72.     sleeptime = 1
  73.   else
  74.     -- Above warning level, sleep longer
  75.     mon.write("More than " ..warning .."% full")
  76.     sleeptime = 10
  77.   end
  78.  
  79.   -- Sleep either 1 or 10 seconds
  80.   sleep(sleeptime)    
  81. end
Advertisement
Add Comment
Please, Sign In to add comment