Car chasis with HC-SR04 sensor

Hey guys, recently I build myself a robot car. So I would like to put an ultrasonic sensor into it to detect obstacle , stop the car and the move backward. So I am stuck in the programming. Can anyone guide me how can I do that ?

Thank you.

My current code that will move forward and stop when sense obstacle less than 20cm. But when I start the car, the car does not move forward properly. It will stop and move alterhating. So what is the problem ?

:slight_smile:

int motorPin1 = 7; // pin 2 on L293D IC
int motorPin2 = 8; // pin 7 on L293D IC
int enablePin_1 = 6; // pin 1 on L293D IC
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int state;
int distance;
 
#include <NewPing.h>
#define TRIGGER_PIN  12
#define ECHO_PIN     11
NewPing sonar(TRIGGER_PIN, ECHO_PIN);
void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
}

void loop()
{
 distance = sonar.ping_cm();
  digitalWrite(motorPin1, LOW); 
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW); 
digitalWrite(motorPin4, HIGH);
if (distance <20)
{
 digitalWrite(motorPin1, LOW); 
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW); 
digitalWrite(motorPin4, LOW);

}
}
void loop()
{
 distance = sonar.ping_cm();
  digitalWrite(motorPin1, LOW); 
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW); 
digitalWrite(motorPin4, HIGH);
if (distance <20)
{
 digitalWrite(motorPin1, LOW); 
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW); 
digitalWrite(motorPin4, LOW);

}
}

Measure the distance. Start the car. If the distance is small, stop the car. Hardly seems like an ideal program flow.

Perhaps only changing the status of the car - moving forward or stopped - when the sensor distance changes would be better.

Yes, that is what I planning to do ! Any guide ?

Make a plan of how you want your car to behave. Write some code that you think matches that plan. Test the code to see how closely its behaviour matches your expectations. Rinse and repeat.

Okay!

I think the problem comes when I want to interface with ultrasonic sensor. When the distance is <20 cm, it will stop. So at this point would probably need to stop the sensing because when iut stop, I want the car to reverse backward, so the distance probably will increase. So I think the program will be weird

Thank you

So I think the program will be weird

Well, until you write it and post it here, we won't know, will we?

By many definitions, the code you posted above, as PaulS noted, could be said to be "weird".

int motorPin1 = 7; // pin 2 on L293D IC
int motorPin2 = 8; // pin 7 on L293D IC
int enablePin_1 = 6; // pin 1 on L293D IC
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int state;
int distance;

#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
NewPing sonar(TRIGGER_PIN, ECHO_PIN);
void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
}

void loop()
{
distance = sonar.ping_cm();
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
if (distance <20)
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(1000);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
}

This is the code I tried for moving forward, when distance <20, stop for 1s and then reverse. This program does not seems correct. The wheels always turn backward then stop then turn backward again. I think it is something to do witht he sensor.

Thank you

How do you expect to debug your sketch without prints?

Yes, it would be somehow hard. I will try to add an LCD inside :)

Thanks

Why add more debug effort?
Use Serial.

Actually I made my own arduino. Well, yea, can use serial monitor too :slight_smile:

I would have to solder for the programming port for ftdi first.

Thank you

#include <NewPing.h>
int motorPin1 = 7; // pin 2 on L293D IC
int motorPin2 = 8; // pin 7 on L293D IC
int enablePin_1 = 6; // pin 1 on L293D IC
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.

NewPing sonar(TRIGGER_PIN, ECHO_PIN); 
 
void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
 
void loop() {
 int distance = sonar.ping_cm();
forward();
if( distance < 25){
  STOP();
  delay(1000);
 }
 
else {
  forward();
}

}


// if the state is '0' the DC motor will turn off
void STOP() {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
}
// if the state is '1' the motor will turn right


void forward () {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
}
// if the state is '2' the motor will turn left
void reverse() {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
}

My current coding :slight_smile: It will move forward when power ON and then the distance is less than 25cm, then the car will stop. So how is it possible for me to program it so that it stops for a few seconds, then reverse, then turn right ?

Hope any of you can help.

Thank you and appreciate :slight_smile:

So how is it possible for me to program it so that it stops for a few seconds, then reverse, then turn right ?

// Stop
// delay(aFewSeconds);
// Reverse
// Turn right (forever?)

Add some code after each comment. Since you have functions for each step, this will be trivial.

If the first part of that statement is not true, STOP! Make it so. The rest is then trivial.

Anyway, I use the if statement.

if (distance <25)
{
STOP()
}

If I were to add the reverse in the if statement, I think the distance would increase and it does not sounds logic. So my main problem is this.

Thank you.

PaulS:

So how is it possible for me to program it so that it stops for a few seconds, then reverse, then turn right ?

// Stop
// delay(aFewSeconds);
// Reverse
// Turn right (forever?)

Add some code after each comment. Since you have functions for each step, this will be trivial.

If the first part of that statement is not true, STOP! Make it so. The rest is then trivial.

Hey guys, I uploaded below code to my atmega 328 and control my car using bluetooth. When I initial start the car and play with it using my android phone, it is ok. But maybe some times later, like for example, I let the car mopve forward like for 15 seconds, then I want to stop the car by pressing '0', the car didnt STOP and it cant receive any other command. What is the cause of this kind of problem usually ?

Thank you :)

int motorPin1 = 7; // pin 2 on L293D IC
int motorPin2 = 8; // pin 7 on L293D IC
int enablePin_1 = 6; // pin 1 on L293D IC
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int state;
int flag=0; //makes sure that the serial only prints once the state

void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is '0' the DC motor will turn off
if (state == '0') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: off");
flag=1;
}
}
// if the state is '1' the motor will turn right
else if (state == '1') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
if(flag == 0){
Serial.println("Motor: Forward");
flag=1;
}
}
// if the state is '2' the motor will turn left
else if (state == '2') {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Reverse");
flag=1;
}
}
else if (state == '3') {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Left_1");
flag=1;
}
}
else if (state == '4') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Right_1");
flag=1;
}
}
else if (state == '5') {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Right_2 ");
flag=1;
}
}
else if (state == '6') {
  digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Left_2");
flag=1;
}
}
else if( state=='7')

{
  digitalWrite (13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
}

Please format your code before posting - it's giving me eye-ache.

AWOL:
Please format your code before posting - it’s giving me eye-ache.

Opps, sorry for that. Anyways, do ignore the serial stuff. I just lazy to change them yet :slight_smile:

int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int state;
int flag=0; //makes sure that the serial only prints once the state

void setup() {
// sets the pins as outputs:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is ‘0’ the DC motor will turn off
if (state == ‘0’) {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println(“Motor: off”);
flag=1;
}
}
// if the state is ‘1’ the motor will turn right
else if (state == ‘1’) {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
if(flag == 0){
Serial.println(“Motor: Forward”);
flag=1;
}
}
// if the state is ‘2’ the motor will turn left
else if (state == ‘2’) {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println(“Motor: Reverse”);
flag=1;
}
}
else if (state == ‘3’) {
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
Serial.println(“Motor: Left_1”);
flag=1;
}
}
else if (state == ‘4’) {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println(“Motor: Right_1”);
flag=1;
}
}
else if (state == ‘5’) {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
Serial.println("Motor: Right_2 ");
flag=1;
}
}
else if (state == ‘6’) {
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
Serial.println(“Motor: Left_2”);
flag=1;
}
}

}

Anyways, do ignore the serial stuff.

I’m going to ignore all of it, since it’s improperly posted.

I just lazy to change them yet

Let us know when you get over that. Then, maybe we’ll be willing to help.

int motorPin1 = 7; int motorPin2 = 8; int enablePin_1 = 6; int motorPin4 = 2; int motorPin3 = 4; int enablePin_2 =3 ; int state; int flag=0; //makes sure that the serial only prints once the state

void setup() { // sets the pins as outputs: pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(enablePin_1, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); pinMode(enablePin_2, OUTPUT); // sets enablePin high so that motor can turn on: digitalWrite(enablePin_1, HIGH); digitalWrite(enablePin_2, HIGH); pinMode(13,OUTPUT); // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

void loop() { //if some date is sent, reads it and saves in state if(Serial.available() > 0){ state = Serial.read(); flag=0; } // if the state is '0' the DC motor will turn off if (state == '0') { digitalWrite(motorPin1, LOW); // set pin 2 on L293D low digitalWrite(motorPin2, LOW); // set pin 7 on L293D low digitalWrite(motorPin3, LOW); // set pin 2 on L293D low digitalWrite(motorPin4, LOW); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: off"); flag=1; } } // if the state is '1' the motor will turn right else if (state == '1') { digitalWrite(motorPin1, LOW); // set pin 2 on L293D low digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high digitalWrite(motorPin3, LOW); // set pin 2 on L293D low digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high if(flag == 0){ Serial.println("Motor: Forward"); flag=1; } } // if the state is '2' the motor will turn left else if (state == '2') { digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high digitalWrite(motorPin2, LOW); // set pin 7 on L293D low digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high digitalWrite(motorPin4, LOW); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: Reverse"); flag=1; } } else if (state == '3') { digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high digitalWrite(motorPin2, LOW); // set pin 7 on L293D low digitalWrite(motorPin3, LOW); // set pin 2 on L293D high digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: Left_1");//180 degree rotate flag=1; } } else if (state == '4') { digitalWrite(motorPin1, LOW); // set pin 2 on L293D high digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high digitalWrite(motorPin4, LOW); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: Right_1");//180 degree rotate flag=1; } } else if (state == '5') { digitalWrite(motorPin1, LOW); // set pin 2 on L293D high digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low digitalWrite(motorPin3, LOW); // set pin 2 on L293D high digitalWrite(motorPin4, LOW); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: Right_2 "); //Left wheel forward, Right wheel static flag=1; } } else if (state == '6') { digitalWrite(motorPin1, LOW); // set pin 2 on L293D high digitalWrite(motorPin2, LOW); // set pin 7 on L293D low digitalWrite(motorPin3, LOW); // set pin 2 on L293D high digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low if(flag == 0){ Serial.println("Motor: Left_2"); //Right wheel forward, left wheel static flag=1; } }

}

Modified !

Thank you :)

void loop() {
//if some date is sent, reads it and saves in state
        if(Serial.available() > 0){
          state = Serial.read();
          flag=0;
}

I'm sorry, I don't believe that has been anywhere near the auto-format tool.