# Programming with push button to control motor help

I am doing a project using 2 push button to control 2 dc motor and 1 ultrasonic sensor to detect the wall

function:
Press LeftButton and RightButton and robot move forward
Press LeftButton and the robot turn left
Press RightButton and the robot turn right
If ultrasonic sensor infront of robot sense the wall infront of it less than 20cm the robot stop
So if there is wall infront of the robot less than 20cm it should not be moving

problem:
if I press LeftButton the robot turn left and when it sense the wall infront of it less than 20cm instead of stopping but it continue moving
this problem is the same for Turn right and forward

how to solve this problem, what should I change in my program

``````int RightButton = 3;
int LeftButton = 4;
int MotorRight = 5;
int MotorLeft= 6;
int trigPin = 7;
int echoPin = 8;

int RightButtonStatus = 0;
int LeftButtonStatus = 0;

long duration;
int distance;

void setup()
{
pinMode(RightButton ,INPUT_PULLUP);
pinMode(LeftButton ,INPUT_PULLUP);
pinMode(MotorRight ,OUTPUT);
pinMode(MotorLeft,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
int RightButtonStatus = digitalRead(RightButton );
int LeftButtonStatus = digitalRead(LeftButton);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
if ((RightButtonStatus == HIGH) && (LeftButtonStatus == HIGH) && (distance < 20))//Move Forward
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,HIGH);
}
else if ((RightButtonStatus == LOW) && (LeftButtonStatus == HIGH)&& (distance < 20))//Turn Left
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,LOW);
}
else ((RightButtonStatus == HIGH )&& (LeftButtonStatus == LOW)&& (distance < 20)); //Turn Left
{
digitalWrite(MotorRight ,LOW);
digitalWrite(MotorLeft ,HIGH);
}
``````

I don’t like using this type of complex IF statement followed by ELSE

``````if ((RightButtonStatus == HIGH) && (LeftButtonStatus == HIGH) && (distance < 20))
``````

Three tests gives 8 options and only one of them is correct. You have no means to know which of the other 7 gives rise to the ELSE.

I would cascade the tests like this

``````if ((RightButtonStatus == HIGH) {
if (LeftButtonStatus == HIGH) {
if (distance < 20) {
``````

and then you can apply an appropriate ELSE for each condition separately.

I also suspect that your distance test should be completely separate. The buttons start the thing moving and while it is moving there is a regular test for distance which may result in it stopping.

This seems like a good application for a state-machine approach (look it up). The different button combinations would set the movement state - maybe ‘F’ for forward, ‘L’ for left, '‘R’ for right and ‘S’ for stop.

The buttons would set F, L or R and the distance would set the S

The code to operate the motor would just check the value of the state variable.

Separating a program into short single purpose functions makes it much easier to develop and test. Your code in loop() could be as simple as

``````void loop() {
checkDistance();
updateMovementState();
moveMotors();
}
``````

Have a look at Planning and Implementing a Program

…R

I am doing a project using 2 push button to control 2 dc motor and 1 ultrasonic sensor to detect the wall

function:
Press LeftButton and RightButton and robot move forward
Press LeftButton and the robot turn left
Press RightButton and the robot turn right
If ultrasonic sensor infront of robot sense the wall infront of it less than 20cm the robot stop
So if there is wall infront of the robot less than 20cm it should not be moving

problem:
if I press LeftButton the robot turn left and when it sense the wall infront of it less than 20cm instead of stopping but it continue moving
this problem is the same for Turn right and forward

how to solve this problem, what should I change in my program

``````int RightButton = 3;
int LeftButton = 4;
int MotorRight = 5;
int MotorLeft= 6;
int trigPin = 7;
int echoPin = 8;

int RightButtonStatus = 0;
int LeftButtonStatus = 0;

long duration;
int distance;

void setup()
{
pinMode(RightButton ,INPUT_PULLUP);
pinMode(LeftButton ,INPUT_PULLUP);
pinMode(MotorRight ,OUTPUT);
pinMode(MotorLeft,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
int RightButtonStatus = digitalRead(RightButton );
int LeftButtonStatus = digitalRead(LeftButton);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
if ((RightButtonStatus == HIGH) && (LeftButtonStatus == HIGH) && (distance < 20))//Move Forward
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,HIGH);
}
else if ((RightButtonStatus == LOW) && (LeftButtonStatus == HIGH)&& (distance < 20))//Turn Left
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,LOW);
}
else ((RightButtonStatus == HIGH )&& (LeftButtonStatus == LOW)&& (distance < 20)); //Turn Left
{
digitalWrite(MotorRight ,LOW);
digitalWrite(MotorLeft ,HIGH);
}
``````

Press LeftButton and RightButton and robot move forward
Press LeftButton and the robot turn left
Press RightButton and the robot turn right

With INPUT_PULLUP as the pinMode, it looks like the logic related to button status is backwards. How are the buttons actually wired, and what do they read when pressed and not pressed?

If ultrasonic sensor in front of robot sense the wall in front of it less than 20 cm the robot stop
So if there is wall in front of the robot less than 20 cm it should not be moving

Why are all the conditionals causing motor action in your code requiring distance to be < 20? < is less than and > is greater than.

It also appears that you never actually stop the motors if both buttons are not pressed.

You never actually turn the motors off. I’d also suggest separating the distance/stop check from the directional conditionals. Try this (not compiled, not tested):

``````int RightButton = 3;
int LeftButton = 4;
int MotorRight = 5;
int MotorLeft= 6;
int trigPin = 7;
int echoPin = 8;

int RightButtonStatus = 0;
int LeftButtonStatus = 0;

long duration;
int distance;

void setup()
{
pinMode(RightButton ,INPUT_PULLUP);
pinMode(LeftButton ,INPUT_PULLUP);
pinMode(MotorRight ,OUTPUT);
pinMode(MotorLeft,OUTPUT);

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

Serial.begin(9600);
}

void loop()
{
int RightButtonStatus = digitalRead(RightButton );
int LeftButtonStatus = digitalRead(LeftButton);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);

//need to check for > 0 since if nothing is in front of
//sensor distance will be set to 0
if( distance < 20 && distance > 0 )
{
digitalWrite(MotorRight ,LOW);
digitalWrite(MotorLeft ,LOW);

}//if
else
{
if ((RightButtonStatus == HIGH) && (LeftButtonStatus == HIGH) )//Move Forward
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,HIGH);
}
else if ((RightButtonStatus == LOW) && (LeftButtonStatus == HIGH) )//Turn Left
{
digitalWrite(MotorRight ,HIGH);
digitalWrite(MotorLeft ,LOW);

}//elseif
else ((RightButtonStatus == HIGH )&& (LeftButtonStatus == LOW) ) //Turn right
{
digitalWrite(MotorRight ,LOW);
digitalWrite(MotorLeft ,HIGH);

}//elseif
else
{
digitalWrite(MotorRight ,LOW);
digitalWrite(MotorLeft ,LOW);

}//else

}//else

}//loop
``````

Please don’t double post. I replied in your other thread 2 days ago.

…R

I've merged your cross posts @ Alextak.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks for your cooperation.