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

if tostring(v)>="50000" then... - Triggers at 500 and 5000?

Started by fresser, 15 September 2012 - 01:48 PM
fresser #1
Posted 15 September 2012 - 03:48 PM
Can someone help me? Im trying to make my combustion engines to turn off automatic when it gets to hot (Heat level > 50000)
Whole script: (Still a beta)
os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
rs.setOutput("left",true)

function printDict(data)
for i,v in pairs (data) do
print(tostring(i).." - "..tostring(v))
end
end

ctrl = sensors.getController()
print("SensorController befinder sig på siden: "..ctrl)
data = sensors.getSensors(ctrl)
print("———")
printDict(data)
bcSensor = data[1]
print("———")

data = sensors.getSensorInfo(ctrl,bcSensor)
printDict(data)
print("———")

sensors.setSensorRange(ctrl,bcSensor,"10")

data = sensors.getProbes(ctrl,bcSensor)
printDict(data)
print("———")
cbEngineProbe = data[2]

function getHeatLevel()
for b=1,12 do
data = sensors.getAvailableTargetsforProbe(ctrl,bcSensor,cbEngineProbe)
target = data
sensors.setTarget(ctrl,bcSensor,target)

data = sensors.getSensorReadingAsDict(ctrl,bcSensor,target,cbEngineProbe)

for i,v in pairs (data) do
if tostring(i)=="engine.heat" then
print(:)/>/>
print(tostring(v))
if tostring(v)>="50000" and tostring(v)~="500" then
rs.setOutput("left", false)
print("NØDLUKNING")
error()
end
end
end
end
end
while true do
getHeatLevel()
sleep(1)
end
GopherAtl #2
Posted 15 September 2012 - 03:56 PM
String comparison used alphabetic sorting, so "6" is greater than "5000", like "b" would sort greater than "apple". You should be comparing as numbers, not strings. You can use tonumber() if you need to convert a string to a number.
fresser #3
Posted 15 September 2012 - 04:29 PM
String comparison used alphabetic sorting, so "6" is greater than "5000", like "b" would sort greater than "apple". You should be comparing as numbers, not strings. You can use tonumber() if you need to convert a string to a number.
Thank you :)/>/> - My mom is a programmer so i didn't know why is wasn't working without the tonumber so she told me i could try to set it into "" and it worked… Until it got under 10000…

I apreciate your help