Variable value does not make sense

Hi,

I have this small app that collects data from the cars CAN bus and calculates consumed fuel and traveled distance.

Part of the sketch that is causing issue is this one:

unsigned long consumedFuel = 0;
unsigned long obdSignalValues[5] = {0,0,0,0,0};

void fuelCalculator() {
if (nextFuelCalculation < millis()) {
if (obdSignalValues[1] > 0) {
consumedFuel = consumedFuel + obdSignalValues[1];
obdSignalValues[2] = consumedFuel/109800;
}
nextFuelCalculation = 100 + millis();
}
}

Another function updates the integer value from the CAN bus to the obdSignalValues[1] variable and the this fuelCalculator() adds it up to obdSignalValues[2] variable to get cumulative consumed fuel.

Only problem is that sometimes obdSignalValues[2] value gets bigger normally as it should while driving the car but suddenly the value goes to a big negative number like -2109597193.

What could be causing this, and how to troubleshoot - it does not happen all the time but quite frequently.

Of course there is other stuff in the sketch too.

After compiling the sketch I get this kind of output from the Arduino IDE (using Arduino Mega 2560):

Sketch uses 11,116 bytes (4%) of program storage space. Maximum is 253,952 bytes.
Global variables use 5,520 (67%) of dynamic memory, leaving 2,672 bytes for local variables. Maximum is 8,192 bytes.

sometimes obdSignalValues[2] value gets bigger normally as it should while driving the car but suddenly the value goes to a big negative number like -2109597193.

In theory that is impossible as an unsigned long cannot have a negative value.

What does the magic number 109800 relate to ?

Try printing the intermediate values in the calculation to help find the source of the problem.

Posting your whole program (in code tags) would help to understand what you are doing and how.

obdSignalValues[2] value gets bigger normally as it should while driving the car but suddenly the value goes to a big negative number like -2109597193

Well it can't do that because it is unsigned. Perhaps you are assigning it to somewhere else? You will get better answers if you post all your code.

void fuelCalculator() {
  if (nextFuelCalculation < millis()) {
    if (obdSignalValues[1] > 0) {
      consumedFuel = consumedFuel + obdSignalValues[1];
      obdSignalValues[2] = consumedFuel/109800;
    }
    nextFuelCalculation = 100 + millis();
  }
}

You should not be adding times. The first if statement looks strange to me. It look like the code is calculating fuel use way too often.

  if(lastFuelCalculation - millis() >= fuelCalculationInterval)
  {
     // calculate fuel use

     lastFuelCalculation = millis();
  }

is the proper way to do things at regular intervals.