Problems with balance robot

Hello guys! I am a student and I am a noob with Arduino.

I use a Arduino mega 2560 with a L298N Motor Drive Shield and one ultrasonic sensor(HC-SR04)
I just can’t use the ultrasonic sensor to control my motor, I really don’t have any ideas why it doesn’t work
What I want to do is that, I want the motor turn to different sides(both left and right) when it is out of the range(data from ultrasonic)
so that it can balance the robot.

Here’s my code:

const int M1 = 0; // direction motor 1
const int M2 = 2; // direction motor 2

const int M1BRAKE = 1; // pwm motor 1
const int M2BRAKE = 3; // pwm motor 2

#define trigPin 9
#define echoPin 8
long duration,distance;

void setup() {
Serial.begin(9600);
pinMode(0, OUTPUT); // direction motor 1, left front
pinMode(2, OUTPUT); // direction motor 2, left rear

pinMode(1, OUTPUT); // motor 1 BRAKE
pinMode(3, OUTPUT); // motor 2 BRAKE

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int val = analogRead(echoPin);

if(val>=10 && val<=50){
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
digitalWrite( M1BRAKE,LOW);
digitalWrite( M2BRAKE,LOW);
}else if(val>=100){
digitalWrite( M1BRAKE,HIGH);
digitalWrite( M2BRAKE,HIGH);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
}else if (val>=5){
digitalWrite( M1BRAKE,LOW);
digitalWrite( M2BRAKE,LOW);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.print(distance);
Serial.println(“cm”);

//Delay 50ms before next reading.
delay(50);

I am pressing to look for your suggestions, thank you!

In what way doesn't it work ?
What range of values are you getting in the val variable ?
Why aren't you using the distance variable which is so carefully calculated for you ?
Did you miss the end of your code when you copied it ?
Why didn't you put your code in code tags ?
Have you read the stickies at the top of this form giving advice on how to post gere ?

sorry......it is my first post.....

My motor didn't have any action but i have got data from arduino between 2-160

i have got data from arduino between 2-160

What variable is giving you that range of values ?
The only thing that you are printing in the program is the distance variable but you are testing the val variable.

Have you tried the motor commands on their own by hard coding a program to make the motors move ?

UKHeliBob:

i have got data from arduino between 2-160

What variable is giving you that range of values ?
The only thing that you are printing in the program is the distance variable but you are testing the val variable.

Have you tried the motor commands on their own by hard coding a program to make the motors move ?

I got the range of values from my ultrasonic sensor
My motor will move slowly when it is in the range

what I want to do is that
(example)
when val<=10 , both motor will turn forward
when val>=20, both motor will turn backward
they will not move if the val is 11-19

I got the range of values from my ultrasonic sensor

You have still not answered the question. Which variable gives you that range of values ?

Another question.
Does the value of that variable change when you move something in front of the ultrasonic sensor ?

More pointedly.
Does the value of the val variable change sensibly when you move something in front of the ultrasonic sensor ?

const int M1 = 0; // direction motor 1
const int M2 = 2; // direction motor 2

const int M1BRAKE = 1; // pwm motor 1
const int M2BRAKE = 3; // pwm motor 2

#define trigPin 9
#define echoPin 8
long duration,distance;

void setup() {
Serial.begin(9600);
pinMode(0, OUTPUT); // direction motor 1, left front
pinMode(2, OUTPUT); // direction motor 2, left rear

Do you want to use pins 0 and 1 for Serial OR for the motor? Both is the wrong answer.

PaulS:

const int M1 = 0; // direction motor 1

const int M2 = 2; // direction motor 2

const int M1BRAKE = 1; // pwm motor 1
const int M2BRAKE = 3; // pwm motor 2

#define trigPin 9
#define echoPin 8
long duration,distance;

void setup() {
Serial.begin(9600);
pinMode(0, OUTPUT); // direction motor 1, left front
pinMode(2, OUTPUT); // direction motor 2, left rear



Do you want to use pins 0 and 1 for Serial OR for the motor? Both is the wrong answer.

I use 0 and 1 for motor

I use 0 and 1 for motor

Then, get rid of the Serial.begin() statement (and all hope of debugging the code).

UKHeliBob:

I got the range of values from my ultrasonic sensor

You have still not answered the question. Which variable gives you that range of values ?

Another question.
Does the value of that variable change when you move something in front of the ultrasonic sensor ?

More pointedly.
Does the value of the val variable change sensibly when you move something in front of the ultrasonic sensor ?

Sorry...I am a real noob with arduino
Yes, the value change when I move something in front of the ultrasonic sensor

Yes, the value change when I move something in front of the ultrasonic sensor

I am getting fed up with this. The value of WHAT VARIABLE changes when you move something in front of the ultrasonic sensor ?

In the program that you posted you are only printing the value of the distance variable so I must assume that is what you see varying, but you are checking the value of the val variable so all bets are off, particularly with your use of pins 0 and 1 for 2 as PaulS pointed out.