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);
}