Controlling 4 DC motors through V1 motor shield using pushbu

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
struggling
 
Posts: 1
Joined: Tue May 17, 2022 9:06 pm

Controlling 4 DC motors through V1 motor shield using pushbu

Post by struggling »

Hello, I've been scouring the internet for days trying to find a tutorial or solution for this.

I have the V1 motor shield and I've been using it to run 4 DC motor wheels. I have no issue getting the motors to run by themselves (as in, run nonstop without the ability to control them), but I've run into issues when trying to make it so that I can use two pushbuttons (one for forwards and one for backwards) to make the motors run in the respective direction while the button is being held then stop when the button is released. All the code I've found online has been for wireless projects and I don't know whether I'm altering it correctly for my project.

Here is the main code I've been using to try to achieve this:

Code: Select all

#include <AFMotor.h>

//initial motors pin
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR34_64KHZ);
AF_DCMotor motor4(4, MOTOR34_64KHZ);



// constants won't change. They're used here to set pin numbers:
const int forwardButtonPin = A2;  // the number of the pushbutton pin
const int backButtonPin = A3;

// variables will change:
int forwardButtonState = LOW;  // variable for reading the pushbutton status
int forwardCheck = 1;
int backButtonState = LOW;
int backCheck = 1;

void setup() {
  // initialize the driving pins as an output:
  // initialize the pushbutton pin as an input:
  pinMode(forwardButtonPin, INPUT);
  pinMode(backButtonPin, INPUT);
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
}

void loop() {
  // read the state of the pushbutton value:
  forwardButtonState = digitalRead(forwardButtonPin);
  forwardCheck = digitalRead(forwardButtonPin);
  backButtonState = digitalRead(backButtonPin);
  backCheck = digitalRead(backButtonPin);

  if (forwardButtonState == HIGH) {
    if (forwardCheck == 1) {
      forward();
    }
    forwardCheck = 0;
  } else if (backButtonState == HIGH) {
    if (backCheck == 1) {
      back();
    }
    backCheck = 0;
  } else {
    forwardCheck = 1;
    backCheck = 1;
    Stop();
  }
}

void forward() {
  motor1.run(FORWARD);  //rotate the motor clockwise
  motor2.run(FORWARD);  //rotate the motor clockwise
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void back() {
  motor1.run(BACKWARD);  //rotate the motor anti-clockwise
  motor2.run(BACKWARD);  //rotate the motor anti-clockwise
  motor3.run(BACKWARD);  //rotate the motor anti-clockwise
  motor4.run(BACKWARD);  //rotate the motor anti-clockwise
}

void Stop() {
  motor1.run(RELEASE);  //stop the motor when release the button
  motor2.run(RELEASE);  //rotate the motor clockwise
  motor3.run(RELEASE);  //stop the motor when release the button
  motor4.run(RELEASE);  //stop the motor when release the button
}
Sorry if that's too much code! Please let me know if I need to provide any additional info.
Last edited by dastels on Tue May 17, 2022 10:00 pm, edited 1 time in total.
Reason: Clean up formatting to make it more readable

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

Re: Controlling 4 DC motors through V1 motor shield using pu

Post by dastels »

I'm not familiar with that shield or library, but the related code looks reasonable.

You don't say how your buttons are wired, other than being connected to A2 & A3. You also don't say what controller board you're using. But I'll comment based on the usual case.

Your buttons should have their other sides connected to ground and their pins should be set to mode INPUT_PULLUP. That means that will read as HIGH when they are not pushed and LOW when they are. This is still done typically even though the reason was that older MCU (e.g. the ATmega328 used in the Arduino UNO) only had internal pullups.

That will mean that your button logic will check against LOW to detect a pushed button.

I'm not clear on the rational of the State & Check variables for each button. My approach to the loop would be:

Code: Select all

void loop() {
  if (!digitalRead(forwardButtonPin)) {
    forward();
  } else if (!digitalRead(backButtonPin)) {
    back();
  } else {
    Stop();
  }
}
Note that the result of digitalRead is a boolean value so you can use it directly in the if, and not have to compare it with HIGH or LOW.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”