I finally came around the code that caused my strange compiler error.
Using the standard code exclusion technique with comment codes, I narrowed in the problem.
First I commented out my functions and function calls, one by one.
That showed me that a problem exists with my function getSpeedValue.
In here the expression
averageOfReadings = sumOfReadings / numberReadings;
causes the problem, since the compiler sails through once this expression is commented out.
The expression returns an integer, but not a rounded off integer, which I really want.
Other than that I can not figure out why the expression should cause a compiler error.
PS : I finally figured out how to submit code.
/* Cruise control logic
Adjusts a PWM outpin pin, by comparing running A/D input to an initially set value.
* The circuit :
* A CruiseControl enable input is taken from an external relay contact.
* This relay works as a bistable relay, set by the driver on a CC start switch button placed on the instrument panel.
* The relay is self contained by the LED output pin and disengaged by either a clutch or brake pedal switch, or both.
* The same relay is used to switch the speed signal between Throttle Pedal Sensor (TPS) value and the CC throttle
* pedal value.
* Extensive use of external protection circuits is a must, to protect the Arduino board against reverse or over voltages.
*/
// never changes, except at compile time on sketch revisions
const int analogInputPin = A0; // Analog input A0 (connector pin 12)
const int PWMoutputPin = 9; // Digital pin D9 output with PWM capability (connector pin 19)
const int LEDoutputPin = 13; // Digital pin D13 output with onboard LED (connector pin 16) for enabling external relay
const int enableInputPin = 4; // Digital pin D5 input (connector pin 23)
const int numberOfReadings = 5; // we dont need more than 5 analog readings for smoothing (to be experimented)
const int sizeOfIncrement = 3; // the maximum rate of change of speed (to be experimented)
const unsigned long debounceDelay = 100; // the maximum number of readings to decide a stable relay contact input (to be experimented)
const int initialPWMout = 13; // initial value of PWM output pin to set a start point greater than 0, 13 = 5% duty cycle
const int maximalPWMout = 251; // maximal value of PWM output pin that will cause a CC droput, 255 = 100% duty cycle
// working global variables
int PWMoutputValue = initialPWMout; // PWM speed output value
int analogInputValue; // holds the current speed reading
int analogInputReference; // holds a speed set reading for reference
int readingsArray[4]; // array holding 5 speed readings (incl index 0) from the analog input
boolean cruiseControlEnable = false; // CC active flag
void setup()
{
pinMode(enableInputPin, INPUT); // prepare the digital enable input
pinMode(LEDoutputPin, OUTPUT); // enable the onboard LED on D13 output pin
pinMode(PWMoutputPin, OUTPUT); // prepare the PWM output
for (int arrayIndex = 0; arrayIndex < numberOfReadings; arrayIndex++) readingsArray[arrayIndex] = 0; // initialize the 5 array places 0 - 4 holding the analog readings
}
void loop()
{
if (cruiseControlEnable == false) // CC is not enabled
{
cruiseControlEnable = getDebounced(debounceDelay);
analogInputReference = getSpeedValue(numberOfReadings); // keep speed reference value updated for instant use if CC is enabled
}
else // CC enabled : calculate speed difference & send a corrected PWM output as throttle pedal value to maintain speed
{
analogInputValue = getSpeedValue(numberOfReadings); // get current speed value
PWMoutputValue = maintainSpeed(analogInputReference, analogInputValue, PWMoutputValue, sizeOfIncrement); // maintain speed
cruiseControlEnable = engineOverload(PWMoutputValue, cruiseControlEnable, initialPWMout, maximalPWMout); // drop out on overload
}
}
int getSpeedValue(const int numberReadings) // read analog input numberOfReadings times for smoothing
{
// local variables
int sumOfReadings; // the running sum of analog readings
int averageOfReadings; // the average of the readings
for (int arrayIndex = 0; arrayIndex > numberReadings - 1; arrayIndex++)
{
sumOfReadings -= readingsArray[arrayIndex]; // subtract the last reading
readingsArray[arrayIndex] = analogRead(analogInputPin); // read from the sensor
sumOfReadings += readingsArray[arrayIndex]; // add the reading to the sum Of Readings
delay(1); // delay in between readings for stability
}
averageOfReadings = sumOfReadings / numberReadings; // calculate the average Of Readings
return averageOfReadings;
}
// this function wont work as the initial millis() reading is not saved in a global, to be corrected
boolean getDebounced(const unsigned long debounceDelayTime) // read the state of the relay contact, that enables the CC
{
// local variables
unsigned long lastDebounceTime; // debounce time counter
boolean enableInputReading; // holds the current reading of enable input
boolean lastEnableInputValue; // the last actual state of enable input
enableInputReading = digitalRead(enableInputPin); // read the enable input value into current reading
if (enableInputReading != lastEnableInputValue) lastDebounceTime = millis(); // If the input changed, due to noise or state change, reset the debouncing timer
if (millis() - lastDebounceTime >= debounceDelayTime) // if the reading has been stable for the debounce time, save it as correct state
{
lastEnableInputValue = enableInputReading; // save the reading input pin state
digitalWrite(LEDoutputPin, enableInputReading); // turn on the LED if CC is enabled
}
return enableInputReading; // return debounced state
}
int maintainSpeed(int reference, int speedValue, int PWMvalue, const int increment) // correct any speedchanges
{
// local variables
int difference; // holds how far the speed is from reference
if (reference != speedValue) // speed has changed, correct it
{
difference = speedValue - reference;
if (difference < 0) // speed is decreasing, get it up
{
if (difference < !increment) PWMvalue += increment; // increase throttle value
else PWMvalue++;
}
if (difference > 0) // speed is increasing, get it down
{
if (difference > increment) PWMvalue -= increment; // decrease throttle value
else PWMvalue--;
}
analogWrite(PWMoutputPin, PWMvalue); // update throttle pedal value
}
return PWMvalue;
}
boolean engineOverload(int PWMvalue, boolean CCenable, int initialPWM, const int maxiPWM) // full throttle does not change speed
{
if (PWMvalue > maxiPWM) // do we have a bottom throttle pedal value ?
{
CCenable = false;
analogWrite(PWMoutputPin, initialPWM); // set the throttle pedal value to 0
digitalWrite(LEDoutputPin, 0); // shut off the external relay and the indicator LED
}
return CCenable;
}