# Ultrasonic sensors problem

Hello guys
the problem:
I have 4 HC-SR04 connected on Arduino Mega
as response 1 or 2 of them gives me randomly a “0” as distance which mess up my code to control dc motor.
I tried different sensors timing but nothing changes(right now is 200 ms)
this is the serial output:

0=59cm 1=0cm 2=0cm 3=32cm
FWD A
FWD B
FWD B
FWD A
0=59cm 1=48cm 2=12cm 3=31cm
FWD A
FWD B
FWD A
0=57cm 1=0cm 2=0cm 3=0cm
FWD A
FWD B
FWD B
FWD A
BKW A
0=58cm 1=47cm 2=0cm 3=22cm

this is the code:

``````// ---------------------------------------------------------------------------
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ---------------------------------------------------------------------------
#include <NewPing.h>

#define SONAR_NUM     4 // Number of sensors.
#define MAX_DISTANCE 300 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 200 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
NewPing(26, 27, MAX_DISTANCE), //  FRONT SENSOR  Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(28, 29, MAX_DISTANCE), // DX SENSOR
NewPing(24, 25, MAX_DISTANCE), //LX SENSOR
NewPing(22, 23, MAX_DISTANCE), // BACK MSENSOR
//  NewPing(23, 24, MAX_DISTANCE),
//  NewPing(25, 26, MAX_DISTANCE),
//  NewPing(27, 28, MAX_DISTANCE),
//  NewPing(29, 30, MAX_DISTANCE),
//  NewPing(31, 32, MAX_DISTANCE),
//  NewPing(34, 33, MAX_DISTANCE),
//  NewPing(35, 36, MAX_DISTANCE),
//  NewPing(37, 38, MAX_DISTANCE),
//  NewPing(39, 40, MAX_DISTANCE),
//  NewPing(50, 51, MAX_DISTANCE),
//  NewPing(52, 53, MAX_DISTANCE)
};

//Motor  LX;
int dir1PinA = 13;
int dir2PinA = 12;
int speedPinA = 10;

//motor RX;
int dir1PinB = 11;
int dir2PinB = 8;
int speedPinB = 9;

void setup() {
Serial.begin(9600);

pinMode (dir1PinA, OUTPUT);
pinMode (dir2PinA, OUTPUT);
pinMode (speedPinA, OUTPUT);
pinMode (dir1PinB, OUTPUT);
pinMode (dir2PinB, OUTPUT);
pinMode (speedPinB, OUTPUT);

pingTimer = millis() + 100;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i;                          // Sensor being accessed.
cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
// Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
// The following code would be replaced with your code that does something with the ping results.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm[i]);
Serial.print("cm ");
}
Serial.println();

// GO forward

if (cm >= 30){

//if (cm <= 30 && cm != 0) // CENTER SENSOR
analogWrite (speedPinA, 255);
analogWrite (speedPinB, 255);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, HIGH);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, HIGH);

//delay(50);

if (dir2PinA = HIGH)
Serial.println("FWD A");

if (dir2PinB = HIGH)
Serial.println("FWD B");
}

if (cm <= 30){

//-----------------------------------motors back
analogWrite (speedPinA, 150);
digitalWrite (dir1PinA, HIGH);
digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 150);
digitalWrite (dir1PinB, HIGH);
digitalWrite (dir2PinB, LOW);

if (dir1PinA = HIGH)
Serial.println("BKW A");

if (dir1PinB = HIGH)
Serial.println("BKW B");
}

//---------------------------------------------RIGHT SENSOR

if (cm <= 15) {

// MOTORS TURN RIGH M
analogWrite (speedPinA, 0);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 255);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, HIGH);

if (dir1PinA = LOW)
Serial.println("STOP A");

if (dir2PinB = HIGH)
Serial.println("FWD B");
}

if (cm <= 15 ) { // LEFT SENSOR

// MOTORS TURN LEFT
analogWrite (speedPinB, 255);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, HIGH);
analogWrite (speedPinB, 0);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, LOW);

if (dir2PinA = HIGH)
Serial.println("FWD A");

if (dir2PinB = LOW)
Serial.println("STOP B");
}

if (cm <= 20) { // BACK SENSOR

// MOTORS TURN RIGH M
analogWrite (speedPinB, 150);
analogWrite (speedPinA, 150);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, HIGH);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, HIGH);

if (dir2PinA = LOW)
Serial.println("STOP A");

if (dir2PinB = LOW)
Serial.println("STOP B");

}

if (cm <=10 && cm <=10) {

analogWrite (speedPinA, 0);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 255);
digitalWrite (dir1PinB, HIGH);
digitalWrite (dir2PinB, LOW);

if (dir2PinA = LOW)
Serial.println("STOP A");

if (dir1PinB = HIGH)
Serial.println("BWD B");

}
if (cm <=10 && cm <=10) {

analogWrite (speedPinA, 255);
digitalWrite (dir1PinA, HIGH);
digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 0);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, LOW);

if (dir1PinA = HIGH)
Serial.println("BWD A");

if (dir1PinB = HIGH)
Serial.println("STOP B");

}
//----------------------STUCK BACK RIGHT

if (cm <=10 && cm <=10) {

analogWrite (speedPinA, 255);
digitalWrite (dir1PinA, LOW);
digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 255);
digitalWrite (dir1PinB, HIGH);
digitalWrite (dir2PinB, LOW);

}
//------------------------STUCK BACK LEFT
if (cm <=10 && cm <=10) {

analogWrite (speedPinA, 255);
digitalWrite (dir1PinA, HIGH);

digitalWrite (dir2PinA, LOW);
analogWrite (speedPinB, 0);
digitalWrite (dir1PinB, LOW);
digitalWrite (dir2PinB, LOW);

if (dir1PinA = HIGH)
Serial.println("BKW A");

if (dir1PinB = LOW)
Serial.println("STOP B");

}
}
``````

Hardware:
Arduino mega, L298N Motor Driver shield

Thanks

Ok I am mapping the distance so to avoid the "0"
anyone can help wit map command?
thanks

You could be getting the numbers due to insufficient power. Put a 10uF cap between 5V and gnd on the sonic ranger. Mine was giving off some occasional random numbers before I added the cap.

Thanks