Calibrate QTR 8A while driving back and forth

Hi,

I am working on a game with the QTR 8A but in order to calibrate the sensor correctly, the robot needs to move about 7 to 9 times back and forth. I made it in an while loop but the while loop doesn’t run.

here is my current code

#include <QTRSensors.h>
//wheels
int reverseRight = 9;
int forwardRight = 10;
int reverseLeft = 6;
int forwardLeft = 11;
//pulsesensor
int speedSensorA = 2; 
int speedSensorB = 3; 
int counterA = 0;
int counterB = 0;
//line tracker
QTRSensors qtr;
const int SensorCount = 6;
int sensorValues[SensorCount];

void setup(){
  Serial.begin(9600);
  pinMode(forwardLeft, OUTPUT); 
  pinMode(forwardRight, OUTPUT);
  pinMode(reverseLeft, OUTPUT);
  pinMode(reverseRight, OUTPUT);
  pinMode(speedSensorA, INPUT_PULLUP);
  pinMode(speedSensorB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(speedSensorA), countA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(speedSensorB), countB, CHANGE);
  qtr.setTypeAnalog();
 qtr.setSensorPins((const byte[]){A0, A1, A2, A3, A4, A5}, SensorCount);
 
while(calibratie(360)){ //this while loop doesn't run
  moveForward(25);
 delayMicroseconds(80);
 moveBackward(25);
delayMicroseconds(80);
 moveForward(25);
 }

//this runs normally 
//  moveForward(25);
// delayMicroseconds(80);
// moveBackward(25);
//delayMicroseconds(80);
// moveForward(25);
}

void loop(){
   
}

bool calibratie(int timesLoop){
  for (int i = 0; i < timesLoop; i++)
  {
    qtr.calibrate();
  }
  digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with calibration

  // print the calibration minimum values measured when emitters were on
  for (int i = 0; i < SensorCount; i++)
  {
    Serial.print(qtr.calibrationOn.minimum[i]);
    Serial.print(' ');
  }
  Serial.println();

  // print the calibration maximum values measured when emitters were on
  for (int i = 0; i < SensorCount; i++)
  {
    Serial.print(qtr.calibrationOn.maximum[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.println();

}

void stopVehicle(){
  analogWrite(forwardLeft, LOW);
  analogWrite(forwardRight, LOW);
  analogWrite(reverseLeft, LOW);
  analogWrite(reverseRight, LOW);
  counterA = 0;
  counterB = 0;
}

void moveForward(int pulsecount) {
  while(counterA <= pulsecount){
  analogWrite(forwardLeft, 200);
  analogWrite(forwardRight, 210);
  analogWrite(reverseLeft, LOW);
  analogWrite(reverseRight, LOW);
  }
  stopVehicle();
  counterA = 0;
  counterB = 0;
}

void moveBackward(int pulsecount) {
  while(counterA <= pulsecount){
    analogWrite(forwardLeft, LOW);
    analogWrite(forwardRight, LOW);
    analogWrite(reverseLeft, 200);
    analogWrite(reverseRight, 210);
  } 
  stopVehicle();
  counterA = 0;
  counterB = 0;
}

void countA(){
  counterA++;
}

void countB(){
  counterB++;
}

Hello.

From how you have that while loop structured, I suspect you are expecting it to work differently than it does. However, instead of using a while loop like that, I suggest doing a simple for loop; you can use the calibration portion of our 3pi+ 32U4 Robot’s LineFollowerSimple example program as reference. That portion of code makes the robot turn back and forth while taking calibration readings.

Brandon

That makes sense,
Thank you for your response!

1 Like