SparkFun Line Follower Array Hookup Guide
Core Functions of the Arduino Library
The basic library has the following parts
Object construction
Each instance of the SensorBar library needs to be constructed in the global scope for all to access.
Arguments:
Pass the device address in HEX ( unsigned 8 bit ).
Example:
For the default address of 0x3E:
SensorBar mySensorBar( 0x3E );
begin()
Use begin to start the sensor bar's I2C expander. This use Wire.h under the hood.
begin() takes no arguments.
Return:
Returns success message (unsigned 8 bit).
returns 1 if it was able to communicate with the sensor bar or 0 if it had troubles. In the example ReadBarOnly the sketch is held if the sensor did not respond.
After .begin();
has been run, the sensor bar is ready to start reading data. Place it in the setup() section to run once.
Example:
language:c
uint8_t returnStatus = mySensorBar.begin();
if(returnStatus)
{
Serial.println("sx1509 IC communication OK");
}
else
{
Serial.println("sx1509 IC communication FAILED!");
}
Serial.println();
getRaw()
Get a reading from the array as a single 8 bit word where each bit represents an IR sensor. If the sensor's lights show: ON, ON, ON, ON, OFF, ON, ON, ON, this function will return 0xF7 matching the bit positions on the silkscreen. If InvertBits
has been set though, the result will be 0x08.
Notice that the silkscreen labels b7 through b0 represent the same bits of this raw data.
getRaw() takes no arguments.
Return:
Returns the states of the IR detectors, as bits of a byte (unsigned 8 bit).
Example:
language:c
//Get the data from the sensor bar.
uint8_t rawValue = mySensorBar.getRaw();
Get the states and place in the temporary variable rawValue
getPosition()
Use to get the data as as a vector to the average of detected points. Returns a signed 8 bit number, -127 to 127.
For example, if the center two bits (b4 and b3) detect line, the average will be 0, or centered. If the left four (b7 through b4) detect line, the result will be an average to the left (-79). If only the left most sensor detects line, b7, the result will be -127. Use the silkscreen axis on the sensor bar to assist interpretation.
Note that if InvertBits
does not match the line/field colors, the result of getPosition() will not have meaning. This is because multiple detected positions get averaged to come up with a vector, and an inverted setting means basically all the positions are detected and average near zero (center).
getPosition() takes no arguments.
Return:
Position as a signed 8 bit integer, ranged: -127 to 127.
Example:
language:c
//Print the position
Serial.print("Position (-127 to 127): ");
Serial.println(mySensorBar.getPosition());
getDensity()
Use to get the number of sensors that are detecting a line.
This is useful for detecting validity of the perceived line or to detect stop conditions, such as if the robot has been picked up.
getDensity() takes no arguments.
Return:
Returns an 8-bit unsigned integer ranged 0 through 8.
Example:
language:c
//Print the density
Serial.print("Density, bits detected (of 8): ");
Serial.println(mySensorBar.getDensity());
setBarStrobe() and clearBarStrobe()
Use to turn on and off the bar's IR strobing to save power.
setBarStrobe() and clearBarStrobe take no arguments and return void.
Example:
language:c
//For this demo, the IR will only be turned on during reads.
mySensorBar.setBarStrobe();
//Other option: Command to run all the time
//mySensorBar.clearBarStrobe();
Also note that a read operation takes 2-3x longer with BarStrobe set, as the library has to enable and disable the LEDs. If extremely rapid reads are required, clear the BarStrobe.
setInvertBits() and clearInvertBits()
Use to reverse the perceived line/field color scheme. With inversion cleared, the sensor is looking for a dark line on light background. With inversion set, it looks for a light line on a dark background.
setInvertBits() and clearInvertBits() take no arguments and return void.
Example:
language:c
//Default dark on light surface
mySensorBar.clearInvertBits();
//Other option: light line on dark
//mySensorBar.setInvertBits();