multiple ping sensors for an autonomous robot

Hi people i require some help please, i'm new to the arduino world :slight_smile: , im building an autonomous robot and im using 3 ping sensors. 1 sensor connected on either side of the robot and 1 in the front, robot consists of two wheels and a tail wheel.i want to use differential drive enabling the robot to move left,right,foward. All sensors seem to be working well when viewed on the serial monitor,here is the code.

const int pingPin1 = 2;
const int pingPin2 = 4;
const int pingPin3 = 7;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  ping1();
  delay(250);
  ping2();
  delay(250);
  ping3();
  delay(250);
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void ping1()
{
  long duration1, cm1;
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);
  pinMode(pingPin1, INPUT);
  duration1 = pulseIn(pingPin1, HIGH);
  cm1 = microsecondsToCentimeters(duration1);
  Serial.print("Left = ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
}

void ping2()
{
  long duration2, cm2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("Right = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
} 
void ping3()
{
  long duration3, cm3;
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);
  cm3 = microsecondsToCentimeters(duration3);
  Serial.print("Front = ");
  Serial.print(cm3);
  Serial.print("cm");
  Serial.println();
}

.

Now the problem is that i require the robot to be able to avoid obstacles, once it comes within 20cm of an obstacle:

for example, if sensor1,sensor2,sensor3 are all greater the 20cm then robot moves foward

if sensor1 less than 20cm and sensor 2/3 are greater than 20cm then robot turns right

if sensor1 and 2 greater than 20cm amd sensor 3 less than 20 then robot turns right

can someone please guide me with the code on how to go about this, will be much appreciated, thanks

Firstly, I'd say you've got three near-identical functions, when one would do the work just as well :

long pingRange (int pingPin)
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  return microsecondsToCentimeters(pulseIn(pingPin, HIGH));
}

Shorter code means easier modifications.

Notice, too, that AWOL's function uses no global variables. There is no reason for the distances to be global when the function could return a value that you could store in a local variable.

Reducing the scope of the variables also reduces the chances of accidentally altering a value in a function that should not be altering a value.

Since you now have a function that can return values, writing the if statements that determine which way to go is trivial. The only thing to be concerned about now is that the functions you actually write to make the robot go left, straight, or right should do ONLY that. Make the robot change direction, that is, and get moving. Do NOT make them make the robot actually travel any distance in that direction. In other words, those functions must NOT use delay().

hi guys thanks so much for the reply

i doubled checked the hardware and tested each sensor seperately to control both motors and they work fine. But when using 3 sensors i seem to have a problem, here is the code i attempted, it compiles ok but the robot does not move foward as expected.

int STBY = 10; //standby to control bot motors with a dual h-bridge tb6612fng
const int pingPin1 = 2;// left sensor
const int pingPin2 = 4;//right sensor
const int pingPin3 = 7;//front sensor
const int threshold = 20;

//Motor A
int PWMA = 3; //Speed control of right motor
int AIN1 = 9; //Direction h-bridge
int AIN2 = 8; //Direction

//Motor B
int PWMB = 5; //Speed control of left motor
int BIN1 = 11; //Direction h-bridge
int BIN2 = 12; //Direction


void setup()
{
  Serial.begin(9600);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  
  pinMode(STBY, OUTPUT);

  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
}

void loop()
{
 
  ping1();
  delay(250);
  ping2();
  delay(250);
  ping3();
  delay(250);
  
  if (pingPin1 && pingPin2 && pingPin3 > threshold)
  {
    digitalWrite(PWMA,1);
    digitalWrite(PWMB,1);
    drive_foward();// function call
  }
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void ping1()
{
  long duration1, cm1;
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);
  pinMode(pingPin1, INPUT);
  duration1 = pulseIn(pingPin1, HIGH);
  cm1 = microsecondsToCentimeters(duration1);
  Serial.print("Left = ");
  Serial.print(cm1);
  Serial.print("cm");
  Serial.println();
}

void ping2()
{
  long duration2, cm2;
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);
  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);
  cm2 = microsecondsToCentimeters(duration2);
  Serial.print("Right = ");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
} 
void ping3()
{
  long duration3, cm3;
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);
  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);
  cm3 = microsecondsToCentimeters(duration3);
  Serial.print("Front = ");
  Serial.print(cm3);
  Serial.print("cm");
  Serial.println();
} 
void drive_foward()
     {
  digitalWrite(AIN1,HIGH);// right wheel foward h-bridge
  digitalWrite(BIN1,HIGH);//left wheel foward h-bridge
  analogWrite(PWMA,115);// speed control
  analogWrite(PWMB,115);// speed control
  return;
     }

can you please assist me as to where im going wrong,thanks

  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);

The Arduino is not stupid. You only need to tell it the mode of a pin once/ PWM pins, addressed using analogWrite() already KNOW that they are OUTPUT pins.

int PWMB = 5; //Speed control of left motor

On which Arduino is pin 5 a PWM pin?

  ping1();
  delay(250);
  ping2();
  delay(250);
  ping3();

Reading comprehension issues, I see.

  if (pingPin1 && pingPin2 && pingPin3 > threshold)

Inventing shortcuts hasn't worked for anyone else, either. What makes you think it will work for you?

Given that the pin numbers are hardcoded, with values well below threshold, it just doesn't seem reasonable that this shorthand, even if the compiler understood it, would ever evaluate to true.