# Readline() function for QTR8A sensor

I don’t understand how the readline() function computes a weighted average of the sensor readings. I see that the formula for the weighted average in the QTR8A sensor documentation is given as:

## 0value0 + 1000value1 + 2000*value2 + …

`````` value0  +  value1  +  value2 + ...
``````

What are the value0, value1, etc. here? Are those the readings reported by each sensors or are they indices?

For example, if the value returned by readline(…) function is 1650, what should the value0, value1, value2, etc. should be?

Hello.

The values in the calculation for the readline() function are the sensor values. As explained in the “QTR Reflectance Sensors” section of the Pololu AVR library command reference, higher sensor values correspond to lower reflectance (e.g. a black line). The readline() function uses these values and returns an integer corresponding to the position of a black line under the sensor array with the first sensor in the array mapping to a return value of 0 and the last sensor in the array mapping to a value of (number of sensors)×1000. For example, if the readline() function returns a value of 1650 then the line is between the second and third sensors in your array.

- Grant

Grant, thanks for clarifying that. My confusion primarily stemmed from the following line in documentation for the readline(…) function at pololu.com/docs/0J19/all:

[quote]
The estimate is made using a weighted average of the sensor indices multiplied by 1000, so that a return value of 0 indicates that the line is directly below sensor 0 (or was last seen by sensor 0 before being lost), a return value of 1000 indicates that the line is directly below sensor 1, 2000 indicates that it’s below sensor 2, etc. Intermediate values indicate that the line is between two sensors.[/quote]

The word indices put me off-track. It should really be values. No?

I have a related question. If the black line spans across more than two sensors, such as when it is wide enough to cover, say 6 sensors, how can we say that the line below the sensor array is between two sensors?

The usage of “indices” in that sentence is correct; the “indices” refer to the position of each value in the array where they are stored. However, we will keep your feedback in mind.

I suspect the readline() function would return a value corresponding to the center of your thick line. However, the readline() function is probably not appropriate for that situation. If you need to detect a thicker line, you might consider buying individual QTR sensors and spacing them more appropriately for your line.

- Grant