Problem in coding

I used the QTR sensor for line follower and there aren’t any connection problems upto what i have seen and i tried to change the values to like boolean but it displayed when i serial printed seperately but when combined in the line follower code it didnt work.
sketch_jan14a.ino (835 Bytes)
This is the code

I don’t know why when I am opening the code, I can see the following:

const int trigPin = A2;
const int echoPin = A1;
// defines variables
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
}

This does not look like a QTR sensor code. Rather looks like an HCSR04 sonar sensor code.

Sorry wrong sketch then

QTRAExample.ino (5.2 KB)

This shows reading only once and don’t be doubted by its name its just i didnt rename it yet.

Hello.

What happens differently than you expect when you run your current code?

There’s a lot going on in your code, but one of the things I noticed is that you are checking if the raw reading is greater than 1000. The readings only go up to 1023 maximum; have you verified that your sensor’s raw readings ever actually read over 1000? You might try printing out more complete debug information. For example, you could include the sensor readings as well:

Serial.print(sensorValues[0]); Serial.print(": "); Serial.println(x);
Serial.print(sensorValues[1]); Serial.print(": "); Serial.println(x1);
Serial.print(sensorValues[2]); Serial.print(": "); Serial.println(x2);
Serial.print(sensorValues[3]); Serial.print(": "); Serial.println(x3);
Serial.print(sensorValues[4]); Serial.print(": "); Serial.println(x4);
Serial.print(sensorValues[5]); Serial.print(": "); Serial.println(x5);

Separately, it looks like the way you are trying to use the readings and set different states for your robot is fairly rudimentary and I am not sure how practical it will be. If you could post more details about what you are trying to do, I might have some alternative suggestions.

Brandon

I just want the serial print for line following robot but the problem is that its not even been a week since i started using arduino and the line follower code i used is by binary values and i don’t know how to use qtr properlly like how to implement it in the line follower code nor use it to sense

i got the sensor reading as 1 and 0 but is it really a good way i mean like how can we practically use it.

No, I don’t think this binary method is a good approach for line following. Instead, I recommend using the readLineBlack() function from our library (or readLineWhite() if you’re using a white line on a black surface), which when used with 6 sensors will return a value between 0 and 5000 corresponding to the position of the line under the sensor array. For example, 0 would mean the line is under the sensor on one end of the array and 5000 would mean it is under the sensor on the opposite side, with 2500 being directly between the middle two sensors. You can see how you can go about using this value to implement a basic line following algorithm in our LineFollowerSimple.ino example program for our 3pi+ Robot (or a more advanced PID algorithm in our LineFollowerPID exmaple).

However, please note that you should properly calibrate the sensors before using the readLineBlack() or readLineWhite() functions. To calibrate the sensors, you will need to call the calibrate() function repeatedly while making sure each sensor is exposed to both the lightest and darkest portion of your line following course.

For reference, in the same LineFollowerSimple.ino example for the 3pi+ Robot, we calibrate the sensors by putting it on the line and having it rotate back and forth (passing the sensors over the line) while calling the calibrate() function, which you can see starting at line 120.

You can also find a walkthrough of simple and advanced line following in the “Example Project #1: Line Following” section of the original 3pi Robot user’s guide. However, please note that the code is not written for Arduino and it uses a different number of sensors, but the concept is basically the same.

Brandon