Hi, I was recently viewing some old projects and couldn't figure out why am I getting this error message. Code for reference:
#include <Servo.h>
#define TRIG_PIN_LEFT A0
#define ECHO_PIN_LEFT A1
#define TRIG_PIN_RIGHT A2
#define ECHO_PIN_RIGHT A3
#define SAFETY_DISTANCE
Servo myServo_right, myServo_left;
float Sonar_Left() {
long duration;
float cm;
digitalWrite(TRIG_PIN_LEFT, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN_LEFT, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN_LEFT, LOW);
duration = pulseIn(ECHO_PIN_LEFT, HIGH);
cm = (duration / 2) / 29.1;
return cm;
}
float Sonar_Right() {
long duration;
float cm;
digitalWrite(TRIG_PIN_RIGHT, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN_RIGHT, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN_RIGHT, LOW);
duration = pulseIn(ECHO_PIN_RIGHT, HIGH);
cm = (duration / 2) / 29.1;
return cm;
}
void Forward() {
myServo_right.write(25);
myServo_left.write(25);
}
void Backward() {
myServo_right.write(155);
myServo_left.write(155);
}
void Left() {
myServo_right.write(0);
myServo_left.write(90);
}
void Right() {
myServo_right.write(95);
myServo_left.write(0);
}
void Avoid_Left() {
do {
Right(); // Keeps turning right until its clear from the obstacle.
} while (Sonar_Left() < SAFETY_DISTANCE);
}
void Avoid_Right() {
do {
Left(); // Keeps turning left until its clear from the obstacle.
} while (Sonar_Right() < SAFETY_DISTANCE);
}
void Avoid_Forward() { // This function was made to randomly decide whether it reverses left or right if there is an obstacle on front of him.
do {
int randNumber = random(2);
if (randNumber == 0) {
myServo_right.write(180);
myServo_left.write(90);
} else {
myServo_right.write(95);
myServo_left.write(180);
}
delay(500);
} while ((Sonar_Left() <= SAFETY_DISTANCE) && (Sonar_Right() <= SAFETY_DISTANCE));
}
void Carlito_Forward() { // I tried to make a special forward move type, but Carlito just bugs out when I try and run this code for him.
myServo_right.write(0);
myServo_left.write(90);
delay(500);
myServo_right.write(95);
myServo_left.write(0);
}
void Dance_Dance_Revolution() { // Dances. Used as a default in the switch loop in the loop() function.
int randNumber = random(5);
switch (randNumber) {
case 0:
myServo_right.write(0);
myServo_left.write(90);
break;
case 1:
myServo_right.write(95);
myServo_left.write(0);
break;
case 2:
myServo_right.write(180);
myServo_left.write(90);
break;
case 3:
myServo_right.write(95);
myServo_left.write(180);
break;
case 4:
myServo_right.write(0);
myServo_left.write(180);
break;
}
delay(500);
}
int Obstacle() {
int obstacle;
if (Sonar_Right() > SAFETY_DISTANCE && Sonar_Left() > SAFETY_DISTANCE)
obstacle = 0; // Moves Forward
else if (Sonar_Right() <= SAFETY_DISTANCE && Sonar_Left() <= SAFETY_DISTANCE)
obstacle = 1; // Moves Back
else if (Sonar_Right() <= SAFETY_DISTANCE && Sonar_Left() > SAFETY_DISTANCE)
obstacle = 2; // Turns Left
else if (Sonar_Right() > SAFETY_DISTANCE && Sonar_Left() <= SAFETY_DISTANCE)
obstacle = 3; // Turns Right
return obstacle;
}
void setup() {
Serial.begin(9600);
// Setting the inputs and outputs, without doing this, I will be unable to use the pins.
pinMode(TRIG_PIN_LEFT, OUTPUT);
pinMode(ECHO_PIN_LEFT, INPUT);
pinMode(TRIG_PIN_RIGHT, OUTPUT);
pinMode(ECHO_PIN_RIGHT, INPUT);
myServo_right.attach(10);
myServo_left.attach(9);
}
void loop() {
int obstacle = Obstacle();
switch (obstacle) {
case 0: Forward();
break;
case 1: Avoid_Forward();
break;
case 2: Avoid_Right();
break;
case 3: Avoid_Left();
break;
default: Dance_Dance_Revolution;
break;
}
/* float cm_left = Sonar_Left();
float cm_right = Sonar_Right();
Serial.print("CM Left: ");
Serial.println(cm_left);
Serial.print("CM Right: ");
Serial.println(cm_right); */
}