Hello there,
I am a beginner at Arduino and I am making an obstacle avoiding car. everything works fine in my car except that only one wheel works and I can't identify the issue. I have taken the move forward function and put it in a new program and both motors worked fine but when I put it with my whole code only the left motor works. Thank you for your help.
#include <Servo.h>
Servo servo_A1;
int distanceRight = 0;
int distanceLeft = 0;
const int Motor1Forward = 7; // pins for motors
const int Motor1Backward = 6;
const int Motor2Forward = 5;
const int Motor2Backward = 4;
const int ENA = 9; // pins for motor speed control for both
const int ENB = 3;
int trigPin = 11; // Trigger
int echoPin = 10; // Echo
long duration, distance, leftDistance, rightDistance;
void setup() { // setups up 6 pins to be output pins
pinMode(Motor1Forward, OUTPUT);
pinMode(Motor1Backward, OUTPUT);
pinMode(Motor2Forward, OUTPUT);
pinMode(Motor2Backward, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo_A1.attach(A1);
}
void loop(){
scan();
servo_A1.write(120);
delay(250);
if (distance <= 20) {
Stop();
delay(800);
moveBackward();
delay(800);
Stop();
delay(200);
lookRight();
delay(50);
scan();
rightDistance = distance;
delay(500);
lookLeft();
delay(50);
scan();
leftDistance = distance;
delay(500);
if (rightDistance >= leftDistance) {
moveRight();
delay(500);
} else {
moveLeft();
delay(500);
}
}
else {
moveForward();
}
}
void lookRight() {
servo_A1.write(60);
delay(1000);
servo_A1.write(120);
return;
}
void lookLeft(){
servo_A1.write(255);
delay(1000);
servo_A1.write(120);
return;
}
void moveForward() {
digitalWrite(Motor1Forward, HIGH); //Motor A will go forward
digitalWrite(Motor1Backward, LOW);
analogWrite(ENA, 200); // Max speed. You can set a speed of a motor from 0 to 255
digitalWrite(Motor2Forward,HIGH);
digitalWrite(Motor2Backward,LOW);
analogWrite(ENB, 200); // Notice that the speeds of your two motors are
}
void Stop() {
digitalWrite(Motor1Forward, LOW); //Motor A will stop
digitalWrite(Motor1Backward, LOW);
analogWrite(ENA, 0);
digitalWrite( Motor2Forward,LOW); //Motor A will stop
digitalWrite( Motor2Backward,LOW);
analogWrite(ENB, 0);
}
void moveBackward() {
digitalWrite(Motor1Forward, LOW); //Motor A will go backward
digitalWrite(Motor1Backward, HIGH);
analogWrite(ENA, 150);
digitalWrite( Motor2Forward,LOW); //Motor B will go backward
digitalWrite( Motor2Backward,HIGH);
analogWrite(ENB, 120);
}
void moveLeft() {
digitalWrite(Motor1Forward, HIGH); //Motor A will go forward
digitalWrite(Motor1Backward, LOW);
analogWrite(ENA, 220); // Max speed. You can set a speed of a motor from 0 to 255
digitalWrite( Motor2Forward,LOW); //Motor B will go backward
digitalWrite( Motor2Backward,HIGH);
analogWrite(ENB, 125);
}
void moveRight() {
digitalWrite(Motor1Forward, LOW); //Motor A will go Backward
digitalWrite(Motor1Backward, HIGH);
analogWrite(ENA, 220); // Max speed. You can set a speed of a motor from 0 to 255
digitalWrite( Motor2Forward,HIGH); //Motor A will go forward
digitalWrite( Motor2Backward,LOW);
analogWrite(ENB, 125);
}
void scan() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
distance = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
// Serial.print(distance);
// Serial.print("cm");
// Serial.println();
}
sketch_oct28a.ino (3.83 KB)