Hi -
I have a for.loop() calculating measurements taken by a single sensors but utilising two different ROI’s. It does this by taking 20 measurements on each ROI and determine the averages of both ROI’s independently, which are then stored as two variables and all seems to be working well.
I now want to change this to a nested for.loop() as I want this entire process to repeat three times and store the results in an Array rather. I get the basics of an Array but am not sure how to use that in combination with the nested for loop and not overwrite existing data. I think I have the nesting correct, it is just creating the array which I assume will hypothetically look something like this after values have been populated?
int PathTrack [6] = {10,20,40,60,80,100}
If not possible, I am happy to store them as different variables, I just do not want to overwrite the values of each iteration as I need them to determine a condition.
Below is applicable section of my code as-is;
int M_distance;
int M_sum_zone_0 = 0;
int M_sum_zone_1 = 0;
int number_attempts = 5;
int PathTrack_array[6] = {0,0,0,0,0,0};
Zone = 0;
if ((sum_zone_0 <= (calibrated_zone_0 * 0.75))) {
Particle.publish("Start Measuring:", PRIVATE);
// measure 3 times //
for (int k=0; k<3; k++) {
// take [i]-number of readings and determine average //
for (int i=0; i<number_attempts; i++) {
// increase sum of values in Zone 0 //
distanceSensor.setIntermeasurementPeriod(55);
distanceSensor.setDistanceModeLong();
distanceSensor.setROI(ROI_height, ROI_width, center[0]); // first value: height of the zone, second value: width of the zone
delay(50);
distanceSensor.setTimingBudgetInMs(50);
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
M_distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
distanceSensor.stopRanging();
M_sum_zone_0 = M_sum_zone_0 + M_distance;
Zone++;
Zone = Zone%2;
// increase sum of values in Zone 1 //
distanceSensor.setIntermeasurementPeriod(55);
distanceSensor.setDistanceModeLong();
distanceSensor.setROI(ROI_height, ROI_width, center[1]); // first value: height of the zone, second value: width of the zone
delay(50);
distanceSensor.setTimingBudgetInMs(50);
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
M_distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
distanceSensor.stopRanging();
M_sum_zone_1 = M_sum_zone_1 + M_distance;
Zone++;
Zone = Zone%2;
}
average_zone_0 = M_sum_zone_0 / number_attempts;
average_zone_1 = M_sum_zone_1 / number_attempts;
Particle.publish("Zone 1: " + String(average_zone_0) + " & Zone 2:" + String(average_zone_1), PRIVATE); // DEBUG
}
}
I am pretty sure there might be a more efficient way of doing things, but this is what I was able to come up with
Getting the results from a single iteration published seems to be working fine, just want to store them now for later processing.
Many thanks,
Friedl