My project is to run two motors using motor shield taking the input information from the ping sensor.
When the distance calculated using ping is less than 25 cm the motors will run in forward direction. Else they will run in the opposite direction.
My problem: When the motor shield
is attached to the arduino uno ping sensor is reading "0" in the serial monitor and the motors are also not running.
The program I am using is
#include <AFMotor.h>
#define trigPin 12 //difine the pins of the sensor
#define echoPin 13
AF_DCMotor motor1(1); // set up motors.
AF_DCMotor motor2(2);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor1.setSpeed(255);
motor2.setSpeed(255);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29;
if(distance<25) {
Serial.println(" ");
Serial.println("Obstacle is close than 25 cm");
Serial.println("Obstcle detail");
Serial.println(distance);
Serial.print("cm");
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
else{
Serial.println(" ");
Serial.println("Obstacle is at a distance greater than 25 cm");
Serial.println("Obstcle detail");
Serial.println(distance);
Serial.print("cm");
motor1.run(FORWARD);
motor2.run(FORWARD);
}
}
I have tested the ping and the motor separately, they work quite well. But when attached together is doesn't works. Please help.