PulseIn() Problem (Arduino 1.8.5 for mac)

Hello all,

I am building a small Arduino-controlled robotic car that can track a line of black electrical tape on the ground, and avoid obstacles. However, those two capabilities only work separately and I am trying to get them to work together. I am having problems doing so, and I think that the problem is the pulseIn() command. All the other parts to initialize the Servo are fine. When I add the pulseIn line, something goes wrong. Can someone help me find out what the problem may be?

Line_tracking_car_enhanced.ino (2.48 KB)

When I add the pulseIn line, something goes wrong.

And you didn't think it would be helpful to describe the symptoms?

Here is OP’s code:

#include <Servo.h>  //servo library
Servo myservo;      // create servo object to control servo
#include "SR04.h"
int Echo = A4;
int Trig = A5;

//Line Tracking IO define
#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11

#define carSpeed 150
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
SR04 sr04 = SR04(Echo, Trig);
long a;

void forward() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go forward!");
}

void back() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go back!");
}

void left() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("go left!");
}

void right() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("go right!");
}

void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
}

//Ultrasonic distance measurement Sub function
int Distance_test() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);
  float Fdistance = pulseIn(Echo, HIGH);
  Fdistance = Fdistance / 58;
  return (int)Fdistance;
}

void setup() {
  Serial.begin(9600);
  pinMode(LT_R, INPUT);
  pinMode(LT_M, INPUT);
  pinMode(LT_L, INPUT);

  myservo.attach(3);  // attach servo on pin 3 to servo object
  Serial.begin(9600);
  pinMode(Echo, INPUT);
  pinMode(Trig, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  stop();
}

void loop() {

    if (LT_M) {
      forward();
    }
    else if (LT_R) {
      right();
      while (LT_R);
    }
    else if (LT_L) {
      left();
      while (LT_L);
    }
}


/*
   1. If no line, then stop.
   2. If no line, then turn around 180 degrees.
   3. Know when the path has ended.
   4. If obstacle == true then avoid (stays off line)
   5. Return back to the line -- what would nature do?



*/

the call to pulseIn is in the Distance_test() function and that function is never called so I don’t see how that will affect anything.

pinMode(LT_R, INPUT);

Let's expand that macro:

pinMode(!digitalRead(10), INPUT);

Does that look right?