Help with example code read line sensors

[size=85]I understand most of the code here except when I get to the numsensor line. Thanks guys for any help you can provide.[/size]

time = 0;
last_time = TCNT2;
while (time < _maxValue)
{
// Keep track of the total time.
// This implicity casts the difference to unsigned char, so
// we don’t add negative values.
unsigned char delta_time = TCNT2 - last_time;
time += delta_time;
last_time += delta_time;
// continue immediately if there is no change
if (PINC == last_c)
continue;
// save the last observed values
last_c = PINC;
// figure out which pins changed
for (i = 0; i < _numSensors; i++) <-- Here is where I get a little lost… What does ‘i’ stand for
and is it going through all of the sensors?
{
if (sensor_values[i] == 0 && !(*_register[i] & _bitmask[i])) <-- And this too. Is there a place I can go to find info on this?
sensor_values[i] = time;
}
}

Hello.

i is an iteration variable that lets us loop once for each sensor. The _register and _bitmask arrays store the PIN register associated with each sensor and the bitmask that isolates the particular register bit that corresponds to each sensor we’re using. For example, if we have a sensor connected to pin PC2, bit 2 of register PINC tells us whether the sensor’s I/O line is high or low. We can check the state of bit 2 of register PINC with the following logic:

if (PINC & (1 << 2))
// the I/O line is high
else
// the I/O line is low

Here we use (1 << 2) as the bitmask.

The second line that confused you sets the discharge time for a sensor if that sensor has not yet had its discharge time set (i.e. if sensor_values[i] == 0) and if the I/O pin the sensor is connected to reads low (i.e. if (*_register[i] & _bitmask[i]) == 0).

Does this help clear things up, or is it bitwise operations in general that are unfamiliar to you?

- Ben

That helps but I guess there are just some procedural steps that confuse me.

for (i = 0; i < _numSensors; i++)
^here it sets i to 0 and then it checks i against _numSensors where is _numSensors set? I am guessing that it is the number of sensors but where in the code is that set?
and i++ what does that do?

Sorry I am a bit of a newbie…I have always programed my atmel chips before in assembly… believe it or not that is easier for me. it takes a lot of writing to do something but there is less to deal with.

I know that this will be better a faster once I get over the first few hurdles. Is there some suggested reading for programing in C for microcontrollers so I don’t have to bug you guys??? thanks for all your help.

i++ increments the value of i by one and then stores it back in i. It is a quick way of doing:

i = i + 1;

If you look through the Pololu AVR library code you should be able to find a line that sets _numSensors. If I remember right, it is a global variable that probably gets set by the QTR init function.

For a basic introduction to the C language, take a look at this C tutorial (you can skip sections on C++).

You also should take a look at nexisnet’s post in this forum thread:

- Ben

Cool thanks for your help those links you posted are just what I was looking for. Thanks again!! -B