This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
grom79's profile picture

Computer Toggling Ender Tank (W/ Rs) At Certain Amount

Started by grom79, 04 October 2013 - 01:35 PM
grom79 #1
Posted 04 October 2013 - 03:35 PM
Hello. I wrote that code for a Computer to connect to Ender Tank on it's left and to empty part of it's content (Ender Tanks auto-eject when they receive redstone signal):
Spoiler
local p = peripheral.wrap("left")
local TankInfo = p.getTanks("left")

--[[
for key,value in pairs(TankInfo) do
			    for key2,value2 in pairs(value) do
							   print(key.."| "..key2..": "..tostring(value2))
			    end
end
--]]

function checkAmount()
			    for i,j in pairs(TankInfo) do
							   TankSize = j["amount"]
			    end
end
			   
function Tank()
			    checkAmount()
			    print(TankSize)
			    if  TankSize > 14000 then
							   redstone.setOutput("left", true)
							   sleep(5)
							   redstone.setOutput("left", false)
			    elseif TankSize < 1400 then
							   redstone.setOutput("left", false)
							   sleep(5)
			    end
			    TankSize = 0
end

while true do
			    checkAmount()
			    Tank()
			    sleep(60)
end

It's reading that amount correctly and emptying… Only on first time. The next, it's printing that same value all over again and it's still toggling that redstone (no more fluid) or is not doing anything (overflow on the other side). What am I doing wrong here? I though about making TankSize as local variable, but I don't know how, since I have to use a function to even get that value, and getting out of that function deletes local, if I remember correctly (and if Computer saying "nil" after try with that is telling me correctly).
MR_nesquick #2
Posted 04 October 2013 - 03:50 PM
put local TankInfo = p.getTanks("left") inside the while loop. you need to refresh the info stored on TankInfo variable to get the correct measurement
grom79 #3
Posted 05 October 2013 - 05:36 AM
Put that in while loop below. I get:
"Tank:12: bad argument: table expected, got nil"

Isn't putting that in "while" just getting that one thing rewritten? It just refresh "Tankinfo" word, not anything set to that word, if I understand correctly, just like "local p = peripheral.wrap("left")" is just giving you kind of shortcut for other functions.

Okay, I got this. I had to put it not in "while" loop, but in the checkAmount() function.