Shadows_Player

Computercraft/Tanks

Jan 7th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. -- Valve, Monitor & Wireless Modem
  2. local tank = peripheral.wrap("back")
  3. local mon = peripheral.wrap("top")
  4. local wmod = peripheral.wrap("bottom")
  5.  
  6. -- Make sure channel/frequency match main computer
  7.  
  8. local sendFreq = 2       -- Modem "Frequency"
  9. local warning = 30       -- Warning level in %
  10. local cap                -- Tank capacity
  11. local amount             -- Amount liquid in tank
  12. local percentfull        -- Percent liquid in tank
  13. local sleeptime          -- How long to sleep
  14. local sendpercent        -- Formatted percentage string
  15. monX,monY = mon.getSize()
  16.  
  17. -- Set warning lamp to off
  18. redstone.setOutput("right", false)
  19.  
  20. function getTank(tank)
  21.     local output = tank.getTanks() -- Local to the getTank function.
  22.  
  23.     fluidName = output[1]["displayName"]    
  24.     fluidAmount = output[1]["amount"]
  25.     fluidCapacity = output[1]["capacity"]
  26.  
  27.     return fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
  28. end
  29.  
  30. -- Main prog loop, never stop
  31. while true do
  32.   wmod.open(2)
  33.   mon.clear()
  34.   mon.setCursorPos(1,1)
  35.  
  36.   -- Fill table with data from tank valve
  37.   local fluidName, fluidAmount, fluidCapacity = getTank(tank)
  38.  
  39.   if fluidName then
  40.     -- Get values for tank capacity and amount
  41.     cap = fluidCapacity / 1000
  42.     am = fluidAmount
  43.    
  44.     -- If tank is empty, to avoid math issues with 0
  45.     if am == nil then
  46.       am = 0
  47.       percentfull = 0
  48.     else
  49.       -- Use math.floor to convert to integers
  50.       am = math.floor(am / 1000)
  51.       percentfull = math.floor(100 * am / cap)
  52.     end
  53.  
  54.     -- Self explanatory :)
  55.     mon.write("Fluid :" ..fluidName)
  56.     mon.setCursorPos(1,2)
  57.     mon.write("Amount: " ..am .."/"..cap .." B.")
  58.     mon.setCursorPos(1,3)
  59.     mon.write("Percent: " ..percentfull .."%  ")
  60.    
  61.     --Transmition Zone
  62.     tankw = {water = percentfull}
  63.     wmod.transmit(2,2,tankw)
  64.     print("Current Percent :"..tankw["water"])
  65.    
  66.     --Warning Zone
  67.     mon.setCursorPos(1,4)
  68.     if percentfull < warning then
  69.       redstone.setOutput("right", true)
  70.       mon.write("Less than " ..warning .."% full")
  71.       sleeptime = 1
  72.     else
  73.       -- Above warning level, sleep longer
  74.       mon.write("More than " ..warning .."% full")
  75.       redstone.setOutput("right", false)
  76.       sleeptime = 10
  77.     end
  78.   else
  79.     -- If no liquid is found in tank..
  80.     mon.write("No liquid in tank")
  81.     percentfull = 0
  82.     term.clear()
  83.     term.setCursorPos(1,1)
  84.     print("No liquid in tank")
  85.     sleeptime = 10
  86.     percentfull = 0
  87.     tankw = {water = percentfull}
  88.     wmod.transmit(2,2,tankw)
  89.     print("Current Percent :"..tankw["water"])
  90.   end
  91.     -- Sleep either 1 or 10 seconds
  92.   sleep(sleeptime)    
  93. end
  94.  
Add Comment
Please, Sign In to add comment