Help- Ultrasonic and DC motor

Hi,

im did some small codes on arduino, but i have some problems, when i try to build
a code with an ultrasonic sensor which controls a motor

what i have

1 x ultrasonic sensor
(https://eckstein-shop.de/Parallax-PING-Ultrasonic-Distance-Sensor?curr=EUR&
gclid=CLKs_pzwwdQCFUeeGwodDRAH2Q)

2 x dc motor
https://www.htfelectronics.nl/en/dc-motor-with-wheel.html

1 x arduino nano + L298N Dual H-Bridge Motor Controller

what i want

the motors should run forward, till the sensor finds an obstacle 25 cm in front.
Mots should stop then and turn backwards. the led should only blinking.

thats it, i wrote the code below, motors a running, sensor is showing that
he works in serial monitor, but the motors do not stop and turn bakwards
when the sensor finds the obstacle 25 cm in front.

the code is below, may somebody can help me

tousand thx.

sunny greetings from germany

Alexander

  • Code Written by Alexander. I've explained all the code in the grey comments.

*/
#include <NewPing.h>

// defines pins numbers

int trigPin=7; // define the pins of your sensor
int echoPin=6;
int MAX_DISTANCE=200;

int led1 = A2;
int led2 = A3;

// Motor 1
int dir1PinA = 2;
int dir2PinA = 3;
int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed

// Motor 2
int dir1PinB = 4;
int dir2PinB = 5;
int speedPinB = 10; // Needs to be a PWM pin to be able to control motor speed

// defines variables

NewPing DistanceSensor(trigPin, echoPin, MAX_DISTANCE);

long duration;
int distanceCm;

void setup() {

Serial.begin(9600); // begin serial communitication

//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);
pinMode(dir1PinB,OUTPUT);
pinMode(dir2PinB,OUTPUT);
pinMode(speedPinB,OUTPUT);

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
int speed; // Local variable
unsigned int cm = DistanceSensor.ping_cm();
Serial.print("Distance: ");
Serial.print(cm);
Serial.println("cm");
delay(1000);

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);
distanceCm= duration*0.034/2;
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */

{

analogWrite(speedPinA, 200);//Sets speed variable via PWM
digitalWrite(dir2PinA, HIGH);
analogWrite(speedPinB, 200);//Sets speed variable via PWM
digitalWrite(dir1PinB, HIGH);

digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second

digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second

}

else {
Serial.println ("No obstacle detected. going backward");
delay (15);
analogWrite(speedPinA, 155);//Sets speed variable via PWM
digitalWrite(dir2PinA, LOW);
analogWrite(speedPinB, 155);//Sets speed variable via PWM
digitalWrite(dir1PinB, LOW);
}
}

long duration, distance; // start the scan
// something missing here
if (distance < 25) {

Where do you assign a value to distance?

can you please give me an example how to fix it, thx

Fix it by assigning something to the variable distance. Hint - what are your variables cm, distance and distanceCm for?

Steve

cm, can you give me please a code example, thx

innofuture:
cm, can you give me please a code example, thx

How about:

if (distanceCm < 25) {

You assign a value to distanceCm. Then you test distance. Does that seem right to you ?

Steve

right, i messure the distance, then i control the motors and tell them what todo

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.