Hello,
I am doing a project which is a obstacle avoiding robot. But the problem is my code only works for forward, backward & right direction moves of the robot. Left direction is not working.
I have attached my full code below. In this project I have used:
- Arduino UNO (1 pcs)
- Ultrasonic Sensor (1 pcs)
- Robot Chassis V1.1 (1 pcs)
- Re Charger able Battery (2 pcs)
- L298N Motor Drive (1 pcs)
- Jumper wires (1pack = 40 pcs)
- Charging & power board (1 pcs)
- Robot Charger (1 pcs)
So Please tell me whether the problem is & also check the other mistakes in my code which is need to correction. My project have to submit between 2-3 days. So please help me. I will be really grateful to you.
Thanks in advance.
—Tanvir.
Code:
const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
// setup pins and variables for HC-SR04 sonar device
int echoPin = 12; // HC-SR04 echo pin (digital 12)
int trigPin = 13; // ping pin (digital 13)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1Pin1 = 10;
int motor1Pin2 = 9;
int motor2Pin1 = 5;
int motor2Pin2 = 6;
int pulsewidth1 = 255;
int pulsewidth2 = 180;
int pulsewidth3 = 10;
int pulsewidth4 = 20;
void setup() {
// set the motor pins as outputs:
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(trigPin, LOW); // stop sending the pulse
pinMode(echoPin, INPUT);
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
}
// check the average distance and move accordingly
if (averageDistance <= 10){
// go backwards
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance <= 35 && averageDistance > 10) {
// turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
if (averageDistance <= 35 && averageDistance > 10) {
// turn right
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance > 35) {
// go forward
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
}
my code for robot.docx (20.4 KB)