Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
/* * demo code for the Pololu reflectance sensor by Vince Callahan * This code will display the value of sensor output on the serial port Arduino pin 2 to reflectance sensor output pin (any one of the 8) Arduino +5V out to Sensor Vcc Arduino GND to Sensor GND pin 2,3,5 work, 0,1,4 do NOT - more to be discovered with experimentation */int sensor1 = 2; // change "2" to whatever pin to which you are hooking the sensor. Just this one change allows you to test operation on other pins.int reflectance;// This function displays the sensor readings updated a few times per second.void setup(){ // initialize the serial communications Serial.begin(9600);}void loop(){ reflectance = 1; //initialize value to 1 at the beginning of each loop pinMode(sensor1, OUTPUT); //set pin as output digitalWrite(sensor1, HIGH); //set pin HIGH (5V) delayMicroseconds(15); //charge capacitor for 15 microseconds pinMode(sensor1, INPUT); //set pin as input while((reflectance < 500) && (digitalRead(sensor1) != LOW)){ //timeout at 500 // read the pin state, increment counter until state = LOW ++ reflectance; // increment value to be displayed via serial port delayMicroseconds(4); //Change value or comment out to adjust value range } if (reflectance < 500){ Serial.println(reflectance);} //Send reflectance value to serial display else { Serial.println("T.O."); //if reflectance value is over 500 then it's a "timeout" } delay(100); }
#include <PololuQTRSensors.h> // create an object for your type of sensor (RC or Analog) // in this example we have six sensors on analog inputs 0 - 5, a.k.a. Arduino digital pins 14 - 19PololuQTRSensorsRC qtr((unsigned char[]) {14, 15, 16, 17, 18, 19}, 6); // edit to adjust for number of sensors and desired Arduino pins for connections//i.e. {14, 15, 16, 17, 18, }, 5); - for only 5 pins - {2, 3, 14, 15, 16, 17, 18, 19}, 8); - for all 8 pins, etc. void setup() { // optional: wait for some input from the user, such as a button press // then start calibration phase and move the sensors over both // reflectance extremes they will encounter in your application: int i; for (i = 0; i < 250; i++) // make the calibration take about 5 seconds { qtr.calibrate(); delay(20); } // optional: signal that the calibration phase is now over and wait for further // input from the user, such as a button press Serial.begin(9600); } void loop() { unsigned int sensors[6]; // change 6 to whatever number of sensors you are using // get calibrated sensor values returned in the sensors array, along with the line position // position will range from 0 to 2000, with 1000 corresponding to the line over the middle sensor int position = qtr.readLine(sensors); // output to Serial monitorSerial.print(sensors[0]); Serial.print(" ");Serial.print(sensors[1]); Serial.print(" ");Serial.print(sensors[2]); Serial.print(" ");Serial.print(sensors[3]); Serial.print(" ");Serial.print(sensors[4]); Serial.print(" ");Serial.print(sensors[5]); // "comment out" this line and the one below to subtract a sensor. Do the same for more pairs above to subtract more sensors.Serial.print(" "); // read comment in line above this one//Serial.print(sensors[6]); remove comment marks to add sensor 7 (and don't forget to "comment out" THIS comment - same for any of the three lines below this)//Serial.print(" "); remove comment marks to add sensor 7//Serial.print(sensors[7]); remove comment marks to add sensor 8//Serial.print(" "); remove comment marks to add sensor 8Serial.println(); // if all six sensors see very low reflectance, take some appropriate action for this situation //add or subtract " && sensors[?] > 750" from line below depending on how many sensors you are using if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750 && sensors[3] > 750 && sensors[4] > 750 && sensors[5] > 750) { // do something. Maybe this means we're at the edge of a course or about to fall off a table, // in which case, we might want to stop moving, back up, and turn around. return; } // compute our "error" from the line position. We will make it so that the error is zero when // the middle sensor is over the line, because this is our goal. Error will range from // -1000 to +1000. If we have sensor 0 on the left and sensor 2 on the right, a reading of -1000 // means that we see the line on the left and a reading of +1000 means we see the line on // the right. int error = position - 1000; int leftMotorSpeed = 100; int rightMotorSpeed = 100; if (error < -500) // the line is on the left leftMotorSpeed = 0; // turn left if (error > 500) // the line is on the right rightMotorSpeed = 0; // turn right // set motor speeds using the two motor speed variables above }
#include <PololuQTRSensors.h> int ledPin = 13;// create an object for your type of sensor (RC or Analog) // in this example we have six sensors on analog inputs 0 - 5, a.k.a. Arduino digital pins 14 - 19PololuQTRSensorsRC qtr((unsigned char[]) {3,4,5}, 3); // edit to adjust for number of sensors and desired Arduino pins for connections//i.e. {14, 15, 16, 17, 18, }, 5); - for only 5 pins - {2, 3, 14, 15, 16, 17, 18, 19}, ; - for all 8 pins, etc. void setup() { pinMode(ledPin, OUTPUT); // optional: wait for some input from the user, such as a button press // then start calibration phase and move the sensors over both // reflectance extremes they will encounter in your application: int i; for (i = 0; i < 250; i++) // make the calibration take about 5 seconds { qtr.calibrate(); delay(20); } // optional: signal that the calibration phase is now over and wait for further // input from the user, such as a button press Serial.begin(9600); } void loop() { unsigned int sensors[3]; // change 6 to whatever number of sensors you are using // get calibrated sensor values returned in the sensors array, along with the line position // position will range from 0 to 2000, with 1000 corresponding to the line over the middle sensor int position = qtr.readLine(sensors); // output to Serial monitorSerial.print(sensors[0]); Serial.print(" ");Serial.print(sensors[1]); Serial.print(" ");Serial.print(sensors[2]); Serial.print(" ");//Serial.print(sensors[3]); //Serial.print(" ");//Serial.print(sensors[4]); //Serial.print(" ");//Serial.print(sensors[5]); // "comment out" this line and the one below to subtract a sensor. Do the same for more pairs above to subtract more sensors.//Serial.print(" "); // read comment in line above this one//Serial.print(sensors[6]); remove comment marks to add sensor 7 (and don't forget to "comment out" THIS comment - same for any of the three lines below this)//Serial.print(" "); remove comment marks to add sensor 7//Serial.print(sensors[7]); remove comment marks to add sensor 8//Serial.print(" "); remove comment marks to add sensor 8Serial.println(); // if all six sensors see very low reflectance, take some appropriate action for this situation //add or subtract " && sensors[?] > 750" from line below depending on how many sensors you are using if (sensors[0] > 750 && sensors[1] > 750 && sensors[2] > 750) { digitalWrite(ledPin, HIGH); // turn on an LED for a second to indicate reflectance drop delay(1000); digitalWrite(ledPin, LOW); // do something. Maybe this means we're at the edge of a course or about to fall off a table, // in which case, we might want to stop moving, back up, and turn around. return; } // compute our "error" from the line position. We will make it so that the error is zero when // the middle sensor is over the line, because this is our goal. Error will range from // -1000 to +1000. If we have sensor 0 on the left and sensor 2 on the right, a reading of -1000 // means that we see the line on the left and a reading of +1000 means we see the line on // the right. int error = position - 1000; int leftMotorSpeed = 100; int rightMotorSpeed = 100; if (error < -500) // the line is on the left leftMotorSpeed = 0; // turn left if (error > 500) // the line is on the right rightMotorSpeed = 0; // turn right // set motor speeds using the two motor speed variables above }
PololuQTRSensorsRC qtr((unsigned char[]) {3,4,5}, 3); // edit to adjust for number of sensors and desired Arduino
PololuQTRSensorsRC qtr((unsigned char[]) {14, 15, 16, 17, 18, 19}, 6); // edit to adjust for number of sensors and desired Arduino pins for connections
// in this example we have six sensors on analog inputs 0 - 5, a.k.a. Arduino digital pins 14 - 19
#include <PololuQTRSensors.h> // create an object for your type of sensor (RC or Analog) // in this example we have three sensors on analog inputs 3 - 5, a.k.a. Arduino digital pins 17 - 19PololuQTRSensorsRC qtr((unsigned char[]) {17, 18, 19}, 3); void setup() { // start calibration phase and move the sensors over both // reflectance extremes they will encounter in your application: int i; for (i = 0; i < 250; i++) // make the calibration take about 5 seconds { qtr.calibrate(); delay(20); } Serial.begin(9600); Serial.println(); Serial.println("CALIBRATION COMPLETE"); Serial.println(); delay(1000);} void loop() { unsigned int sensors[3]; // for three sensors // get calibrated sensor values returned in the sensors array, along with the line position // position will range from 0 to 2000, with 1000 corresponding to the line over the middle sensor int position = qtr.readLine(sensors); // output to Serial monitorSerial.print(sensors[0]); Serial.print(" ");Serial.print(sensors[1]); Serial.print(" ");Serial.print(sensors[2]); Serial.print(" ");Serial.println(); // if all sthree sensors see very low reflectance, print the string "no reflectance seen" if (sensors[0] > 750 && sensors[1] > 750 && sensors[2]) { Serial.println ("NO REFLECTANCE SEEN"); return; } }