HC SR-04 with PID

Hello,

Im creating robot which could follow walls, by doing that it would travel around the room. I made it with PID, everything looks good, except I found one weird problem, it is my ultrasound sensors. As my robot goes, time by time, especially on the corners, Im getting random 0 values from my two sensors, and it changes my motor speeds. Im using HC-SR04 two sensors, one in front, other one on the right side of the robot, to follow up the wall. I`m trying to use NewPing library to make everything easier.

I made 100ms delay (60 ms minimum if you want your echo dont interrupt with trigger). Using Arduino mega and analog pins for trigger / echo. By multiplying with 10, Im getting it to millimeters.
Any ideas why this is happening? Btw everything is powered of adafruit motorshield v1.2 analog, vcc and gnd pins.

Here is my sensors code (short version):

//sensor
#define TRIGGER1  A0
#define ECHO1    A1
#define TRIGGER2  A2
#define ECHO2    A3
#define MAX_DISTANCE 5000
double Input;
double Front;
NewPing sonar1(TRIGGER1, ECHO1, MAX_DISTANCE);
NewPing sonar2(TRIGGER2, ECHO2, MAX_DISTANCE);
void sonar()
{
    Front = sonar1.ping_cm()*10;
    Serial.print("Front: ");
    Serial.print(Front);
    Serial.println("cm");
    delayMicroseconds(100);

    Input = sonar2.ping_cm()*10;
    Serial.print("Input: ");
    Serial.print(Input);
    Serial.println("cm");
    delayMicroseconds(100);

}

I would say that 0.1 ms between sonar readings is too short, but there's too little to go on to comment properly.

I don`t think that is the problem. I made it even with delay(200) 0.2s, and still getting random 0.

Serial monitor example:

Front: 2150.00cm
Input: 610.00cm
Lmotor: 240
Rmotor: 80
Output: -80.00
Front: 0.00cm
Input: 620.00cm
Lmotor: 0
Rmotor: 255
Output: -80.00
Front: 950.00cm
Input: 550.00cm

Any other ideas what could be wrong?

hollowltu:
Any other ideas what could be wrong?

I have alternate code that I use that is non blocking and triggered all ping sensors (I have 8 connected for 360° radar to an uno) at the same time. It uses interrupts and doesn't suffer the zero issue you describe. But I'm sure it will require adapting to use with a mega. I'll post it if your up to the challenge.
Z

Well Ill do anything to make this work correct. Weirdly, I cant find similar problems so Im not sure how to solve them. I tried using these sensors with NewPing and without it, and come in to same problem. Ill do anything to make this work, and if it wont, I might need to try other type of sensors.

Well I`ll do anything to make this work correct

...apart from, it would seem, provide anyone else with the means to help you.

hollowltu:
Well Ill do anything to make this work correct. Weirdly, I cant find similar problems so Im not sure how to solve them. I tried using these sensors with NewPing and without it, and come in to same problem. Ill do anything to make this work, and if it wont, I might need to try other type of sensors.

Attached is the 8 ultrasound sensor example code but I only enabled 1 sensor attached to pin 3

Without your source code I am not able to help further though.

by un-remarking // pciSetup ( 4 ) ; for the pins you will be attaching to the echos pin on your UNO you can add as many ping sensors as you would like.

This code uses interrupts directly and is not configured for MEGA boards so you will need to adapt this to work on a mega.

How it works:
all trigger pins are connected to pin 2 so that the echo triggers at the same time for all Ping sensors. (Note: you can trigger each sensor separately if you would like the distance measurement is independent of the trigger pulse. ) I chose to trigger all at the same time because they are facing in different directions.

The Ping sensor Echo pin rises triggering an interrupt at which time I record this as the start time when the sound leaves the sensor. When the echo pin falls I capture the end time and subtract the two to get the time in microseconds of the round trip of the sound pulse. Every pin can handle this at the same time. To prevent weird errors I also watch for all echo pins to fall LOW (Ping sensor will time out if no pulse is received.) before allowing the next trigger pulse to occur.
Read the specs for the default no echo duration of the sensor. This will be substantially higher time than the max echo time of the sensor. you will need to handle this appropriately as an error reading.

In the code uncomment the pins you wish to attach to the ECHO pin on the ping sensor. every pin is an eligible including analog pins. I could see every pin being used but what good would that do :).
Currently only pin 3 is assigned to be used in the attached sketch.
Pin Assignment code snip:

  // pciSetup(0); // Serial Communication
  // pciSetup(1); // Serial Communication
  // pciSetup(2); //This is my trigger pin
  pciSetup(3);
  //  pciSetup(4);
  //  pciSetup(5);
  //  pciSetup(6);
  //  pciSetup(7);
  //  pciSetup(8);
  //  pciSetup(9);
  //  pciSetup(10);
  //  pciSetup(11); // SPI communications
  //  pciSetup(12); // SPI communications
  //  pciSetup(13); // SPI communications
  //  pciSetup(14); // A0
  //  pciSetup(15); // A1
  //  pciSetup(16); // A2
  //  pciSetup(17); // A3
  //  pciSetup(18); // A4  i2c Communication
  //  pciSetup(19); // A5  i2c Communication

Z

UltrasoundPingForForum.ino (8.17 KB)

You have your MAX_DISTANCE set to 5000 cm (50 meters) which is very ambitious for an HC-SR04. You might get 5 meters (500 cm).

The 0 value probably means that it did not receive an echo pulse which means that the sensor was unable to get a distance reading. That could mean the distance was too close ,or too far, or the target is not reflective enough. Perhaps you could try using the latest good (non-zero) reading in place of the 0. Or a rolling average. Or assume it means "too far" and set it to MAX_DISTANCE*10.

Did you set the trigger pins as output?

pinMode(TRIGGER1, OUTPUT);

Or does the library do that?