Hello, I am sorry for the late reply!
The Autonomous car is going well but we have changed the design slightly and added two more motors making it a 4wd car. This was due to the unreliability of the motor at the front which was key for turning the car. We are using the same motor board and sensor and I have just written a basic code which turns the motors on, and if the distance (in front of the car) is less than 15 cm, it reverses:
#define trigPin 10
#define echoPin 12
#include <AFMotor.h>
int sound = 250;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200);
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void loop() {
uint8_t i;
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10) {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(100);
}
else {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(10);
}
}
One problem which i am having is that when it detects that it an object is 15cm away, it reverses then moves forward straight away causing the wheels to crash. How can i solve this problem and how can i make it turn when an object is 15cm away and then go forwards?
Any help would be much appreciated