A repo for the code of the Line Following robot we are making for Robotex 2022
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

116 lignes
2.7KB

  1. #include <QTRSensors.h>
  2. QTRSensors qtr;
  3. const uint8_t SensorCount = 8;
  4. uint16_t sensorValues[SensorCount];
  5. int MODE = 8;
  6. int left_motor_in = 6;
  7. int left_motor_out = 9;
  8. int right_motor_in = 3;
  9. int right_motor_out = 5;
  10. void setup() {
  11. Serial.begin(9600);
  12. Serial.println("Start");
  13. init_motor();
  14. qtr.setTypeRC();
  15. qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
  16. qtr.setEmitterPin(7);
  17. delay(500);
  18. pinMode(LED_BUILTIN, OUTPUT);
  19. digitalWrite(LED_BUILTIN, HIGH);// turn on Arduino's LED to indicate we are in calibration mode
  20. Serial.println("Starting Calibration");
  21. // 2.5 ms RC read timeout (default) * 10 reads per calibrate() call
  22. // = ~25 ms per calibrate() call.
  23. // Call calibrate() 400 times to make calibration take about 10 seconds.
  24. for (uint16_t i = 0; i < 400; i++)
  25. {
  26. qtr.calibrate();
  27. }
  28. digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with calibration
  29. Serial.println("Calibration finished");
  30. // print the calibration minimum values measured when emitters were on
  31. Serial.begin(9600);
  32. for (uint8_t i = 0; i < SensorCount; i++)
  33. {
  34. Serial.print(qtr.calibrationOn.minimum[i]);
  35. Serial.print(' ');
  36. }
  37. Serial.println();
  38. // print the calibration maximum values measured when emitters were on
  39. for (uint8_t i = 0; i < SensorCount; i++)
  40. {
  41. Serial.print(qtr.calibrationOn.maximum[i]);
  42. Serial.print(' ');
  43. }
  44. Serial.println();
  45. Serial.println();
  46. delay(1000);
  47. }
  48. void loop() {
  49. uint16_t position = qtr.readLineBlack(sensorValues);
  50. for (uint8_t i = 0; i < SensorCount; i++)
  51. {
  52. Serial.print(sensorValues[i]);
  53. Serial.print('\t');
  54. }
  55. Serial.println(position);
  56. delay(250);
  57. if (position >= 3000 and position <= 4300) {
  58. left_motor_go(200);
  59. right_motor_go(200);
  60. Serial.print(position);Serial.println("center");
  61. }
  62. if (position < 3000) {
  63. left_motor_go(200);
  64. right_motor_go(0);
  65. Serial.print(position);Serial.println("left");
  66. }
  67. if (position > 4300) {
  68. left_motor_go(0);
  69. right_motor_go(200);
  70. Serial.print(position);Serial.println("right");
  71. }
  72. }
  73. void init_motor(){
  74. pinMode(left_motor_in, OUTPUT);
  75. pinMode(left_motor_out, OUTPUT);
  76. pinMode(right_motor_in, OUTPUT);
  77. pinMode(right_motor_out, OUTPUT);
  78. pinMode(MODE, OUTPUT);
  79. digitalWrite(MODE, HIGH);
  80. }
  81. //range:0<value<255
  82. void left_motor_go(int motor_speed){
  83. analogWrite(left_motor_in, motor_speed);
  84. digitalWrite(left_motor_out, LOW);
  85. }
  86. //range:0<value<255
  87. void right_motor_go(int motor_speed){
  88. analogWrite(right_motor_in, motor_speed);
  89. digitalWrite(right_motor_out, LOW);
  90. }