Page 1 of 1

Im going insane. Help required geeting 2 scripts to work

Posted: Tue Sep 26, 2017 11:40 am
by Taner
Hi All.

I have a script running currently that is very simple and turns my fans on and off sepeding on coolant temp.

What I want to do is run this script and Another which just filters my fuel level reading.

My problem is that when I try to run both scripts my fans stop working. its like the script is being ignored.


Here is the fan script and below that is what I was trying to run to allow both functions to work at the same time



function onTick()

if getAnalog(1) > 90
then
setGpio(0, 1)
else
setGpio(0, 0)
end
end





AND


function getCoolantTemp()
if getAnalog(1) > 90
then
setGpio(0, 1)
else
setGpio(0, 0)
end
end

function updateFuelAvg(value)
local i
if #fuelAvg == 0 then
--initialize averaging table
for i = 1, maxAvg do fuelAvg=0 end
end
fuelAvg[fuel2Index] = value
fuel2Index = fuel2Index + 1
if fuel2Index > maxAvg then fuel2Index = 1 end
local sum = 0
for i = 1, #fuelAvg do
sum = sum + fuelAvg
end
setChannel(fuel2Id, sum / maxAvg)
end


setTickRate(30)
function onTick()
getcoolantTemp()
updateFuelAvg(getAnalog(0))
end





Any help would be greatly appreciated

Posted: Tue Sep 26, 2017 2:12 pm
by ross2004
I think your problem lies with two "function onTick()" commands. That's all I've got.

Posted: Tue Sep 26, 2017 2:14 pm
by psfp
Is your fuel average function working? Where did you set the maxAvg value? And where did you initialize the fuel2Index variable?

Your idea is to smooth the fuel reading, is it right? You can try this setting as well:

viewtopic.php?p=25368&highlight=alpha#25368

Posted: Tue Sep 26, 2017 5:44 pm
by Taner
Hi Ross

You may be right but I have no clue how else to write it.



PSFP. Yes the fuel averaging works a treat but if combined with my fan swithing script it seems to stop that working??

Just by creating the script it automatically creates Avg (filtered) channel for fuel.

Posted: Tue Sep 26, 2017 5:56 pm
by gizmodo
The problem is almost certainly two onTick functions. Get rid of the top onTick function

Code: Select all

function onTick() 
    getcoolantTemp() 
    updateFuelAvg(getAnalog(0)) 
end
The onTick method is what the Lua engine executes every N (the value of setTick) milliseconds. Defining it twice will cause problems. Copy and paste the entire script that isn't working, and only that script so we can actually see what you have.[/code]

Posted: Tue Oct 03, 2017 6:33 pm
by wizrd54
gizmodo wrote:The problem is almost certainly two onTick functions. Get rid of the top onTick function

Code: Select all

function onTick() 
    getcoolantTemp() 
    updateFuelAvg(getAnalog(0)) 
end
The onTick method is what the Lua engine executes every N (the value of setTick) milliseconds. Defining it twice will cause problems. Copy and paste the entire script that isn't working, and only that script so we can actually see what you have.[/code]
I think this is the answer as well. You should define each function separately and call all of your functions in the single onTick function. I believe onTick is what executes all of your custom scripts.