Weirdest array issue I have ever seen

Hello all,

Here is something that I cannot solve, I've spent something like 2h on it and I am willing to now ask for some help :smiley:

Here is the code that gets sensor data and puts it into 2 float arrays and then proceeds to make an average of those two so at the final step I just get the average values:

void getDustSensorData() {
  int dustSensorRuntime = 5;

  float tempPm10[dustSensorRuntime];
  float tempPm25[dustSensorRuntime];

  float sumPm10 = 0.0, avgPm10;
  float sumPm25 = 0.0, avgPm25;
  delay(300);
  
  my_sds.wakeup();

  Serial.println("Getting dust sendor samples: \n");
  for(i=0; i<=dustSensorRuntime; i++) {
    Serial.println("\n");
    sds011Error = my_sds.read(&tempPm25[i], &tempPm10[i]);
    
    Serial.println("Dust sensor sample: " + String(i) + "/" + String(dustSensorRuntime) + "\n");
    Serial.println("Temp P2.5: " + String(tempPm25[i]));
    Serial.println("Temp P10:  " + String(tempPm10[i])); 
    delay(1000);
  }

  for(i=0; i<=dustSensorRuntime; i++) {
   sumPm25 += tempPm25[dustSensorRuntime];
   avgPm25 = sumPm25 / dustSensorRuntime;
  }

  for(i=0; i<=dustSensorRuntime; i++) {
   sumPm10 += tempPm10[dustSensorRuntime];
   avgPm10 = sumPm10 / dustSensorRuntime; 
  }

  p25 = avgPm25;
  p10 = avgPm10;
  
  if (!sds011Error) {
    Serial.println("P2.5: " + String(p25));
    Serial.println("P10:  " + String(p10));
  }
}

Here is the output of the console:

Getting dust sendor samples:

Dust sensor sample: 0/5

Temp P2.5: 0.00
Temp P10: 0.00

Dust sensor sample: 1/5

Temp P2.5: 0.00
Temp P10: 0.00

Dust sensor sample: 2/5

Temp P2.5: 3.30
Temp P10: 5.70

Dust sensor sample: 3/5

Temp P2.5: 6.40
Temp P10: 8.70

Dust sensor sample: 4/5

Temp P2.5: 7.20
Temp P10: 11.10

Dust sensor sample: 5/5

Temp P2.5: 0.00
Temp P10: 9.90
P2.5: 0.00
P10: 11.88

The problem is that Temp P2.5 is equal to 0.0 and here is the weird part, even the average of P.25 is 0.

Lets make it weirder: if I move the declaration of my variables from this:

float tempPm10[dustSensorRuntime];
float tempPm25[dustSensorRuntime];

float sumPm10 = 0.0, avgPm10;
float sumPm25 = 0.0, avgPm25;

To this:

float tempPm25[dustSensorRuntime];
float tempPm10[dustSensorRuntime];

float sumPm25 = 0.0, avgPm25;
float sumPm10 = 0.0, avgPm10;

Now P10 will be equal to 0.0 and the average is once again 0.0.
So the problem switches to the other array... this is so weird..

Any ideas?

Cheers

You are writing past the end of the tempPM arrays. Each has 5 elements

but you are putting 6 elements into them 0,1,2,3,4,5.
i<=dustSensorRuntime;

Lose the <=, make it just <.

Same here.

2 Likes

Some issues...

As per @groundFungus you are reading beyond the bounds of the array.

Also...

Shouldn't this be using i as the index?

Also..

You only need to do this once, at the end of the for loop.

1 Like

Oops ? check your index value [i] not [dustSensorRuntime]

  for(i=0; i<=dustSensorRuntime; i++) {
   sumPm25 += tempPm25[i];
   avgPm25 = sumPm25 / dustSensorRuntime;
  }

  for(i=0; i<=dustSensorRuntime; i++) {
   sumPm10 += tempPm10[i];
   avgPm10 = sumPm10 / dustSensorRuntime; 
  }

1 Like

The compiler will give you helpful warnings if you set "Compiler warnings" to "All".

2 Likes

This will make sumPm25 equal to 5*0.

Why? You are using dustSensorRuntime as an index, and it is the size of the array. As an index, it points to a non existing element that is beyond the limits of the array. Most likely tempPm25[dustSensorRuntime] is 0. Also, it does not vary inside the for loop.

Same for sumPm10.

Use something as

1 Like

That is still writing to tempPm25[5] and tempPm10[5] which are outside the bounds of

  int dustSensorRuntime = 5;
  float tempPm10[dustSensorRuntime];
  float tempPm25[dustSensorRuntime];

Change the <= to <.

1 Like

Absolutely right!

I fell into the cracks of copy/paste :face_with_open_eyes_and_hand_over_mouth:

Thank you all for the reply, I guess I need to stop programming late at night haha

1 Like

I stopped that practice too. I don't think effectively after about 10pm :smile:

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