Push button, Arduino, Dc motor

Hello, I'm new to Arduino.
I wanted to make a push button that when I push, the motors work in order for 1 round until end without holding the button. And when I press the button again it starts the motor order process again. I only know basics about this because I just started. I also don't know how to connect push button with Arduino. My push button has 2 legs which seems to connect with wire, so do I need to use bread board and connect it to Arduino?
I've coded the way motor will work in order, I only need to connect the push button and make it run only 1 round when I push it. Please give me advices.

int In1 = 7;
int In2 = 8;
int ENA = 5;
int SPEED = 255;
int In3 = 4;
int In4 = 3;
int ENB = 2;
int In5 = 10;
int In6 = 9;
int ENA2 = 11;
void setup()
{
pinMode(In1,OUTPUT);
pinMode(In2,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(In3,OUTPUT);
pinMode(In4,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(In5,OUTPUT);
pinMode(In6,OUTPUT);
pinMode(ENA2,OUTPUT);
pinMode(stop, OUTPUT);
Serial.begin(9600);
lastB
void loop()
{
  digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
Serial.println("Motor Right");
delay(1000);
  digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
Serial.println("Motor Stop");
delay(2000);
  digitalWrite(In1,LOW);
digitalWrite(In2,HIGH);
Serial.println("Motor Left");
delay(1000);
  digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
Serial.println("Motor Stop");
delay(2000);
analogWrite(ENA,SPEED);

  digitalWrite(In3,HIGH);
digitalWrite(In4,LOW);
Serial.println("Motor Right");
delay(1000);
  digitalWrite(In3,LOW);
digitalWrite(In4,LOW);
Serial.println("Motor Stop");
delay(2000);
  digitalWrite(In3,LOW);
digitalWrite(In4,HIGH);
Serial.println("Motor Left");
delay(1000);
  digitalWrite(In3,LOW);
digitalWrite(In4,LOW);
Serial.println("Motor Stop");
delay(2000);
analogWrite(ENB,SPEED);

 digitalWrite(In5,HIGH);
digitalWrite(In6,LOW);
Serial.println("Motor Right");
delay(1000);
  digitalWrite(In5,LOW);
digitalWrite(In6,LOW);
Serial.println("Motor Stop");
delay(2000);
  digitalWrite(In5,LOW);
digitalWrite(In6,HIGH);
Serial.println("Motor Left");
delay(1000);
  digitalWrite(In5,LOW);
digitalWrite(In6,LOW);
Serial.println("Motor Stop");
delay(2000);
analogWrite(ENA2,SPEED)
}

Hello LEMON

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started.

Keep it simple and stupid firstly.

Follow the example code that comes with the library or
run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

buttons are most commonly wired up with one leg connected to ground and the other leg connected to an arduino pin. Inside setup, you declare the pin as INPUT_PULLUP, which enabled a built-in pullup resistor for that pin. This means that if you are not pressing the button, you will read the pin as HIGH. If the button is pressed, the contacts will touch and the pin will be connected to ground so the pin will read as LOW.

In conjunction with @blh64's advice wire the switch as per S3 in the below drawing.

Arduino TypicalOutputs with warning

You will need to solder wires to the switch
Do you know how to solder?

Well done posting your code as a code-section

Here is a tutorial on how to connect buttons / switches

Your code is badly formatted. There is an automatic function for this
just press ctrl-T.

You are using the function delay(). This is the most easy and at the same time the most troubleful way to write code.

As soon as you want your microcontroller to do something additional. To the

  • motor on/off / wait
  • motor on/off / wait
  • motor on/off / wait
  • motor on/off / wait
    ....
    All the delay()s are in the way and must be replaced by non-blocking timing.

Here is your code well formatted.

int In1 = 7;
int In2 = 8;
int ENA = 5;
int SPEED = 255;
int In3 = 4;
int In4 = 3;
int ENB = 2;
int In5 = 10;
int In6 = 9;
int ENA2 = 11;


void setup() {
  Serial.begin(9600);
  Serial.println("Setup-Start");

  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(In5, OUTPUT);
  pinMode(In6, OUTPUT);
  pinMode(ENA2, OUTPUT);
  pinMode(stop, OUTPUT);
}


void loop()  {
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW);
  Serial.println("Motor Right");
  delay(1000);

  digitalWrite(In1, LOW);
  digitalWrite(In2, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  digitalWrite(In1, LOW);
  digitalWrite(In2, HIGH);
  Serial.println("Motor Left");
  delay(1000);
  
  digitalWrite(In1, LOW);
  digitalWrite(In2, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  analogWrite(ENA, SPEED);
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
  Serial.println("Motor Right");
  delay(1000);

  digitalWrite(In3, LOW);
  digitalWrite(In4, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  digitalWrite(In3, LOW);
  digitalWrite(In4, HIGH);
  Serial.println("Motor Left");
  delay(1000);
  
  digitalWrite(In3, LOW);
  digitalWrite(In4, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  analogWrite(ENB, SPEED);

  digitalWrite(In5, HIGH);
  digitalWrite(In6, LOW);
  Serial.println("Motor Right");
  delay(1000);
  
  digitalWrite(In5, LOW);
  digitalWrite(In6, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  digitalWrite(In5, LOW);
  digitalWrite(In6, HIGH);
  Serial.println("Motor Left");
  delay(1000);
  
  digitalWrite(In5, LOW);
  digitalWrite(In6, LOW);
  Serial.println("Motor Stop");
  delay(2000);
  
  analogWrite(ENA2, SPEED)
}

best regards Stefan

1 Like

Thanks for everyone's effort.
I've done wired the push button with the Arduino.
Now I need help for how to code it. As like I said in first place, I wanted to make a push button that when I push, the motors work in order for 1 round until end without holding the button. And when I press the button again it starts the motor order process again. So what are the functions and codes I need to do to make this work? Please give me some advices.

reading the state of a button is done by the function

digitalRead()

and you have to use the

if ()-statement

The code how to do this is in the link I have already posted.

In my pinion this user-forum is not for delivering ready to use code.
It is for answering specific questions. specific code-questions.

The pattern is:

you

write your own attempt how you think it how it might be coded.

If the code does not yet work you post your code combined with three things

  1. A detailed description what behaviour of the microcontroller you observe
  2. A specific question
  3. your code posted as a code-section

Don't tell me that you can't observe the behaviour.

As an example what could be the minimum what you can write:

"I upload the code. After uploading the code I see a short movement of the servohorn but then nothing else happends. If I press the button nothing happends."

and this description is much much much more precise than writing
"it doesn't work"

YOu can ask hundreds of questions if you like. As long as there can be seen a learning-progress of you all your questions will be answered.

The more you refuse to learn
the more the answers will be unhelpful, short, generalised, or no answer at all.

So look up the demo-codes given in the link
best regards Stefan

1 Like

When the pushbutton actuation is sensed generate a state change that sets a boolean variable. While this boolean is set the code does its thing(s). When the operation ends clear /reset the boolean to wait for the next switch.

A DC motor needs a method to recognize one round, like a limit switch. Maybe you want a stepper motor, which has an exact number of steps per revolution. This link is an example of a stepper motor.

I guess "one round" is one sequence of running down all the digitalWrite()s / delay()s
So a very simple if-condition

if (button is pressed ) {
  // execute sequence
}

would be sufficient.
best regards Stefan

Okay, I've did some coding. is this supposed to make it work?

int In1 = 7;
int In2 = 8;
int ENA = 5;
int SPEED = 255;
int In3 = 4;
int In4 = 6;
int ENB = 2;
int In5 = 10;
int In6 = 9;
int ENA2 = 11;
const int buttonPin = 3;
int buttonState = HIGH;
int lastButtonState = HIGH;
bool buttonPressed = false;
bool codeRunning = false;
int stop = 0;


void setup() {


  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(In5, OUTPUT);
  pinMode(In6, OUTPUT);
  pinMode(ENA2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}


void loop()  {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH) {
    // Button was pressed
    buttonPressed = true;
  }
  if (buttonPressed && buttonState == HIGH) {
    if (!codeRunning) {
      // Start the code sequence
      Serial.println("Button pressed and released. Running code...");
      digitalWrite(In1, HIGH);
      digitalWrite(In2, LOW);
      Serial.println("Motor Right");
      delay(1000);

      digitalWrite(In1, LOW);
      digitalWrite(In2, LOW);
      Serial.println("Motor Stop");
      delay(2000);

      digitalWrite(In1, LOW);
      digitalWrite(In2, HIGH);
      Serial.println("Motor Left");
      delay(1000);

      digitalWrite(In1, LOW);
      digitalWrite(In2, LOW);
      Serial.println("Motor Stop");
      delay(2000);
      analogWrite(ENA, SPEED);

      digitalWrite(In3, HIGH);
      digitalWrite(In4, LOW);
      Serial.println("Motor Right");
      delay(1000);

      digitalWrite(In3, LOW);
      digitalWrite(In4, LOW);
      
     digitalWrite(In3, LOW);
      digitalWrite(In4, HIGH);
      Serial.println("Motor Left");
      delay(1000);

      digitalWrite(In3, LOW);
      digitalWrite(In4, LOW);
      Serial.println("Motor Stop");
      delay(2000);

      analogWrite(ENB, SPEED);

      digitalWrite(In5, HIGH);
      digitalWrite(In6, LOW);
      Serial.println("Motor Right");
      delay(1000);

      digitalWrite(In5, LOW);
      digitalWrite(In6, LOW);
      Serial.println("Motor Stop");
      delay(2000);

      digitalWrite(In5, LOW);
      digitalWrite(In6, HIGH);
      Serial.println("Motor Left");
      delay(1000);

      digitalWrite(In5, LOW);
      digitalWrite(In6, LOW);
      Serial.println("Motor Stop");
      delay(2000);

      analogWrite(ENA2, SPEED);
      codeRunning = true;
    } else {
      // End the code sequence
      Serial.println("Code sequence ended.");
      digitalWrite(In1, LOW);
      digitalWrite(In2, LOW);
     digitalWrite(ENA, stop);
     
      digitalWrite(In4, LOW);
      digitalWrite(In5, LOW);
      digitalWrite(ENB, stop);
      
      digitalWrite(In5, LOW);
      digitalWrite(In6, LOW);

      analogWrite(ENA2, stop);

      codeRunning = false;
      buttonPressed = false; // Reset the flag
    }
  }

}

You should do a test by uploading the code. And then report the result.
What did you observe?

It seems like it's not working, I pressed the button but nothing happened. I tried checking wires, motors, nothing went wrong. I tried it with normal code with no button function and it works. I don't know what's wrong with it, maybe my codes are wrong or maybe my button wiring are incorrect.
I connect each leg of my momentary button with wire and connect it with bread board, then using jumpers wire to connect 1 leg to gnd and another leg to pin 3. I still don't understand what's wrong.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Insert Serial.println()´s at points of interrest and analyze the test results.

Hallo

I have resolved the knotted start condition.

Just test and play:

int In1 = 7;
int In2 = 8;
int ENA = 5;
int SPEED = 255;
int In3 = 4;
int In4 = 6;
int ENB = 2;
int In5 = 10;
int In6 = 9;
int ENA2 = 11;
const int buttonPin = 3;
int buttonState = HIGH;
int lastButtonState = HIGH;
bool buttonPressed = false;
bool codeRunning = false;
int stop = 0;
void setup()
{
  pinMode(In1, OUTPUT);
  pinMode(In2, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(In5, OUTPUT);
  pinMode(In6, OUTPUT);
  pinMode(ENA2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    // Start the code sequence
    Serial.println("Button pressed and released. Running code...");
    digitalWrite(In1, HIGH);
    digitalWrite(In2, LOW);
    Serial.println("Motor Right");
    delay(1000);
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    Serial.println("Motor Stop");
    delay(2000);
    digitalWrite(In1, LOW);
    digitalWrite(In2, HIGH);
    Serial.println("Motor Left");
    delay(1000);
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    Serial.println("Motor Stop");
    delay(2000);
    analogWrite(ENA, SPEED);
    digitalWrite(In3, HIGH);
    digitalWrite(In4, LOW);
    Serial.println("Motor Right");
    delay(1000);
    digitalWrite(In3, LOW);
    digitalWrite(In4, LOW);
    digitalWrite(In3, LOW);
    digitalWrite(In4, HIGH);
    Serial.println("Motor Left");
    delay(1000);
    digitalWrite(In3, LOW);
    digitalWrite(In4, LOW);
    Serial.println("Motor Stop");
    delay(2000);
    analogWrite(ENB, SPEED);
    digitalWrite(In5, HIGH);
    digitalWrite(In6, LOW);
    Serial.println("Motor Right");
    delay(1000);
    digitalWrite(In5, LOW);
    digitalWrite(In6, LOW);
    Serial.println("Motor Stop");
    delay(2000);
    digitalWrite(In5, LOW);
    digitalWrite(In6, HIGH);
    Serial.println("Motor Left");
    delay(1000);
    digitalWrite(In5, LOW);
    digitalWrite(In6, LOW);
    Serial.println("Motor Stop");
    delay(2000);
    analogWrite(ENA2, SPEED);
    codeRunning = true;
  }
  else
  {
    // End the code sequence
    Serial.println("Code sequence ended.");
    digitalWrite(In1, LOW);
    digitalWrite(In2, LOW);
    digitalWrite(ENA, stop);
    digitalWrite(In4, LOW);
    digitalWrite(In5, LOW);
    digitalWrite(ENB, stop);
    digitalWrite(In5, LOW);
    digitalWrite(In6, LOW);
    analogWrite(ENA2, stop);
    codeRunning = false;
    buttonPressed = false; // Reset the flag
  }
}

Have a nice day and enjoy coding in C++.

1 Like

I've tested, it actually works, I really appreciate your efforts, but I've got only 1 issue left, it's keeps running and it never ends. What I really want is, when I pressed the button, it will run a set of code only 1 set. After done, it keeps turned off rest of the time until pressing the button again to do another 1 set of codes. The circuit is usually off, only turn on to do only 1 set of code when pressed button. I hope you would help.

Post a schematic to see how the push button is connected.

If your button is wired correctly your initial code already works!

I added initialisation of the serial interface to your code and added letters and motor-numbers to the serial printing. Through the letters it is unique where your code is executing in the moment of the printing.

I left anything else as you coded it and it works.
You can test in in the WOKWI-simulator

If your code repeats one of this two reasons are likely cause the repeated execution

  1. You did not wire your button between ground and IO-pin 3
  2. you changed something in your code since you posted your code in post # 14

To find out what is really happening you should add serial printing to your code

Here is your code with:

  1. simplified button-checking
  2. non-blocking timing and but slowed down serial printing

.

best regards Stefan