Help with HC-SR04 and Line Follower code

Hi Guys
Im new to coding and was hoping to get some help with a project i am working on.
my idea was to have a robot which could follow a line, and if the robot came within range of a wall/obstacle it would just stop wait and then continue once it was clear…
i have got the robot to follow a line, but trying to add the Ping sensor (HC-SR04) to the line follower code makes the robot do circles once it is uploaded… ill put my code below, Im sorry in advance if it is messy or could have been done a better way.

Thank-you for your time.

include < ZumoReflectanceSensorArray.h>
include < ZumoBuzzer.h>
include < Pushbutton.h>
include < QTRSensors.h>
include < ZumoMotors.h>
define trigPin 4
define echoPin 5

Pushbutton button(ZUMO_BUTTON);
ZumoMotors motors;
ZumoBuzzer buzzer;
ZumoReflectanceSensorArray reflectanceSensors;
int lastError = 0;
const int MAX_SPEED = 400;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

 // Play a little welcome song">g32>>c32");

  // Initialize the reflectance sensors module

  // Wait for the user button to be pressed and released

  // Turn on LED to indicate we are in calibration mode
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // Wait 1 second and then begin automatic sensor calibration
  // by rotating in place to sweep the sensors over the line
  int i;
  for(i = 0; i < 80; i++)
    if ((i > 10 && i <= 30) || (i > 50 && i <= 70))
      motors.setSpeeds(-200, 200);
      motors.setSpeeds(200, -200);

    // Since our counter runs to 80, the total delay will be
    // 80*20 = 1600 ms.

  // Turn off LED to indicate we are through with calibration
  digitalWrite(13, LOW);">g32>>c32");

  // Wait for the user button to be pressed and released

  // Play music and wait for it to finish before we start driving."L16 cdegreg4");

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);        // start of the pulse 
  digitalWrite(trigPin, HIGH); 
  digitalWrite(trigPin, LOW);        // end of the pulse
  duration = pulseIn(echoPin, HIGH); // finding returning pulse *echo*
  distance = (duration/2) / 29.1;    // math to find distance 
  if (distance < 30) {               // distance you want it to stop at.. *not accurate*
     motors.setSpeeds (0,0);         // motors stop 
  unsigned int sensors[6];

  // Get the position of the line.  Note that we *must* provide the "sensors"
  // argument to readLine() here, even though we are not interested in the
  // individual sensor readings
  int position = reflectanceSensors.readLine(sensors);

  // Our "error" is how far we are away from the center of the line, which
  // corresponds to position 2500.
  int error = position - 2500;

  // Get motor speed difference using proportional and derivative PID terms
  // (the integral term is generally not very useful for line following).
  // Here we are using a proportional constant of 1/4 and a derivative
  // constant of 6, which should work decently for many Zumo motor choices.
  // You probably want to use trial and error to tune these constants for
  // your particular Zumo and line course.
  int speedDifference = error / 4 + 6 * (error - lastError);

  lastError = error;

  // Get individual motor speeds.  The sign of speedDifference
  // determines if the robot turns left or right.
  int m1Speed = MAX_SPEED + speedDifference;
  int m2Speed = MAX_SPEED - speedDifference;

  // Here we constrain our motor speeds to be between 0 and MAX_SPEED.
  // Generally speaking, one motor will always be turning at MAX_SPEED
  // and the other will be at MAX_SPEED-|speedDifference| if that is positive,
  // else it will be stationary.  For some applications, you might want to
  // allow the motor speed to go negative so that it can spin in reverse.
  if (m1Speed < 0)
    m1Speed = 0;
  if (m2Speed < 0)
    m2Speed = 0;
  if (m1Speed > MAX_SPEED)
    m1Speed = MAX_SPEED;
  if (m2Speed > MAX_SPEED)
    m2Speed = MAX_SPEED;

  motors.setSpeeds(m1Speed, m2Speed);


It is not clear to me if you have a Zumo Robot for Arduino and if you are using the HC-SR04 ultrasonic sensor or the Parallax PING))) Ultrasonic Sensor, can you please clarify?

From your code, it looks like you are missing the # character for each of your preprocessor directives (e.g. #include, #define), and you should not have the extra space between the open angle bracket < and the included header file in each of your includes. The includes should look like #include <ZumoReflectanceSensorArray.h>. Also, it looks like you might have forgotten to put else for the code enclosed in the second pair of curly braces in loop() like so:

if (distance < 30) {               // distance you want it to stop at.. *not accurate*
   motors.setSpeeds (0,0);         // motors stop 
else {                             // missing else
   unsigned int sensors[6];
   // rest of your code inside the curly braces

Without that else, the code in that scope (or enclosed in the curly braces) will run regardless if the distance is less than 30.

I do not expect correcting those errors to fix the issue of your robot going in circles, but it would be better if you could try simplifying your code to the point where it should work but does not for a specific task (e.g. movement based on detection). Since the issue happened after adding the sensor to the robot, you might try simplifying the code to just drive the robot forward until the sensor detect something then stop and see if that works as expected.

- Amanda