Hi there guys, this is my first project with arduino, so I apologize in advance for any beginner mistakes or seemingly obvious doubts.
I'm doing a project that consists in triggering 2 12vDC motor's trought an ultrasonic sensor when distance < 40cm. I want the motors to be still, and only start rotating when an object enters de 0-40cm zone captated by the sensor.
The components i'm using are:
Arduino Uno
Shield Motor (L293Dx2)
Ultrasonic Sensor (HC-SR04)
2 12vDCmotors
Trought tutorials of 'how to do an obstancle avoiting car' I've already done the hardware and some software parts and it's working great. I'm running this code that i've founded online and modifidet a bit, to test it:
#include <AFMotor.h>
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
void setup() {
Serial.begin(9600); // begin serial communitication
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);
motor1.setSpeed(200);
motor2.setSpeed(200);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Starting");
motor1.run(BACKWARD); // keep as long as there's an obstacle ahead.
motor2.run (BACKWARD);
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
}
}
Now, to try and do the thing that i want, i've humble modifided it to :
#include <AFMotor.h>
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
void setup() {
Serial.begin(9600); // begin serial communitication
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);
motor1.setSpeed(200);
motor2.setSpeed(200);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance > 40)/*if there's an obstacle 40 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.
Serial.println (" The obstacle is declared. ");
Serial.println (" Starting");
motor1.run(0); // keep as long as there's an obstacle ahead.
motor2.run (0);
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
motor2.run(FORWARD);
}
}
I've tried to make it as if the speed of the motor is alaways 0, and if there's an obstacle tectected at distance<40 (cm) it starts running, but the only thing that i keep having is the motors always running.
Can somebody help this beginner, but motivated enthusiast?
Thank you,
RT