I'm building a simple robot, using an Arduino Uno, an Arduino Motor Shield, 2 DC motors (this is the part I used https://www.aliexpress.com/item/33026775429.html) and 2 12V batteries. I have got the robot to control 2 motors, but I also want it to navigate using SR04 ultrasonic sensor. I've put two scenarios into the void loop, if distance measured by the ultrasonic sensor is greater than 40cm it drives forward, else it should reverse and change direction. However, when my code runs all that happens is one of my motors is completely still, and the other jerks back and forth at regular intervals. I've tried making many amendments to the code, but the outcome is always the same. Please take a look at the code I've written and explain where I'm going wrong. When I write code just to control the motors they both work, and when there was a loose connection to the ultrasonic sensor both motors worked, but once the sensor is involved everything grinds to a halt. Here's my code:
const int brakeleft = 9; //identifies pin 9
const int speedleft = 3; //identifies pin 3
const int directionright = 13; //identifies pin 13
const int brakeright = 8; //identifies pin 8
const int speedright = 11; //identifies pin 11
const int trigPin = 7; //identifies pin 7
const int echoPin = 6; //identifies pin 6
long duration; //creates a variable under which the duration between emission and reception of sound can be stored
int distance;
void setup() {
pinMode(trigPin, OUTPUT); //assigns a role to the pin we will be emitting sound on
pinMode(echoPin, INPUT); //assigns a role to the pin that will be reporting back the return of that sound wave
pinMode(brakeleft, OUTPUT); //assigns a role to the pin that will be stopping motor A
pinMode(directionleft, OUTPUT); //assigns a role to the pin that will be deciding motor A's direction
pinMode(speedleft, OUTPUT); //assigns a role to the pin that will be deciding motor A's speed
pinMode(brakeright, OUTPUT); //assigns a role to the pin that will be stopping motor B
pinMode(directionright, OUTPUT); //assigns a role to the pin that will be deciding motor B's direction
pinMode(speedright, OUTPUT); //assigns a role to the pin that will be deciding motor B's speed
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); //emits ultrasonic pulse
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //recieves the echo of the ultrasonic pulse
distance = duration * 0.01715; //converts duration for sound to echo into a distance in cm, based on the speed of light, half of 0.0343 cm per second
if (distance > 40) {
delay(10);
digitalWrite(directionleft, HIGH); //Make motor A spin forwards
digitalWrite(directionright, HIGH); //Make motor B spin forwards
digitalWrite(brakeleft, LOW); //Disengage the brake for motor A
digitalWrite(brakeright, LOW); //Disengage the brake for motor B
analogWrite(speedleft, 50); //Spins the motor A at a low speed
analogWrite(speedright, 50); //Spins motor B at a low speed
delay(3000);
}
else {
delay(10);
digitalWrite(directionleft, LOW);
digitalWrite(directionright, LOW);
digitalWrite(brakeleft, LOW);
digitalWrite(brakeright, LOW);
analogWrite(speedleft, 50);
analogWrite(speedright, 50);
delay(2000);
analogWrite(brakeright, HIGH);
analogWrite(speedright, 0);
digitalWrite(directionleft, LOW);
digitalWrite(brakeleft, LOW);
analogWrite(speedleft, 50);
delay(2000);
}
}