Problem Controlling Multiple Ping Sensors with Servos for Firefighting Robot

Hi guys! I am supposed to build a firefighting robot for a school project. I am having trouble making servos respond to three ping sensors. I can control multiple ping sensors to ping extremely fast. I have tested the motors to see how long it takes for the motors to change directions without any delays. It only takes like a millisecond in order for the motors to change directions from forward to reverse and vice versa. The problem occurs when I try to make the motors respond to the ping sensors. When I give the motors the command to change direction, the ping sensors stop detecting distances for about 3 seconds.

Why do the sensors go offline for such a long duration? They should only go off for about a millisecond (the time it takes for a pulse to be sent to the motors). I have no delays in my program. In order for my program to work effectively, the ping sensors on the three sides of my robot need to be pinging constantly otherwise the robot goes blind. It will collide against things. What can I do to fix this problem? I appreciate any help you can provide.

It will work with the newping library and the 15 sensor array example. I am running 3 ping sensors, and never stop moving forward unless completely blocked and need to reverse, or spin and find an opening. There is no delay in turning as it approaches an obstacle with that library.

Ty :slight_smile: I will try this and ask for further assistance if I need it.

Hi, we need a posting of your sketch, using # code tags, to see how you command your motors.
Also a circuit diagram of your project in CAD or hand drawn in pdf, jpg or png format.

If you have any delays in your sketch, that will cause your ping pauses.
You would need to look at blink without delay in the IDE example to get around this.

Tom.... :slight_smile:

Northof49:
It will work with the newping library and the 15 sensor array example. I am running 3 ping sensors, and never stop moving forward unless completely blocked and need to reverse, or spin and find an opening. There is no delay in turning as it approaches an obstacle with that library.

I tried using the 15 ping sensor example code like suggested (I just changed it to work for 3 sensors. I made no other changes). However, the output that I am getting is a mixture of some weird symbols. I was just trying to output the distances read by the sensors. How do i fix this? I am using ping sensors with only 3 pins instead of 4. The trig and echo pin are the same. Does this make a difference?

Here is an image of the problem and my slightly modified ultrasonic sensor program.

WorkingPing.ino (2.27 KB)

The Baud rates don't match, that'll give you trash in the serial window. I did that last weekend :slight_smile:

markrmorton:
The Baud rates don't match, that'll give you trash in the serial window. I did that last weekend :slight_smile:

So how do you fix this?

Either change the baud rate in the program to what the serial monitor is set to, or change the serial monitor setting to match the program's specified baud rate. There is a pull down menu of various baud rates when you open the serial monitor.

Thanks, the ultrasonic sensors are showing readings now.

I tried to attach the servos next in the 15 ping sensor example sketch. The ultrasonic sensors stop working as soon as I do this. The wiring is proper. I didn't even send commands to the servos, I just attached them to pins. Why do the ping sensors stop working? How do I fix this?

I attached the program below.

#include <NewPing.h>
#include <Servo.h>

#define SONAR_NUM     3 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 10 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo
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(7, 7, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(8, 8, MAX_DISTANCE),
  NewPing(11, 11, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200); 
  servoLeft.attach(10);  // Set left servo to digital pin 10
  servoRight.attach(9);  // Set right servo to digital pin 9  
  pingTimer[0] = millis() + 75;           // 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();
}

/*void forward() {
  servoLeft.write(180);
  servoRight.write(0);
}

void reverse() {
  servoLeft.write(0);
  servoRight.write(180);
}

void right() {
  servoLeft.write(180);
  servoRight.write(180);
}
void left() {
  servoLeft.write(0);
  servoRight.write(0);
}

void stopR() {
  servoLeft.write(94);
  servoRight.write(96);
}*/