Guest User

tanks

a guest
Apr 29th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. -- Valve, Monitor & Wireless Modem
  2. local nir = peripheral.wrap("right")
  3. local mon = peripheral.wrap("top")
  4. local wmod = peripheral.wrap("back")
  5.  
  6. local warning = 25          -- 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              -- Modem Tx Frequency
  14. local content = liquidType  -- What is in tank?
  15. -- Make sure frequency match the main computer
  16.  
  17. -- grab data values
  18. a,b,c,data = nir.get(1)
  19. if data["liquidId"] == 11
  20.   then
  21.     liquidType = "Lava"
  22.     sendFreq = "3"
  23. elseif data["liquidId"] == 255
  24.   then
  25.     liquidType = "Oil"
  26.     sendFreq = "4"
  27. elseif data["liquidId"] == 4064
  28.   then
  29.     liquidType = "Fuel"
  30.     sendFreq = "5"
  31. else
  32.   liquidType = "Unknown"
  33.   sendFreq = "6"
  34. end
  35.  
  36. -- Set warning lamp to off
  37. redstone.setOutput("left", false)
  38.  
  39.  
  40. -- Main prog loop, never stop
  41. while true do
  42.   mon.clear()
  43.   mon.setCursorPos(1,1)
  44.  
  45.   -- Get values for tank capacity and amount
  46.   cap = data["capacity"]
  47.   amount = data["amount"]
  48.  
  49.   -- If tank is empty, to avoid math issues with 0
  50.   if amount == nil then
  51.     amount = 0
  52.     percentFull = 0
  53.   else
  54.  
  55.     -- Use math.floor to convert to integers
  56.     percentFull = math.floor(100 * amount / cap)
  57.   end
  58.  
  59.   -- Self explanatory :)
  60.   mon.write(content)
  61.   mon.setCursorPos(1,2)
  62.   mon.write("Amount: " ..amount .."/"..cap .." mB.")
  63.   mon.setCursorPos(1,3)
  64.   mon.write("Amount: " ..percentFull .."%  ")
  65.  
  66.   -- Check for change since last loop  
  67.   if percentFull == lastPercent then
  68.     print("Still " ..percentfull .. "%, nothing sent.")
  69.   else
  70.     -- If value changed, send to main!
  71.     sendmsg = content ..": " ..percentFull .." %"
  72.     wmod.transmit(sendFreq,"0",sendmsg)
  73.     print("Sent: " ..sendmsg)
  74.   end
  75.  
  76.   -- Save for next loop
  77.   lastPercent = percentFull
  78.  
  79.   -- Warning control, local lamp
  80.   mon.setCursorPos(1,5)
  81.  
  82.   if percentFull < warning then
  83.     redstone.setOutput("left", true)
  84.     mon.write("Less than " ..warning .."% full")
  85.     sleep(1)
  86.     redstone.setOutput("left", false)
  87.     sleeptime = 1
  88.   else
  89.     -- Above warning level, sleep longer
  90.     mon.write("More than " ..warning .."% full")
  91.     sleeptime = 5
  92.   end
  93.  
  94.   -- Sleep either 1 or 10 seconds
  95.   sleep(sleeptime)    
  96. end
Advertisement
Add Comment
Please, Sign In to add comment