I'm trying to write a code that will read the distance from the ultrasonic sensor, and depending on that distance the motors should turn in different directions (forwards or backwards). But the code only makes the motors jittering around in one direction, and the code runs through both if- statements no matter what the distance is. What am I doing wrong?
(Since I still don't know how to upload images here) I've linked to a video of the Arduino running the program and a picture where you can see the connections clearer:
https://www.instagram.com/p/BqCiYpXn-OU/
https://www.instagram.com/p/BqCiVLnhD9o/
This is (parts of) what comes up in the monitor:
Duration: 6009 Distance: 102 cm. One way. Distance 2: 102 Other way. Distance 3: 102 Whyyy Duration: 303 Distance: 5 cm. One way. Distance 2: 5 Other way. Distance 3: 5 Whyyy
Duration: 159 Distance: 2 cm. One way. Distance 2: 2 Other way. Distance 3: 2 Whyyy
Duration: 7185 Distance: 122 cm. One way. Distance 2: 122 Other way. Distance 3: 122 Whyyy
Here's the code:
//Motor driver + ultrasonic sensor
//Variables
//Motor A
int in1 = 9;
int in2 = 8;
int enA = 10;
//Motor B
int in3 = 7;
int in4 = 6;
int enB = 5;
//Ultrasonic sensor
const int trigPin = 11;
const int echoPin = 3;
long duration;
int distanceCm;
void setup()
{
//Motor A
pinMode(in1, OUTPUT); //Direction control
pinMode(in2, OUTPUT); //Direction control
pinMode(enA, OUTPUT); //PMW (pulse switch modulation)
//Motor B
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enB, OUTPUT);
//Ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600); //Begins serial communications?
}
void loop()
{
//Reads distance with ultrasonic sensor
digitalWrite(trigPin, LOW); //Clears trigPin.
delayMicroseconds(2);
//Turns trigPin on for 10 microseconds and then turns it off again.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Reads echoPin which sends back soundwawes travel time in microseconds.
duration = pulseIn(echoPin, HIGH);
//Calculates distance in cm
distanceCm=(duration)*(0.034/2);
//Prints distance in the monitor.
Serial.print("Duration: ");
Serial.print(duration);
Serial.print(" Distance: ");
Serial.print(distanceCm);
Serial.print(" cm. ");
//Motors turns in different directions depending on the distance.
//If distance is less than 15 cm -> turn in one direction.
if(distanceCm <= 15);
{
Serial.print(" One way. "); //Check
//Motors turn the same way
//Motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
//Motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//Sets speed for motors?
analogWrite(enA, 80); //Motor A
analogWrite(enB, 80); //Motor B
Serial.print(" Distance 2: "); //Check
Serial.print(distanceCm); //Check
}
//If distance is more than 15 cm -> turn in the other direction.
if(distanceCm > 15);
{
Serial.print(" Other way. "); //Check
//Motor A
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 80);
//Motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enB, 80);
Serial.print(" Distance 3: "); //Check
Serial.print(distanceCm); //Check
}
Serial.print(" Whyyy "); //Check
}