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;
}
}