Why can't I call a function more than 5 times within an 'if' loop

My program controls the speed of a diesel engine.
To monitor the RPM I call a function
void measureThrottle (int throttle)
{
throttle = analogRead(A2);
}

I'm finding that I can't call the function more than 5 times within the main 'if else' loop.
If I compile the program with more than 5 calls to the function, it simply stops working.

This has occurred with a second function... same problem can't call the function more than 5 times

Please post ALL the code, using code tags, along with some evidence for your claim.

It's a do-nothing function anyway. It takes the parameter, assigns it to a local variable 'throttle', assigns an analogRead() to it, and then throws the result away as it returns. I would be willing to bet, the rest of the code is full of such errors.

Good point! There is no reason to call that function, ever.

Ok thanks for the comments but I’m still at a loss. The function’s job is to read the analog input and return with a value in the variable ‘throttle’ . At the end of this listing is the analog read ‘spelt out’ in the code because calling the function the fifth time crashes.

if (digitalRead(engineRunning)== HIGH ) //Start governing speed.
{ //Engine is running. Ignition switch is ON

isEngineRunning = true;
actuatorSpeed = actuatorMaxSpeed;

readRPM(engineRevs);

measureThrottle(throttle);

if (engineRevs < throttle)// Engine speed too slow Retract actuator
{
readRPM(engineRevs);

measureThrottle(throttle);

difference = throttle - engineRevs ;

if (difference < -tolerance1 || difference > tolerance1)
{

readRPM(engineRevs);
measureThrottle(throttle);

difference = throttle - engineRevs ;

digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
retractActuator();
}
delay(25);
stopActuator();
digitalWrite(yellowled, HIGH);
}
else
if (engineRevs > throttle)// Engine speed too fast Extend actuator
{
readRPM(engineRevs);

measureThrottle(throttle);

difference = throttle - engineRevs ;

if (difference < -tolerance1 || difference > tolerance1)
{

readRPM(engineRevs);

difference = throttle - engineRevs ;

digitalWrite(redled, HIGH);
digitalWrite(greenled,LOW );
extendActuator();
}
delay(25);
stopActuator();
digitalWrite(yellowled, HIGH);
}

throttle = analogRead(A2); // Read throttle potentiometer value
throttle = (throttle / 10) * 10;

}

}

See reply #1

You need to learn how to write and call functions that return values. Here is just one of many on line tutorials.

The function's job is to read the analog input and return with a value in the variable 'throttle' .

Then you should have passed the variable as a pointer or reference.

Many thanks. I can see where I've gone wrong now however, strangely, the program worked until the fifth call. lol Thankyou all for your help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.