49 lines
1.7 KiB
Arduino
49 lines
1.7 KiB
Arduino
|
#include <QTRSensors.h>
|
||
|
|
||
|
// This example is designed for use with six QTR-1A sensors or the first six sensors of a
|
||
|
// QTR-8A module. These reflectance sensors should be connected to analog inputs 0 to 5.
|
||
|
// The QTR-8A's emitter control pin (LEDON) can optionally be connected to digital pin 2,
|
||
|
// or you can leave it disconnected and change the EMITTER_PIN #define below from 2 to
|
||
|
// QTR_NO_EMITTER_PIN.
|
||
|
|
||
|
// The main loop of the example reads the raw sensor values (uncalibrated).
|
||
|
// You can test this by taping a piece of 3/4" black electrical tape to a piece of white
|
||
|
// paper and sliding the sensor across it. It prints the sensor values to the serial
|
||
|
// monitor as numbers from 0 (maximum reflectance) to 1023 (minimum reflectance).
|
||
|
|
||
|
|
||
|
#define NUM_SENSORS 6 // number of sensors used
|
||
|
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
|
||
|
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2
|
||
|
|
||
|
// sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
|
||
|
QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5},
|
||
|
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
|
||
|
unsigned int sensorValues[NUM_SENSORS];
|
||
|
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
delay(500);
|
||
|
Serial.begin(9600); // set the data rate in bits per second for serial data transmission
|
||
|
delay(1000);
|
||
|
}
|
||
|
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
// read raw sensor values
|
||
|
qtra.read(sensorValues);
|
||
|
|
||
|
// print the sensor values as numbers from 0 to 1023, where 0 means maximum reflectance and
|
||
|
// 1023 means minimum reflectance
|
||
|
for (unsigned char i = 0; i < NUM_SENSORS; i++)
|
||
|
{
|
||
|
Serial.print(sensorValues[i]);
|
||
|
Serial.print('\t'); // tab to format the raw data into columns in the Serial monitor
|
||
|
}
|
||
|
Serial.println();
|
||
|
|
||
|
delay(250);
|
||
|
}
|