Good Day,
I'm baffled at this issue. When my Mega 2560 is connected to my Mac and Arduino IDE 2.0.4, I get a different response from the Ultrasonic sensor.
While connected, it triggers an if statement when the distance is less than 4 cm. The trigger moves the servo to move the sensor 90 degrees left to 90 degrees right. It then determines the furthest distance without an obstacle and displays the result to the serial printer.
When it is not connected and powered by the 2 x 3.2v batteries, it triggers the if statement (registered by a distance less than 4 cm) and begins to move the servo.
Thinking that the surface I am placing the project on may be causing the issue, I tried it in various hard surfaces with no background noise. No matter what I do, it responds the same way when running from battery power. The batteries are fully charged too.
Here's the code and diagram of this project:
#include <Servo.h>
#include <HCSR04.h>
#define TRIGPIN 34
#define ECHOPIN 36
UltraSonicDistanceSensor ultraSonic(34, 36);
Servo head;
int distanceIN = ultraSonic.measureDistanceCm();
// Variables for Ultrasonic distances based on head direction
int max;
String maxName;
int farRight;
String farRightName = "Far Right";
int quarterRight;
String quarterRightName = "Quarter Right";
int front;
String frontName = "Front";
int quarterLeft;
String quarterLeftName = "Quarter Left";
int farLeft;
String farLeftName = "Far Left";
void setup() {
// SET UP SERVO
head.attach(52);
head.write(90);
Serial.begin(9600);
}
void loop() {
distanceIN = ultraSonic.measureDistanceCm();
// Serial.println(distanceIN);
if (distanceIN < 4) {
Serial.println("Obstacle in front of me");
front = distanceIN; // take initial reading
Serial.print("Front: ");
Serial.println(front);
delay(1000);
head.write(45); // Look near right
quarterRight = ultraSonic.measureDistanceCm();
Serial.print("Quarter right: ");
Serial.println(quarterRight);
delay(0750);
head.write(0); // Look far right
farRight = ultraSonic.measureDistanceCm();
Serial.print("Far right: ");
Serial.println(farRight);
delay(0750);
head.write(135); // Look near left
quarterLeft = ultraSonic.measureDistanceCm();
Serial.print("Quarter left: ");
Serial.println(quarterLeft);
delay(0750);
head.write(180); // Look far left
farLeft = ultraSonic.measureDistanceCm();
Serial.print("Far left: ");
Serial.println(farLeft);
delay(0750);
head.write(90); // Look forward
max = front;
maxName = frontName;
if (farRight > max) {
max = farRight;
maxName = farRightName;
}
if (quarterRight > max) {
max = quarterRight;
maxName = quarterRightName;
}
if (quarterLeft > max) {
max = quarterLeft;
maxName = quarterLeftName;
}
if (farLeft > max) {
max = farLeft;
maxName = farLeftName;
}
Serial.println(farRight);
Serial.println(quarterRight);
Serial.println(quarterLeft);
Serial.println(farLeft);
if (maxName == "Front") {
Serial.print("Direction to move is ");
Serial.print(maxName);
Serial.print(" with a distance of ");
Serial.print(max);
Serial.println(" cm");
}
if (maxName == "Far Right") {
Serial.print("Direction to move is ");
Serial.print(maxName);
Serial.print(" with a distance of ");
Serial.print(max);
Serial.println(" cm");
}
if (maxName == "Quarter Right") {
Serial.print("Direction to move is ");
Serial.print(maxName);
Serial.print(" with a distance of ");
Serial.print(max);
Serial.println(" cm");
}
if (maxName == "Quarter Left") {
Serial.print("Direction to move is ");
Serial.print(maxName);
Serial.print(" with a distance of ");
Serial.print(max);
Serial.println(" cm");
}
if (maxName == "Far Left") {
Serial.print("Direction to move is ");
Serial.print(maxName);
Serial.print(" with a distance of ");
Serial.print(max);
Serial.println(" cm");
}
}
}
I'm grateful for any assistance provided.
Sincerely,
Kevin