Stop the motor using the button

Hi, everybody

I am working on a project related to a motor and a button.

When I send the character "1" to the serial number:

You need to check if the button is pressed. If the button is pressed:

The motors rotate in the opposite direction for 700 milliseconds and must stop.

When I send the character "2" to the serial number:
You need to check if the button is pressed. If not pressed, then:
The motors rotate forward until the button is pressed, and then should stop.

So, I put together a sample code here and stopped. Help, please:

#define PWMA 5

#define AIN1 4
#define AIN2 7

#define STBY 6

int motorSpeed = 255;

void setup() {
  Serial.begin(9600);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(STBY, OUTPUT);

  pinMode(13, INPUT_PULLUP);

  digitalWrite(STBY, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    char data = Serial.read();

    if (data == '1') {
      if (digitalRead(13) == 1) { //if the button is pressed then
        Serial.println("close1");
        digitalWrite(AIN2, HIGH);
        analogWrite(PWMA, motorSpeed);
        delay(700);
        digitalWrite(AIN2, LOW);
      }
      else {
        Serial.println("Nothing to do");
      }
    }


    else if (data == '2') {
      if (digitalRead(13) == 0) { //if the button is not pressed then
        Serial.println("open1");
        digitalWrite(AIN1, HIGH);
        analogWrite(PWMA, motorSpeed);

        if (digitalRead(13) == 1) { // stop the motor when the button is pressed
          Serial.println("motor_stop");
          digitalWrite(AIN1, LOW);
        }
      }
    }

  }
}

1 pin of the button is connected to GND, the second pin is connected to pin 13

Um where's the bug?

1 Like

try this

Hello?

The button function not work for me if there is an error in it?

Did you try me code?

If you want your problem to be solved then reply

You didn't send the code, you just duplicated my code. It didn't work initially

#define PWMA 5

#define AIN1 4
#define AIN2 7

#define STBY 6

int motorSpeed = 255;

void setup() {
  Serial.begin(9600);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(STBY, OUTPUT);

  pinMode(13, INPUT_PULLUP);

  digitalWrite(STBY, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    char data = Serial.read();

    if (data == '1') {
      if (digitalRead(13) == 1) { //if the button is pressed then
        Serial.println("close1");
        digitalWrite(AIN2, HIGH);
        analogWrite(PWMA, motorSpeed);
        delay(700);
        digitalWrite(AIN2, LOW);
      }
      else {
        Serial.println("Nothing to do");
      }
    }


    else if (data == '2') {
      if (digitalRead(13) == 0) { //if the button is not pressed then
        Serial.println("open1");
        digitalWrite(AIN1, HIGH);
        analogWrite(PWMA, motorSpeed);
}

        if (digitalRead(13) == 1) { // stop the motor when the button is pressed
          Serial.println("motor_stop");
          digitalWrite(AIN1, LOW);
      
      }
    }

  }
}

Does this work?

The button doesn't work

So you want that when you type 2 and then press the button the motor should stop

When I send the character "2" to the serial number:
You need to check if the button is pressed. If not pressed, then:
The motors rotate forward until the button is pressed, and then should stop.

Yes or NO?

#define PWMA 5

#define AIN1 4
#define AIN2 7

#define STBY 6

int motorSpeed = 255;

void setup() {
  Serial.begin(9600);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(STBY, OUTPUT);

  pinMode(13, INPUT_PULLDOWN);

  digitalWrite(STBY, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    char data = Serial.read();

    if (data == '1') {
      if (digitalRead(13) == 1) { //if the button is pressed then
        Serial.println("close1");
        digitalWrite(AIN2, HIGH);
        analogWrite(PWMA, motorSpeed);
        delay(700);
        digitalWrite(AIN2, LOW);
      }
      else {
        Serial.println("Nothing to do");
      }
    }


    else if (data == '2') {
      if (digitalRead(13) == 0) { //if the button is not pressed then
        Serial.println("open1");
        digitalWrite(AIN1, HIGH);
        analogWrite(PWMA, motorSpeed);
}

        if (digitalRead(13) == 1) { // stop the motor when the button is pressed
          Serial.println("motor_stop");
          digitalWrite(AIN1, LOW);
      
      }
    }

  }
}

Try this

"1" - does not work at all

"2" - spinning but not stopped

And here the error appears

Problem: When I wrote in Serial "2" The engine immediately stops if you don't even press the button

#define PWMA 5
#define AIN1 4
#define AIN2 7
#define STBY 6

int motorSpeed = 255;

void setup() {
  Serial.begin(9600);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(STBY, OUTPUT);
  pinMode(13, INPUT_PULLUP);
  digitalWrite(STBY, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    char data = Serial.read();

    if (data == '1') {
      if (digitalRead(13) == LOW) { // if the button is pressed
        Serial.println("Button pressed, scrolling back");
        digitalWrite(AIN2, HIGH);
        analogWrite(PWMA, motorSpeed);
        delay(700);
        digitalWrite(AIN2, LOW);
      } else {
        Serial.println("Button not pressed");
      }
    } else if (data == '2') {
      Serial.println("Rotating forward");
      digitalWrite(AIN1, HIGH);
      analogWrite(PWMA, motorSpeed);

      while (digitalRead(13) == HIGH) {
        // Continuously check button state
      }

      Serial.println("Button pressed, motor stop");
      digitalWrite(AIN1, LOW);
    }
  }
}

@TomGeorge @cattledog @Paul_KD7HB
Help please :pray:

Help is possible when you begin your project from the bottom, meaning write code to only test the switches. When that works as your like, the add the motor stuff. Don't do projects from the TOP downward!

I can not confirm this. I can see the "Rotating Forward" holding until the button is pressed. You can see the 7 second hold period before the press.

//Enter '1' with button not pressed
09:26:37.967 -> Button not pressed
//Enter '1' with button pressed
09:27:28.234 -> Button pressed, scrolling back
//Enter '2' with button not pressed
09:27:43.569 -> Rotating forward
//press button
09:27:50.990 -> Button pressed, motor stop

Can you see the "Button not pressed" message when you enter 1 and the button is not pressed? This should verify the wiring of the button so that the with input pullup it is reading high without being pressed.

What Arduino is running this code? It's possible that if the internal led is on pin 13 it may be affecting how the button is working. Try a different pin.