I am working on an obstacle avoiding robot. It uses an ultrasound sensor to avoid obstacles, but currently has trouble avoiding objects that are diagonal to it. Thus, i want to add a second ultrasound sensor, and alternate their readings. I am testing the code, which should look right and then leftn and return the distances. The code worked for just one ultrasound. and it is a follows:
#include <NewPing.h>
#include <Servo.h>
#define trig_pin1 A1 //analog input 1
#define echo_pin1 A2 //analog input 2
#define trig_pin2 A3 //analog input 1
#define echo_pin2 A4 //analog input 2
int distance1 = 100;
int distance2 = 100;
#define maximum_distance 200 //sensor function
Servo servo1; //our servo name
Servo servo2;
NewPing sonar1(trig_pin1, echo_pin1, maximum_distance);
NewPing sonar2(trig_pin2, echo_pin2, maximum_distance);
void setup() {
// put your setup code here, to run once:
servo1.attach(11); //our servo pin
servo1.write(90);
servo2.attach(6); //our servo pin
servo2.write(90);
distance1 = readPing1();
distance2 = readPing2();
delay(100);
distance1 = readPing1();
distance2 = readPing2();
delay(100);
distance1 = readPing1();
distance2 = readPing2();
delay(100);
distance1 = readPing1();
distance2 = readPing2();
delay(100);
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
}
}
int readPing1(){
delay(10);
int cm = sonar1.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
int lookRight(){
servo1.write(50);
delay(500);
int distance1 = readPing1();
delay(100);
servo1.write(115);
return distance1;
}
int readPing2(){
delay(10);
int cm = sonar2.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
int lookLeft(){
servo2.write(170);
delay(500);
int distance2 = readPing2();
delay(100);
servo2.write(115);
return distance2;
delay(100);
}
I get the following error:
'readPing1' was not declared in this scope
and I get the same error for the readPing2 function. Why could this be?
if (cm==0) cm=250;