Line Sensors Sparkfun 11769
/*
Reflectance Sensor test for three(3) Sparkfun https://www.sparkfun.com/products/11769 line sensors
written by Diane Brancazio [email protected] and Jonathan Dietz [email protected]
Reads in analog data on pins A0, A1, A2 and prints the result to the serial monitor.
Sensor should be 1/8" (0.125) from the surface it is reading.
For a moving device (such as a line-following robot) the surface must be very flat.
Ambient light, such as sunlight, will affect the readings.
Connect an 8 ohm speaker to pin 6 and Gnd to hear the sensor reading as tones
Note: Tone command not compatible with use of ultrasonic distance sensor
*/
#define rightSensorPin A0
#define middleSensorPin A1
#define leftSensorPin A2
#define speakerPin 6
#define BLACKlimit 700 // typical range white > 100, black <800-- may need to be adjusted
#define WHITElimit 100 // may need to be adjusted
int rightSensor;
int middleSensor;
int leftSensor;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
// read the input on analog pin 0, 1, 2:
rightSensor = analogRead(rightSensorPin);
middleSensor= analogRead(middleSensorPin);
leftSensor= analogRead(leftSensorPin);
// print out the value you read:
Serial.print(leftSensor);
Serial.print(" "); Serial.print(middleSensor);
Serial.print(" "); Serial.println(rightSensor);
Serial.println();
/* Play tones when sensors are over black line (read back < 200) */
if (rightSensor > BLACKlimit)
{
tone(speakerPin, 440,100); // tone (pin, note in Hz, time played)
}
if (middleSensor > BLACKlimit)
{
tone(speakerPin, 480,100); // tone (pin, note in Hz, time played)
}
if (leftSensor > BLACKlimit)
{
tone(speakerPin, 580,100);
}
delay(200); // delay in between reads for stability
}
Reflectance Sensor test for three(3) Sparkfun https://www.sparkfun.com/products/11769 line sensors
written by Diane Brancazio [email protected] and Jonathan Dietz [email protected]
Reads in analog data on pins A0, A1, A2 and prints the result to the serial monitor.
Sensor should be 1/8" (0.125) from the surface it is reading.
For a moving device (such as a line-following robot) the surface must be very flat.
Ambient light, such as sunlight, will affect the readings.
Connect an 8 ohm speaker to pin 6 and Gnd to hear the sensor reading as tones
Note: Tone command not compatible with use of ultrasonic distance sensor
*/
#define rightSensorPin A0
#define middleSensorPin A1
#define leftSensorPin A2
#define speakerPin 6
#define BLACKlimit 700 // typical range white > 100, black <800-- may need to be adjusted
#define WHITElimit 100 // may need to be adjusted
int rightSensor;
int middleSensor;
int leftSensor;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
// read the input on analog pin 0, 1, 2:
rightSensor = analogRead(rightSensorPin);
middleSensor= analogRead(middleSensorPin);
leftSensor= analogRead(leftSensorPin);
// print out the value you read:
Serial.print(leftSensor);
Serial.print(" "); Serial.print(middleSensor);
Serial.print(" "); Serial.println(rightSensor);
Serial.println();
/* Play tones when sensors are over black line (read back < 200) */
if (rightSensor > BLACKlimit)
{
tone(speakerPin, 440,100); // tone (pin, note in Hz, time played)
}
if (middleSensor > BLACKlimit)
{
tone(speakerPin, 480,100); // tone (pin, note in Hz, time played)
}
if (leftSensor > BLACKlimit)
{
tone(speakerPin, 580,100);
}
delay(200); // delay in between reads for stability
}
Challenge: Start by using a single sensor as an edge detector to make the robot follow a line- turn by setting one motor on + other motor varying amounts off. Then see pages on Line Following Algorithms for using multiple sensors for more complex algorithms.