Why the ultrasonic sensors can't get the right reading at the beginning?

Hi,
This sketch got the first 1 or 2 readings 0 or low value, why?
24 is the right number.
I tested both newPing and send pulse all same.

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(2, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 6, MAX_DISTANCE),
  NewPing(7, 8, MAX_DISTANCE)
};

int ult_sensorT[] = {2, 5, 7 };
int ult_sensorE[] = {4, 6, 8 };

uint8_t U[3] = {0};

const int trigPin = 5;
const int echoPin = 6;

int motorA1 = 10;       //REVerse motion of Left motor
int motorA2 = 11;       //ForWarD motion of Left motor
int motorB1 = 12;      //REVerse motion of Right motor
int motorB2 = 13;      //ForWarD motion of Right motor

int EnMotorA = 3;      //REVerse motion of Right motor
int EnMotorB = 9;       //ForWarD motion of Right motor

uint8_t Speed = 0;

long duration, distance;

void setup() {

  delay(random(500, 2000));  // delay for random time
  Serial.begin(9600);

  Serial.println("setup");

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(motorA1, OUTPUT);      // set Motor pins as output
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);

  pinMode(EnMotorA, OUTPUT);
  pinMode(EnMotorB, OUTPUT);

  for (int i = 0; i < SONAR_NUM; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    pinMode(ult_sensorT[i], OUTPUT); ////
    digitalWrite(ult_sensorT[i], LOW);
  }

  for (int i = 0; i < SONAR_NUM; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    pinMode(ult_sensorE[i], INPUT_PULLUP); ////too slow, changed : INPUT -- INPUT_PULLUP,
    digitalWrite(ult_sensorE[i], LOW);
  }
}

void loop() {
  Serial.println("loop");
  delay(3000);

  ///// NewPing3Sensors_loop();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
  delay(10);
  // If you dont get proper movements of your robot then alter the pin numbers

  /////  distance = distance_center;
  Serial.print("distance1=");
  Serial.println(distance);
  if (distance > 19)
  {

    Speed = map(distance, 19, 200, 100, 255);
    analogWrite(EnMotorA, Speed);
    analogWrite(EnMotorB, Speed);

    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, HIGH);
    digitalWrite(motorB1, LOW);
    digitalWrite(motorB2, HIGH);                    // move forward
  }

  if (distance < 18)
  {

    Serial.print("distance2=");
    Serial.println(distance);

    Serial.print("U1=");
    Serial.println(U[1]);

    digitalWrite(motorB2, LOW);  //Stop
    digitalWrite(motorB1, LOW);
    digitalWrite(motorA2, LOW);
    digitalWrite(motorA1, LOW);
    delay(500);

    Speed = map(distance, 0, 18, 255, 100);
    analogWrite(EnMotorA, Speed);
    analogWrite(EnMotorB, Speed);

    Serial.print("speed2=");
    Serial.println(Speed);

    digitalWrite(motorA1, HIGH);
    digitalWrite(motorA2, LOW);
    digitalWrite(motorB1, HIGH);
    digitalWrite(motorB2, LOW);
    delay(500);
    digitalWrite(motorB2, LOW);  //Stop
    digitalWrite(motorB1, LOW);
    digitalWrite(motorA2, LOW);
    digitalWrite(motorA1, LOW);
    delay(100);

    Speed = map(distance, 0, 18, 255, 100);
    analogWrite(EnMotorA, Speed);
    analogWrite(EnMotorB, Speed);

      digitalWrite(motorB2, HIGH);
    digitalWrite(motorB1, LOW);
    digitalWrite(motorA1, LOW);
    digitalWrite(motorA2, LOW);
    delay(500);
  }
}

readings:
sm.3
comment out all delays same:

Simplify, simplify, simplify.

Go back to the basic Ping example and make sure that EACH of your sensors is working correctly, alone, with no motors or anything else connected to the Arduino.

Then add the pieces back, one at a time, testing as you add each one.

1 Like

That code is questionable. In setup() you initialize trigPin=LOW, so that in the first loop() iteration no pulse is generated. Serial Monitor confirms that behaviour.

The delay after the waves can suppress echoes from nearby obstacles. In no case will it send waves. All µs timing is questionable if interrupts from e.g. Serial can inject pauses of several µs.

1 Like

Why bother with the input pullup, and then immediately turn it off?

Other than that, what DrDiettrich said.

1 Like

You are using NewPing, there is no need to fiddle with the pins.

1 Like

Hi,
Why aren't you using NewPing like this?

#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(115200);
}

void loop() {
delay(50);
Serial.print("Ping: ");
Serial.print(sonar.ping_cm());
Serial.println("cm");
 }

Simple...
This link will show you how to use multiple ultrasonic sensors.
https://playground.arduino.cc/Code/NewPing/

The examples in your IDE have code for multiple sensors..

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Thanks for all helps.

  1. I used newPing & Pulse respectively, comment out ONE when use other got same result;
  2. the send pulse code ref. from: Getting Started with the HC-SR04 Ultrasonic sensor - Arduino Project Hub
const int trigPin = 9;
const int echoPin = 10;

float duration, distance;

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

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}
  1. I'll re code to do from basic and see.
    Best to you.

Looks like you stated with the File->Examples->NewPing->NewPing2Sensors example and then didn't use the NewPing part.

Start with that example to see that your three sensors work properly. Then modify it to control your motors.

1 Like

Thanks.
The basic NewPing works fine.
As long as added some lines, measured 0 time by time.

// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(2, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 6, MAX_DISTANCE),
  NewPing(7, 8, MAX_DISTANCE)
};

uint8_t U[3] = {0};
long duration;
int distance;
uint8_t Speed = 0;

int motorA1 = 10;       //REVerse motion of Left motor
int motorA2 = 11;       //ForWarD motion of Left motor
int motorB1 = 12;      //REVerse motion of Right motor
int motorB2 = 13;      //ForWarD motion of Right motor

int EnMotorA = 3;      //REVerse motion of Right motor
int EnMotorB = 9;       //ForWarD motion of Right motor



void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);

   pinMode(motorA1, OUTPUT);      // set Motor pins as output
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);

  pinMode(EnMotorA, OUTPUT);
  pinMode(EnMotorB, OUTPUT);

}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");

    U[i] = sonar[i].ping_cm();
    distance = U[1];
    Serial.print("distance1=");
    Serial.println(sonar[1].ping_cm());

     if (distance > 19)
    {
      Speed = map(distance, 19, 200, 100, 255);
      analogWrite(EnMotorA, Speed);
      analogWrite(EnMotorB, Speed);

      digitalWrite(motorA1, LOW);
      digitalWrite(motorA2, HIGH);
      digitalWrite(motorB1, LOW);
      digitalWrite(motorB2, HIGH);                    // move forward
    }

    if (distance < 18)
    {

      Serial.print("distance2=");
      Serial.println(distance);

      Serial.print("U1=");
      Serial.println(U[1]);

      digitalWrite(motorB2, LOW);  //Stop
      digitalWrite(motorB1, LOW);
      digitalWrite(motorA2, LOW);
      digitalWrite(motorA1, LOW);
      ////// delay(500);
    }
 }
  Serial.println();
}

result report: sorry, BAD sensor!

Actually, what I really need is a multi-sensor with sonar.ping_median(iterations [, max_cm_distance]) method. NewPing had a 15 sensor example shown the way I need, but seems that code made many people confused? at lease I didn't get an suitable answer at one asking before: Need help on explanation please
This post solved the first reading error problem, but if I can get answer with this question also, will save me lot. Thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.