My object avoidance car is coming together nicely but I need to tweak the code some more. So far the car is able to move forward until it reaches 10cm from a wall, then backs up as it turns left (as seen in the code below). The problem is when it backs up it doesn't complete at least a 90 degree turn before moving forward again. This causes it to continuously run into the same wall with the right wheel.
I feel that this problem would be resolved if I mounted the ultrasonic sensor on a rotating servo instead of being fixed on the car and increase the sensor range. But until I buy a servo for this I would like to figure out how to make it back up for 3 or 4 more seconds before moving forward again.
Any ideas would be appreciated!
#include <NewPing.h>
#include <AFMotor.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer = 75; // Holds the next ping time, start at 75ms to give time for the Arduino pins to stabilize.
AF_DCMotor motor2(2);
AF_DCMotor motor4(4);
void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}
void loop() {
if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set the next ping time.
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
Serial.print("Ping: ");
Serial.print(cm); // Print the result (0 = outside the set distance range, no ping echo)
Serial.println("cm");
if (cm >10) // Go forward
motor2.setSpeed(255);
motor4.setSpeed(255);
motor2.run(FORWARD);
motor4.run(FORWARD);
if (cm <10) // backup turning left
motor2.run(BACKWARD);
motor4.setSpeed(0);
}
}
Sorry I thought from the second sentence in my first comment and the last comment in my code it was clear. When the sensor reaches less than 10cm from a wall it backs up while turning left then both motors continue forward.
Update!!
I added a delay at the end of my last if statement
if (cm <10)
motor2.run(BACKWARD);
if (cm <10)
motor4.setSpeed(0);
delay(250);
That seems to have allowed the car to backup further before going forward, missing the wall.
I will order a servo soon and hope it will give the sensor a wider range!
No, it wasn't at all clear.
I have no idea of the mechanical or electrical arrangements of your vehicle.
Your "if (cm > 10)" only causes the very next expression "motor.setSpeed(255);" to be conditional.
The next three will be executed whatever the value of "cm".
Your indentation made it look as though you intended all four to be conditional.
Point the sensor slightly to the right so that it detects the imminent wheel/wall collision and triggers another backup/avoidance maneuver? (And perhaps reduce the angle turned by this maneuver so that the vehicle can turn parallel ish to the wall instead of always turning away from it?)