Robot Car

Hi
I was trying to build arduino robot car. I have small problem wich I cant solve. Here is a list of components :
1- Arduino board with sensor sheild v5.0.
2- Two 18650 Batteries.
3- Keyes l298 rev02 for motors driving.
4- Ultrasonic Sensor.
5- Four buttons in the edges of the robot to prevent it from stuck if hitting walls.

The right two tires are connected together and the left two tires are connected together.
I tried to run the l298 alone in separate project and it works very well. It move forward, backward, left and right with no problem.
But when I tried to program the robot with the components above the IN1 of l298 keep HIGH even after many attempts to turn it LOW. The problem is only in IN1.
I will put the sketch wich I am doing it.

/*-----------UltraSonic Sensor-----------*/
#define trigPin1 7
#define echoPin1 8
long duration, distance, FrontSensor;
/*-----------Motors-----------*/
int in1 = 1;
int in2 = 2;
const int ena = 5;
int in3 = 3;
int in4 = 4;
const int enb = 6;
/*-----------Direction-----------*/
int dir;
/*-----------Buttons-----------*/
const int buttonPin1 = 9;
int buttonState1 = 0;
const int buttonPin2 = 10;
int buttonState2 = 0;
const int buttonPin3 = 11;
int buttonState3 = 0;
const int buttonPin4 = 12;
int buttonState4 = 0;

void setup() {
/*-----------Motors-----------*/
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
/*-----------UltraSonic Sensor-----------*/
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
/*-----------Direction-----------*/
  randomSeed(2);
  Serial.begin(9600);
/*-----------Buttons-----------*/
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}

void loop() {
    buttonState1 = digitalRead(buttonPin1);
    Serial.print(buttonState1);
    Serial.write("  ,  ");
    buttonState2 = digitalRead(buttonPin2);
    Serial.print(buttonState2);
    Serial.write("  ,  ");
    buttonState3 = digitalRead(buttonPin3);
    Serial.print(buttonState3);
    Serial.write("  ,  ");
    buttonState4 = digitalRead(buttonPin4);
    Serial.print(buttonState4);
    Serial.write("  ,  ");
    if(buttonState1 == 1 || buttonState2 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    delay(200);
    }
    if(buttonState3 == 1 || buttonState4 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(200);
    }  
   
  SonarSensor(trigPin1, echoPin1);
  FrontSensor = distance;
  if (FrontSensor <= 15){
    dir = random(1,3);
    Serial.print(dir);
    if (dir == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    delay(700);

    if(buttonState1 == 1 || buttonState2 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    delay(200);
    }
    if(buttonState3 == 1 || buttonState4 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(200);
    }  
  }
    if (dir == 2){
    analogWrite(ena, 220);
    analogWrite(enb, 220);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(700);

    if(buttonState1 == 1 || buttonState2 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    delay(200);
    }
    if(buttonState3 == 1 || buttonState4 == 1){
    analogWrite(ena, 160);
    analogWrite(enb, 160);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    delay(200);
    }    
   }
  }
  else{
    analogWrite(ena, 150);
    analogWrite(enb, 150);
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
  }

}

void SonarSensor(int trigPin,int echoPin){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}

Pins 0 and 1 are for serial. Do not use them.

Thank you it worked now :roll_eyes: :smiley: .

You've posted this question twice, both times hijacking threads which have nothing to do with what you are talking about. I suggest you press "Report to moderator" on both posts and ask the moderator to find you somewhere sensible to ask your question.

And while you're at it please read "How to use this forum" at the top of any of the forums and that might help asking a better question. For a start if you want help with code we will need to see the code.

Steve

hi alecks_teo.

I made two programs, one controlling just the servos and another one controlling just the platform.
And now i don't know how I can read all this data and not to mix it up.

First, please make a new topic about your problem.
from the quote, you should check for the sliders and string data first.

Hey I am making a 4 WD robot smart car and started to learn how to program a more advance Arduino project.I have a couple of issues, but I hope you can help me with this and thank you so much But the problems lies LCD, Servo and motor(Left and Right), so do I have to put all these codes down under "void setup" or before "void setup"?

Bertha.txt (12 KB)

Bertha_2_0.ino (5.03 KB)

Your code layout is horrid.

The type and function name go on the SAME line, not two different lines.

NOTHING follows the { at the start of a function.

NOTHING goes on the same line as the case statement.

Use Tools + Auto Format before posting code again.

 case '8':advance();
UP 
break;

What do you expect UP to mean/do?

You have not declared an instance, named lcd, of any class. You can not use methods of a non-existent instance.