Error Redefenition of Newping ultrasonic

I am receiving an error redefinition of newping ultrasonic while compiling the code below.
I am trying to create an obstacle avoiding autonomous car with two ultrasonic sensors on the sides.
Once any of the sensors detect an obstacle either side the car should stop.
Would you advise on what the issue might be and any solving?
Thanks

#include <Servo.h>

#include <NewPing.h>

#define leftIr A0
#define rightIr A1
#define leftIrPower 8
#define rightIrPower 10

#define MotorSpeed 3
#define MotorP 4
#define MotorN 5

#define leftultrasonicTrigger 11
#define leftultrasonicEcho 12
#define rightultrasonicTrigger 11
#define rightultrasonicEcho 12
Servo myservo;
// Max distance read by ultrasonic sensor
const int maxDistance = 400;
NewPing ultrasonic(leftultrasonicTrigger, leftultrasonicEcho, maxDistance);
NewPing ultrasonic(rightultrasonicTrigger, rightultrasonicEcho, maxDistance);

const char GREEN_SIGN = 'd'; // Drive
const char RED_SIGN = 's'; // Stop

const int MIN_DISTANCE_FROM_OBJECT = 5;
// error of the distance read by the ultrasonic sensor
const float DISTANCE_OFFSET = -0.31;

// motors speed (0 - 255)
const float FORWARD_SPEED = 100;
const float CURVE_SPEED = FORWARD_SPEED;

// variable responsible for ignoring small variations in reflectance
const int SENSITIVITY_OFFSET = 30;

// reflectance thresholds of the external parts to the central band
int leftThreshold;
int rightThreshold;

char vehicleAction;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(leftIrPower, OUTPUT);
  pinMode(rightIrPower, OUTPUT);
  leftThreshold = analogRead(leftIr);
  rightThreshold = analogRead(rightIr);
  
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  
  
  pinMode(leftultrasonicTrigger, OUTPUT); 
  pinMode(leftultrasonicEcho, INPUT);
  pinMode(rightultrasonicTrigger, OUTPUT); 
  pinMode(rightultrasonicEcho, INPUT);
}

void loop() {
  if (Serial.available() > 0) {
    char serial = Serial.read();

    if (serial == GREEN_SIGN || serial == RED_SIGN) {  
      vehicleAction = serial;
    }
  }
  
  if (vehicleAction == GREEN_SIGN)
    drive();
  else if (vehicleAction == RED_SIGN)
    stopCar();
}

void drive() {
  if (hasObstacle()) stopCar();
  while (hasObstacle()) delay(100);
  
  // If a new vehicle action was received, must process it first
  if (Serial.available() > 0) return;
  
  int leftIrValue = analogRead(leftIr) - SENSITIVITY_OFFSET;
  int rightIrValue = analogRead(rightIr) - SENSITIVITY_OFFSET;
  
  if (leftIrValue <= leftThreshold && rightIrValue <= rightThreshold)
    moveForward();
  else if (leftIrValue > leftThreshold && rightIrValue <= rightThreshold)
    moveLeft();
  else if (leftIrValue <= leftThreshold && rightIrValue > rightThreshold)
    moveRight();
  else if (leftIrValue > leftThreshold && rightIrValue > rightThreshold)
    stopCar();
  
  // Runs an action during just 125ms to make sure it doesn't cross the line  
  delay(125);
  stopCar();
  delay(75);
}

boolean hasObstacle() {
  int distance = ultrasonic.ping_cm();
  
  // Check if distance different of 0, because NewPing library returns 0 if 
  // the distance is greater than the specified
  return distance > 0 && distance <= MIN_DISTANCE_FROM_OBJECT;
}

void moveForward() {
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(90);
;
}

void moveLeft() {
  // PWM
  analogWrite(MotorSpeed, 0);
  analogWrite(MotorSpeed, CURVE_SPEED);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(135);
 
}

void moveRight() {
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  analogWrite(MotorSpeed, 0);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(45);

}

void stopCar() {
  // PWM
  analogWrite(MotorSpeed, 0);
  analogWrite(MotorSpeed, 0);
  
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(90);
 
}

am receiving an error redefinition of newping ultrasonic while compiling the code below.

NewPing ultrasonic(leftultrasonicTrigger, leftultrasonicEcho, maxDistance);
NewPing ultrasonic(rightultrasonicTrigger, rightultrasonicEcho, maxDistance);

No surprise there

Give each of the instances a different name

UKHeliBob:

NewPing ultrasonic(leftultrasonicTrigger, leftultrasonicEcho, maxDistance);

NewPing ultrasonic(rightultrasonicTrigger, rightultrasonicEcho, maxDistance);



No surprise there

Give each of the instances a different name

i tried renaming and it compilled

#include <SoftwareSerial.h>

#include <Servo.h>

#include <NewPing.h>

#define leftIr A0
#define rightIr A1
#define leftIrPower 8
#define rightIrPower 10

#define MotorSpeed 3
#define MotorP 4
#define MotorN 5

#define leftultrasonicTrigger 11
#define leftultrasonicEcho 12
#define rightultrasonicTrigger 11
#define rightultrasonicEcho 12
Servo myservo;
// Max distance read by ultrasonic sensor
const int maxDistance = 400;
NewPing leftultrasonic(leftultrasonicTrigger, leftultrasonicEcho, maxDistance);
NewPing rightultrasonic(rightultrasonicTrigger, rightultrasonicEcho, maxDistance);

const char GREEN_SIGN = 'd'; // Drive
const char RED_SIGN = 's'; // Stop

const int MIN_DISTANCE_FROM_OBJECT = 8;
// error of the distance read by the ultrasonic sensor
const float DISTANCE_OFFSET = -0.31;

// motors speed (0 - 255)
const float FORWARD_SPEED = 100;
const float CURVE_SPEED = FORWARD_SPEED;

// variable responsible for ignoring small variations in reflectance
const int SENSITIVITY_OFFSET = 30;

// reflectance thresholds of the external parts to the central band
int leftThreshold;
int rightThreshold;

char vehicleAction;

void setup() {
  Serial.begin(115200);
  myservo.attach(13);
  pinMode(leftIr, INPUT);
  pinMode(rightIr, INPUT);
  pinMode(leftIrPower, OUTPUT);
  pinMode(rightIrPower, OUTPUT);
  leftThreshold = analogRead(leftIr);
  rightThreshold = analogRead(rightIr);
  
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorP, OUTPUT);
  pinMode(MotorN, OUTPUT);
  
  
  pinMode(leftultrasonicTrigger, OUTPUT); 
  pinMode(leftultrasonicEcho, INPUT);
  pinMode(rightultrasonicTrigger, OUTPUT); 
  pinMode(rightultrasonicEcho, INPUT);
}

void loop() {
  if (Serial.available() > 0) {
    char serial = Serial.read();

    if (serial == GREEN_SIGN || serial == RED_SIGN) {  
      vehicleAction = serial;
    }
  }
  
  if (vehicleAction == GREEN_SIGN)
    drive();
  else if (vehicleAction == RED_SIGN)
    stopCar();
}

void drive() {
  if (hasObstacle()) stopCar();
  while (hasObstacle()) delay(100);
  
  // If a new vehicle action was received, must process it first
  if (Serial.available() > 0) return;
  
  int leftIrValue = analogRead(leftIr) - SENSITIVITY_OFFSET;
  int rightIrValue = analogRead(rightIr) - SENSITIVITY_OFFSET;
  
  if (leftIrValue <= leftThreshold && rightIrValue <= rightThreshold)
    moveForward();
  else if (leftIrValue > leftThreshold && rightIrValue <= rightThreshold)
    moveLeft();
  else if (leftIrValue <= leftThreshold && rightIrValue > rightThreshold)
    moveRight();
  else if (leftIrValue > leftThreshold && rightIrValue > rightThreshold)
    stopCar();
  
  // Runs an action during just 125ms to make sure it doesn't cross the line  
  delay(125);
  stopCar();
  delay(75);
}

boolean hasObstacle() {
  int leftdistance = leftultrasonic.ping_cm();
  
  // Check if distance different of 0, because NewPing library returns 0 if 
  // the distance is greater than the specified
  return leftdistance > 0 && leftdistance <= MIN_DISTANCE_FROM_OBJECT;
  int rightdistance = rightultrasonic.ping_cm();
  
  // Check if distance different of 0, because NewPing library returns 0 if 
  // the distance is greater than the specified
  return rightdistance > 0 && rightdistance <= MIN_DISTANCE_FROM_OBJECT;
}

void moveForward() {
  // PWM
  analogWrite(MotorSpeed, FORWARD_SPEED);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(90);
;
}

void moveLeft() {
  // PWM
  analogWrite(MotorSpeed, 0);
  analogWrite(MotorSpeed, CURVE_SPEED);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(135);
 
}

void moveRight() {
  // PWM
  analogWrite(MotorSpeed, CURVE_SPEED);
  analogWrite(MotorSpeed, 0);
  
  // Direction
  digitalWrite(MotorP, HIGH);
  digitalWrite(MotorN, LOW);
  myservo.write(45);

}

void stopCar() {
  // PWM
  analogWrite(MotorSpeed, 0);
  analogWrite(MotorSpeed, 0);
  
  // Direction
  digitalWrite(MotorP, LOW);
  digitalWrite(MotorN, LOW);
  myservo.write(90);
 
}

I am having two ultrasonic sensors that should be attached to the left and right side of an autonomous car to detect objects within the span o the car. I am a little confused about how I can interface the two ultrasonic sensors to the Arduino Uno r3.
Any direction will be appreciated.

Duplicate topics merged

Why did you create a new topic about the same problem ?
Doing so wastes time and splits answers that could be relevant

UKHeliBob:
Duplicate topics merged

Why did you create a new topic about the same problem ?
Doing so wastes time and splits answers that could be relevant

okay

Hi,

#define leftultrasonicTrigger 11
#define leftultrasonicEcho 12
#define rightultrasonicTrigger 11
#define rightultrasonicEcho 12

You can't have both ultra sonic units connected to the same pins.
Can I suggest you write some code JUST concerning the ultrasonic units and get them working and producing data.
Leave the rest of the code out till you have the sensors working in code.

Have you written your code in stages?
That is write code JUST for each input and output device, THEN when they all work, begin to combine them.

I see an awful lot of code, and now you have this problem

Do you know if the other parts of your code work?

Tom.... :slight_smile: