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);
}
}