# Code

Hello The below code is from following function.
void PololuQTRSensors::calibrateOnOrOff(unsigned int **calibratedMinimum,
unsigned int **calibratedMaximum, unsigned char readMode)

``````// record the min and max calibration values
for(i=0;i<_numSensors;i++)
{
if(min_sensor_values[i] > (*calibratedMaximum)[i])
(*calibratedMaximum)[i] = min_sensor_values[i];
if(max_sensor_values[i] < (*calibratedMinimum)[i])
(*calibratedMinimum)[i] = max_sensor_values[i];
}
``````

Is above code correct? I am bit confused. What is the initial values of arrays
*calibratedMaximum[i] and * calibratedMinimum[i] ? Is it 0 and 1023?
Could you please explain to me?

Regards
Jagdish

Hello,

I think the code is correct.

There is code several lines up in that function that initialized the minimum to 0 and the maximum to _maxValue. The point of this whole function is to update the minimum and maximum sensor values while excluding occasional spurious readings. So we do 10 readings, and if ALL of them are greater than our current maximum, we update the maximum. If ALL of them are lower than our current minimum, we update the minimum. Determining whether all are higher or lower than a certain point is done by calculating the minimum and maximum of the 10 readings.

-Paul

``````// Initialize the max and min calibrated values to values that
// will cause the first reading to update them.

for(i=0;i<_numSensors;i++)
(*calibratedMaximum)[i] = 0;
/*********** here initializing Maximum to 0 *****************************/

}
if(*calibratedMinimum == 0)
{
*calibratedMinimum = (unsigned int*)malloc(sizeof(unsigned int)*_numSensors);

// If the malloc failed, don't continue.
if(*calibratedMinimum == 0)
return;

for(i=0;i<_numSensors;i++)
(*calibratedMinimum)[i] = _maxValue;
/**************** here Minimum to 1023 *************************************/
/****** therefor I am bit confuse how the code works???   *****************/
}

int j;
for(j=0;j<10;j++)
{
for(i=0;i<_numSensors;i++)
{
// set the max we found THIS time
if(j == 0 || max_sensor_values[i] < sensor_values[i])
max_sensor_values[i] = sensor_values[i];

// set the min we found THIS time
if(j == 0 || min_sensor_values[i] > sensor_values[i])
min_sensor_values[i] = sensor_values[i];
}
}

// record the min and max calibration values
for(i=0;i<_numSensors;i++)
{
if(min_sensor_values[i] > (*calibratedMaximum)[i])
(*calibratedMaximum)[i] = min_sensor_values[i];
/********** I am still having a bit problem to understand this logic **********************/

if(max_sensor_values[i] < (*calibratedMinimum)[i])
(*calibratedMinimum)[i] = max_sensor_values[i];
}
}
``````

Sorry, I said it wrong. The minimum is initialized to _maxValue and the maximum is initialized to 0. This ensures that the first measurement will cause both minimum and maximum to be updated.

-Paul