A repo for the code of the Line Following robot we are making for Robotex 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4KB

  1. //Made by POLULU
  2. #include <QTRSensors.h>
  3. // This example is designed for use with eight RC QTR sensors. These
  4. // reflectance sensors should be connected to digital pins 3 to 10. The
  5. // sensors' emitter control pin (CTRL or LEDON) can optionally be connected to
  6. // digital pin 2, or you can leave it disconnected and remove the call to
  7. // setEmitterPin().
  8. //
  9. // The setup phase of this example calibrates the sensors for ten seconds and
  10. // turns on the Arduino's LED (usually on pin 13) while calibration is going
  11. // on. During this phase, you should expose each reflectance sensor to the
  12. // lightest and darkest readings they will encounter. For example, if you are
  13. // making a line follower, you should slide the sensors across the line during
  14. // the calibration phase so that each sensor can get a reading of how dark the
  15. // line is and how light the ground is. Improper calibration will result in
  16. // poor readings.
  17. //
  18. // The main loop of the example reads the calibrated sensor values and uses
  19. // them to estimate the position of a line. You can test this by taping a piece
  20. // of 3/4" black electrical tape to a piece of white paper and sliding the
  21. // sensor across it. It prints the sensor values to the serial monitor as
  22. // numbers from 0 (maximum reflectance) to 1000 (minimum reflectance) followed
  23. // by the estimated location of the line as a number from 0 to 5000. 1000 means
  24. // the line is directly under sensor 1, 2000 means directly under sensor 2,
  25. // etc. 0 means the line is directly under sensor 0 or was last seen by sensor
  26. // 0 before being lost. 5000 means the line is directly under sensor 5 or was
  27. // last seen by sensor 5 before being lost.
  28. QTRSensors qtr;
  29. const uint8_t SensorCount = 8;
  30. uint16_t sensorValues[SensorCount];
  31. void setup()
  32. {
  33. // configure the sensors
  34. qtr.setTypeRC();
  35. qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
  36. qtr.setEmitterPin(7);
  37. delay(500);
  38. pinMode(LED_BUILTIN, OUTPUT);
  39. digitalWrite(LED_BUILTIN, HIGH); // turn on Arduino's LED to indicate we are in calibration mode
  40. // 2.5 ms RC read timeout (default) * 10 reads per calibrate() call
  41. // = ~25 ms per calibrate() call.
  42. // Call calibrate() 400 times to make calibration take about 10 seconds.
  43. for (uint16_t i = 0; i < 400; i++)
  44. {
  45. qtr.calibrate();
  46. }
  47. digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with calibration
  48. // print the calibration minimum values measured when emitters were on
  49. Serial.begin(9600);
  50. for (uint8_t i = 0; i < SensorCount; i++)
  51. {
  52. Serial.print(qtr.calibrationOn.minimum[i]);
  53. Serial.print(' ');
  54. }
  55. Serial.println();
  56. // print the calibration maximum values measured when emitters were on
  57. for (uint8_t i = 0; i < SensorCount; i++)
  58. {
  59. Serial.print(qtr.calibrationOn.maximum[i]);
  60. Serial.print(' ');
  61. }
  62. Serial.println();
  63. Serial.println();
  64. delay(1000);
  65. }
  66. void loop()
  67. {
  68. // read calibrated sensor values and obtain a measure of the line position
  69. // from 0 to 5000 (for a white line, use readLineWhite() instead)
  70. uint16_t position = qtr.readLineBlack(sensorValues);
  71. // print the sensor values as numbers from 0 to 1000, where 0 means maximum
  72. // reflectance and 1000 means minimum reflectance, followed by the line
  73. // position
  74. for (uint8_t i = 0; i < SensorCount; i++)
  75. {
  76. Serial.print(sensorValues[i]);
  77. Serial.print('\t');
  78. }
  79. Serial.println(position);
  80. delay(250);
  81. }