Datasheet

Robot Control with Distance Detection • Chapter 8
Robotics with the BOE Shield-Bot 251
In this example, an object is in Zone 3. That means that the object can be detected when
38000 and 39000 Hz is transmitted, but it cannot be detected with 40000, 41000, or 42000
Hz. If you were to move the object into Zone 2, then the object would be detected when
38000, 39000, and 40000 Hz are transmitted, but not with 41000 or 42000 Hz.Z
The
irDistance function from the next sketch performs distance measurement using the
technique shown above. Code in the main loop calls
irDistance and passes it values for a
pair of IR LED and receiver pins. For example, the
loop function uses int irLeft =
irDistance(9, 10)
to check distance to an object in the left side of the BOE Shield-Bot’s
detection zone “field of vision.”
// IR distance measurement function
int irDistance(int irLedPin, int irReceivePin)
{
int distance = 0;
for(long f = 38000; f <= 42000; f += 1000) {
distance += irDetect(irLedPin, irReceivePin, f);
}
return distance;
}
The irDetect function returns a 1 if it doesn’t see an object, or a zero if it does. The
expression
distance += irDetect(irLedPin, irReceivePin, f) counts the number of
times the object is not detected. After the
for loop is done, that distance variable stores
the number of times the IR detector did not
see an object. The more times it doesn’t see an
object, the further away it must be. So, the distance value the function returns represents
the zone number in the drawing above.