control dc motor via ultrasonic distance module and L298 motor driver

plz , i want code to make dc motor move forword when something is in front of ultrasonic distance module for 2 seconds then stop,and move backward when nothing in front it ! for 2 seconds only too ...
i tried alot and failed ! and iam using L298 motor driver.... thanks for you :smiley:

hi ramy1rafat,

We'd love to help, but we'll need to see your code and a diagram of your circuit.

Click the </> button above and paste your code inside the blocks.

How do you have the 298 wired up? Does the motor control work using simple button presses?

Pat


http://store1.up-00.com/2016-08/1472134538241.jpg

http://store1.up-00.com/2016-08/147213453922.jpg

it is a part of project ,,i want code to make that to make dc motor move forword when something is in front of ultrasonic distance module for 2 seconds then stop,and move backward when nothing in front it ! for 2 seconds only too ...
i tried alot and failed ! and iam using L298 motor driver.
and i will edit it to fit my ciruit !!

i want to know the problem in this code ,, i want code to make dc motor move forword when something is in front of ultrasonic distance module for 2 seconds then stop,and move backward when nothing in front it ! for 2 seconds only too ...
i tried alot and failed ! and iam using L298 motor driver.... thanks for you
this code is done uploded ,but when i am put my hand on ultrasonic ,nothing happen

const int trigPin = 5;

const int echoPin = 6;
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 7;
void setup() {

// initialize serial communication:

Serial.begin(9600);
pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT); // attaches the servo on pin 9 to the servo object

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 7 && cm < 14)

{

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2,HIGH ); // sets the servo position according to the scaled value

delay(4000);

}

else if ( cm < 8)

{
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value

delay(100);

}

else

{

digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

What ultrasonic sensor are you using? It looks like a Ping))) which is very simple to use, your code on the other posting looks complicated. Did you get it from someone that wrote it for your sensor?

I suggest you divide your project into small pieces:

  1. Get the sensor to measure distances in a loop and report the range to the serial output to make sure it works,

  2. Get the motor control to work, first forward, then in reverse with simple scripts and no sensor,

3 combine the two.

Nice smiley in your code above. Smileys do not compile though...

If you want some help here, PLEASE use code tags to update your code in your post (and CTRL-T first in the IDE before copying so that its indented in a readable manner)

I'd recommend you go look again at source code using ultrasonic sensor and what goes in setup() and what goes in the body when you sense.

const int trigPin = 5;

const int echoPin = 6;
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 7;
void setup() {

// initialize serial communication:

Serial.begin(9600);
pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT); // attaches the servo on pin 9 to the servo object
digitalWrite(enablePin, HIGH);
// attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 7 && cm < 14)

{

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2,HIGH ); // sets the servo position according to the scaled value

delay(100);

}

else if ( cm < 8)

{
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value

delay(100);

}

else

{

digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value

delay(100);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

now this code is working,but now i want to make motor move in one direction when i put something in front module for 2 sec and stop ,,,and in other direction when i remove this thing for 2 sec then stop ! ? what should i do ?

Also, don't post the same question twice. You've already got a thread running for the same topic in this forum - you'll confuse the readers that are trying to help you.

You've got 3 conditions in your code to test for distance that sets levels for your H bridge. What do those represent and what do you want the motors to do in them?

i want edit in code ,to make motor move in one direction when i put something in front module for 2 sec and stop ,,,and in other direction when i remove this thing for 2 sec then stop ! ?

Still smiley in the code

???

const int trigPin = 5;

const int echoPin = 6;
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 7;
void setup() {

// initialize serial communication:

Serial.begin(9600);
pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT); // attaches the servo on pin 9 to the servo object
digitalWrite(enablePin, HIGH);
// attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void loop() {

// and the distance result in centimeters:

long duration, cm;

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(20);

digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// the condition for the distance

if ( cm > 1 && cm < 24)

{

digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2,HIGH ); // sets the servo position according to the scaled value

delay(1000);

}

else

{

digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value

delay(1000);

}

Serial.print(cm);

Serial.print("cm");

Serial.println();

delay(100);

}

long microsecondsToCentimeters(long microseconds) {

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;

}

this code has 2 coditions
i want edit in code ,to make motor move in one direction when i put something in front module for 2 sec and stop ,,,and in other direction when i remove this thing for 2 sec then stop ! ?

Come on - PLEASE POST CODE WITH THE CODE TAGs SO THAT IT IT WELL FORMATTED IN THE FORUM. Read the How to use this forum post


this code has 2 coditions
i want edit in code ,to make motor move in one direction when i put something in front module for 2 sec and stop ,,,and in other direction when i remove this thing for 2 sec then stop ! ?

So where do you capture the time something is in front of the device or removed?

Do you think

pinMode(trigPin, OUTPUT);orpinMode(echoPin, INPUT); belong to the loop()? Have you seen any example doing so?

what is the pin configuration in your code for L298 and the 2 ultrasonics.,.?

Someone PLease Help
It says duration is unspecified for the code above

It’s defined as a local variable in the loop()

void loop() {
// and the distance result in centimeters:
[color=red]long duration[/color], cm;Β   [color=red]// <==== HERE[/color]
pinMode(trigPin, OUTPUT);
...
}

Post YOUR code using code tags.