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