# Code

Hello

I asked you previously regarding calibratedMaximum[5] and calibratedMinimum[5] values.
I got minimum value in calibratedMaximum[5] and maximum in calibratedMinimum[5] .
I have simulated your code in simple format as belows.

Could you please explain to me why I am getting other way round.
I bought 3pi last week and going to try. First thing is I have to download gnu compiler and figure it out what to do. I will let you know asap.
Regards
Jagdish

``````/********************** code start from here*****************************************/
unsigned int  max_sensor_values[5];  // storage for max & min sensor values
unsigned int  min_sensor_values[5];

unsigned int  calibratedMaximum[5];
unsigned int  calibratedMinimum[5];

unsigned int  sensor_values[5];

unsigned int code  adc_values[10][5] = {
{ 200, 200, 900, 200, 200},
{ 200, 200, 400, 700, 200},
{ 200, 200, 200, 900, 200},
{ 200, 200, 200, 200, 900},
{ 200, 200, 200, 200, 1000},
{ 200, 220, 615, 915, 200},
{ 200, 200, 915, 200, 200},
{ 200, 905, 200, 200, 200},
{ 913, 200, 200, 200, 200},
{ 200, 200, 890, 200, 200}
};

/*********************************************************************************************************
Description: Reads the sensors 10 times and uses the results for calibration. The sensor values are not
returned; instead, the maximum and minimum values found over time are stored internally
and used for the readCalibrated() method.
**********************************************************************************************************/
//void calibrate_line_sensors(void)
void main(void)
{
int i;
int j;
static int r = 0;

unsigned int maxValue = 1023;

/* step 1 */
// emitter_on

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

for(i=0;i<5;i++)
{
calibratedMaximum[i] = 0;  /* RESET BUFFER */
}

for(i=0;i<5;i++)
{
calibratedMinimum[i] = maxValue; /* STORE MAX VALUE 1023 */
}

for(j=0;j<10;j++)
{
//        IR_EMITTER = ON; /* switch emitter ON */
//        IR_EMITTER = OFF; /* switch emitter OFF */
for(i=0;i<5;i++)
{
}
r++;  // point to next row of adc values

for(i=0;i<5;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<5;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];
}
}
}

/*****************************end****************************************************/
``````

Hello,

I reformatted your post so that I could look at the code. Please use the “Code” button in the future if you want people to look at your code.

Anyway, it looks like you have simulated a single call to the calibration function, which does indeed put the maximum sensor values in calibratedMinimum and the minimum sensor values in calibratedMaximum. The idea is that this set of readings comes from each sensor looking at a single spot, and any different between minimum and maximum values just comes from sensor noise - you are supposed to continue calling this function multiple times as your robot moves over regions of different colors, allowing a much greater range of values to be measured, which will set calibratedMinimum and calibratedMaximum to their proper values.

-Paul

Hello
Will you be able to give me one set of another reading which will change the min and max reading?
I still can see how this code is going to change the min and max reading.

Thanks

Jagdish

Hello,

Let’s just think about a single sensor for a moment. Suppose you call calibrate 5 times, and the readings it gets are something like this:

1. 100,101,99,103,100
2. 103,102,100,101,98
3. 500,505,504,502,499
4. 499,496,999,504,504
5. 102,100,101,100,15

After each step, the min and max will be updated:

1. min = 103, max = 99
2. min = 103, max = 99
3. min = 103, max = 499
4. min = 103, max = 496
5. min = 102, max = 496

The extra step that makes it look backwards to you prevents spurious values like the 999 and the 15 from affecting the calibration. If you can understand that, you can understand the code.

-Paul