0

I just started using Arduino Uno, I can't get the ultrasonic to work ,with motor controller board, the output value is always zero.

Just to make sure I'm talking about the correct stuff: I used this guide to try to make it work (look at the picture how did I connect the parts).

And this is my code:

#define trigPin A4//Attach Trig to port 8
#define echoPin A5//Attach Echo to port 7
#include <AFMotor.h>
//SMARS Demo 2 with Line sensor

AF_DCMotor R_motor(2); // defines Right motor pin
AF_DCMotor L_motor(1); // defines Left motor pin

int lmotorpin1 = 3;
int lmotorpin2 = 5;
int rmotorpin1 = 6;
int rmotorpin2 = 9;
int distancecm = 0;
//const int trigPin = 18;
//const int echoPin = 19;

// defines variables
long duration;
int distance;
void setup() {


  Serial.begin(9600); // sets up Serial library at 9600 bps

  //changes the following values to make the robot drive as straight as possible
  L_motor.setSpeed(200); // sets L motor speed
  R_motor.setSpeed(140); // sets R motor speed

  R_motor.run(RELEASE); //turns L motor on
  L_motor.run(RELEASE); //turns R motor on

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
      Serial.println(trigPin);
    Serial.println(echoPin);
  distancecm = mdistance();
  if (distance < 5) {
    L_motor.run(BACKWARD); //moves motor L Forward
    R_motor.run(FORWARD); //moves motor L Forward
//    delay(1000);
//    L_motor.run(FORWARD); //moves motor L Forward
//    R_motor.run(FORWARD); //moves motor L Forward
//    delay(1000);
  }
  else {
    L_motor.run(FORWARD); //moves motor L Forward
    R_motor.run(FORWARD); //moves motor L Forward
  }
}

int mdistance()
{
  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);
  Serial.print("Duration: ");
  Serial.println(duration);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
}

As you can see I tried different pin numbers but it always 0 ....

what do you think can be wrong?

The motors work. Motor Controller: KEYES L293D Motor Control Shield Sensor: Ultrasonic Ranger V2.0

5
  • and the motors work? do you have the shield?
    – Juraj
    Nov 16, 2018 at 8:05
  • what sensor are you using?
    – MichaelT
    Nov 16, 2018 at 8:07
  • I added the info in the main post at the end
    – Alex K
    Nov 16, 2018 at 17:29
  • the way the description of your problem reads is the ranger does not work with the motor controller, but it works without the motor controller ..... is that what is happening?
    – jsotola
    Nov 16, 2018 at 18:39
  • I didn't check it without the motor controller.
    – Alex K
    Nov 16, 2018 at 22:04

1 Answer 1

-1

Pin numbers 0 and 1 are reserved for the Serial monitor which you're using on the board. The function int mdistance() should return an integer. You also didn't seem to set the motor pins as OUTPUT.

3
  • This seems to be more of a comment than actual solution to the OP's problem
    – MichaelT
    Nov 16, 2018 at 23:56
  • Where is pins 0 and 1? I don't see why this is a problem because the motors are working.. The problem is the ultrasonic sensor.. Please explain yourself.
    – Alex K
    Nov 17, 2018 at 6:06
  • the motor number is not a pin number
    – Juraj
    Nov 18, 2018 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.