Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Valve, Monitor & Wireless Modem
- local tank = peripheral.wrap("back")
- local mon = peripheral.wrap("top")
- local wmod = peripheral.wrap("bottom")
- -- Make sure channel/frequency match main computer
- local sendFreq = 2 -- Modem "Frequency"
- local warning = 50 -- Warning level in %
- local cap -- Tank capacity
- local amount -- Amount liquid in tank
- local percentfull -- Percent liquid in tank
- local lastpercent = 1000 -- Percent last loop
- local sendmsg -- Message to send
- local sleeptime -- How long to sleep
- local sendpercent -- Formatted percentage string
- -- Set warning lamp to off
- redstone.setOutput("right", false)
- function getTank(tankPeriph)
- local tableInfo = tankPeriph.getTankInfo("unknown") -- Local to the getTank function.
- fluidRaw = nil
- fluidName = nil
- fluidAmount = nil
- fluidCapacity = nil
- for k,v in pairs(tableInfo) do
- fluidCapacity = v.capacity
- if v.contents then
- for i,w in pairs(v.contents) do
- if i == "rawName" then
- fluidRaw = w
- elseif i == "amount" then
- fluidAmount = w
- elseif i == "name" then
- fluidName = w
- end
- end
- end
- end
- return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
- end
- -- Main prog loop, never stop
- while true do
- mon.clear()
- mon.setCursorPos(1,1)
- -- Fill table with data from tank valve
- local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(tank)
- if fluidName then
- -- Get values for tank capacity and amount
- fluidName = string.gsub(fluidName, "^%l", string.upper)
- cap = fluidCapacity / 1000
- amount = fluidAmount
- -- If tank is empty, to avoid math issues with 0
- if amount == nil then
- amount = 0
- percentfull = 0
- else
- -- Use math.floor to convert to integers
- amount = math.floor(amount / 1000)
- percentfull = math.floor(100 * amount / cap)
- end
- -- Self explanatory :)
- mon.write(fluidName)
- mon.setCursorPos(1,2)
- mon.write("Amount: " ..amount .."/"..cap .." B.")
- mon.setCursorPos(1,3)
- mon.write("Amount: " ..percentfull .."% ")
- -- Check for change since last loop
- if percentfull == lastpercent then
- print("Still " ..percentfull .. "%, nothing sent.")
- else
- -- If value changed, send to main!
- -- First format the left side:
- if percentfull < 10 then
- sendpercent = " " ..percentfull
- elseif percentfull < 100 then
- sendpercent = " " ..percentfull
- else
- sendpercent = 100
- end
- -- Now format the rest of the text
- if percentfull < warning then
- sendmsg = sendpercent .."% .red." ..fluidName
- else
- sendmsg = sendpercent .."% .green." ..fluidName
- end
- wmod.transmit(sendFreq,0,sendmsg)
- print("Sent: " ..sendmsg)
- end
- -- Save for next loop
- lastpercent = percentfull
- -- Warning control, local lamp
- mon.setCursorPos(1,5)
- if percentfull < warning then
- redstone.setOutput("right", true)
- mon.write("Less than " ..warning .."% full")
- sleep(1)
- redstone.setOutput("right", false)
- sleeptime = 1
- else
- -- Above warning level, sleep longer
- mon.write("More than " ..warning .."% full")
- sleeptime = 10
- end
- else
- -- If no liquid is found in tank..
- mon.write("No liquid in tank")
- term.clear()
- term.setCursorPos(1,1)
- print("No liquid in tank")
- sleeptime = 10
- end
- -- Sleep either 1 or 10 seconds
- sleep(sleeptime)
- end
Advertisement
Add Comment
Please, Sign In to add comment