hello everyone please help me, I've been trying to make a project (auto-braking system) using arduino uno, L298n motor driver , ultrasonic sensor and dc motor.
I have uploaded the code in the arduino but the motor is not stopping when the obstacle is detected..
here is the code I'm using.....
#include <Ultrasonic.h> #include <L298N.h>
//Define trig and echo pins for the ultrasonic sensor #define trigPin 8 #define echoPin 9
//Define pin numbers for the L298N library #define enA 5 #define in1 6 #define in2 7
//Create an ultrasonic sensor object
Ultrasonic ultrasonic(trigPin, echoPin);
//Create a L298N object for the motor
L298N motor(enA, in1, in2);
bool objectFlag = false;
byte objectSeen = 20;
byte objectGone = 30;
//^****
void setup()
{
//Set the motor's speed
motor.setSpeed(1000);
} //END of setup()
//^****
void loop()
{
//Measure the distance to the obstacle
long distance = ultrasonic.read();
//*******************************************
//If the distance is less than objectSeen CM
if (objectFlag == false && distance < objectSeen)
{
//Stop the car
motor.stop();
//we see an object
objectFlag = true;
}
//*******************************************
//If the distance is equal to or more than objectGone CM
if (objectFlag == true && distance >= objectGone)
{
//object is gone
objectFlag = false;
First you need to post your code so we can read and copy it.
In the IDE, click on Edit, then Copy For Forum, that will copy you code. Then just come here and do a paste.
A wiring diagram of how you have things connected would also help.
#include <Ultrasonic.h>
#include <L298N.h>
//Define trig and echo pins for the ultrasonic sensor
#define trigPin 8
#define echoPin 9
//Define pin numbers for the L298N library
#define enA 5
#define in1 6
#define in2 7
//Create an ultrasonic sensor object
Ultrasonic ultrasonic(trigPin, echoPin);
//Create a L298N object for the motor
L298N motor(enA, in1, in2);
bool objectFlag = false;
byte objectSeen = 20; //change as needed
byte objectGone = 30; //change as needed
//********************************************^************************************************
void setup()
{
//Set the motor's speed
motor.setSpeed(1000);
} //END of setup()
//********************************************^************************************************
void loop()
{
//Measure the distance to the obstacle
long distance = ultrasonic.read();
//*******************************************
//If the distance is less than objectSeen CM
if (objectFlag == false && distance < objectSeen)
{
//Stop the train
motor.stop();
//we see an object
objectFlag = true;
}
//*******************************************
//If the distance is equal to or more than objectGone CM
if (objectFlag == true && distance >= objectGone)
{
//object is gone
objectFlag = false;
//start the train
motor.forward();
}
} //END of loop()
Your code only starts the motor once it's been stopped. If it never stops then you must have a wiring error somewhere. If your diagram is correct then there is no ground connection to the L298 driver.
Turn the builtin LED ON/OFF when you check the distance so you can see if that is working.
I moved your topic to an appropriate forum category @chamo-luke .
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.