Using ardumoto to drive motor with momentary switches for fwd and rev

Using Arduino UNO, generic ardumoto shield, and two momentary tactile buttons.

I want the motor to go forward only while btnA is pressed then stop when released, and the same for btnB except in reverse.

btnA does nothing btnB works as it should, I swapped pin numbers between them and btnA worked while btnB failed.

Then I swapped both pin num's to new one's, and got the same failure. So I concluded it's most likely in the code.

Here is my code, it is a modified ardumoto example I found with google.

/* Ardumoto Example Sketch
  by: Jim Lindblom
  date: November 8, 2013
  license: Public domain. Please use, reuse, and modify this 
  sketch!
  
  This sketch has been modified to use buttons instead of predefined actions
  Also not using motor B
  
  Three useful functions are defined:
    setupArdumoto() -- Setup the Ardumoto Shield pins
    driveArdumoto([motor], [direction], [speed]) -- Drive [motor] 
      (0 for A, 1 for B) in [direction] (0 or 1) at a [speed]
      between 0 and 255. It will spin until told to stop.
    stopArdumoto([motor]) -- Stop driving [motor] (0 or 1).
  
  setupArdumoto() is called in the setup().
  The loop() demonstrates use of the motor driving functions.
*/

// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW  0
#define CCW 1

// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1

// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3;  // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
const byte btnA = 8;   // FWD button
const byte btnB = 9;   // REV button
int FWD = 0;  // Variable to store btnA value
int REV = 0;  // Variable to store btnB value

void setup()
{
  pinMode(btnA, INPUT_PULLUP);
  pinMode(btnB, INPUT_PULLUP);
  setupArdumoto(); // Set all pins as outputs
}

void loop()
{
  FWD = digitalRead(btnA); // Read button A's value
  REV = digitalRead(btnB); // Read button B's value
  if (FWD == LOW)
  {
    driveArdumoto(MOTOR_A, CW, 255); // Set motor A to CCW at max
  } 
  
  else if (REV == LOW)
  {
    driveArdumoto(MOTOR_A, CCW, 255);  // Set motor A to CCW at max
  }

  else
  {
    stopArdumoto(MOTOR_A);  // STOP motor A
  }    

}

// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
  if (motor == MOTOR_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
  }
  else if (motor == MOTOR_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
  }  
}

// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
  driveArdumoto(motor, 0, 0);
}

// setupArdumoto initialize all pins
void setupArdumoto()
{
  // All pins should be setup as outputs:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  
  // Initialize all pins as low:
  digitalWrite(PWMA, LOW);
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
}

any help is appreciated .

Submicro

Did you swap the pin numbers in the code or the wires from the buttons?
Can you post a wiring diagram?

Did you swap the pin numbers in the code or the wires from the buttons?
Can you post a wiring diagram?

I did both

As far a diagram goes I'm not sure how

SubMicro:
As far a diagram goes I'm not sure how

Get a pencil and paper.
Draw a picture of the components. Include enough detail (like pin numbers) that we can determine what you did with the wires.
Take a picture of the paper.
Post the image here following this image guide.

I just added an export jpg from fritzing to my last post I think I did it right?

Also I was wondering if the v-logic jumper had to be set to one or the other? I just left it unused

btnA does nothing btnB works as it should, I swapped pin numbers between them and btnA worked while btnB failed.

I hate those stupid switches. What possessed them to make the stupid things square?

Make sure that you have the switches oriented correctly. so that pressing the switch actually completes the circuit. If you rotate the switch 90 degrees, it will not work.

I also switched wires to check that switch was functioning as it should.

So are you saying that the code should work?

According to serial monitor both button's are working as they should
here's new code

/* Ardumoto Example Sketch
  by: Jim Lindblom
  date: November 8, 2013
  license: Public domain. Please use, reuse, and modify this 
  sketch!
  
  This sketch has been modified to use buttons instead of predefined actions
  Also not using motor B
  
  Three useful functions are defined:
    setupArdumoto() -- Setup the Ardumoto Shield pins
    driveArdumoto([motor], [direction], [speed]) -- Drive [motor] 
      (0 for A, 1 for B) in [direction] (0 or 1) at a [speed]
      between 0 and 255. It will spin until told to stop.
    stopArdumoto([motor]) -- Stop driving [motor] (0 or 1).
  
  setupArdumoto() is called in the setup().
  The loop() demonstrates use of the motor driving functions.
*/

// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW  0
#define CCW 1

// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1

// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3;  // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
const byte btnA = 8;   // FWD button
const byte btnB = 9;   // REV button
int FWD = 0;  // Variable to store btnA value
int REV = 0;  // Variable to store btnB value
// variables will change:


void setup()
{
  Serial.begin(9600);  // Just for debugging
  setupArdumoto(); // Set all pins as outputs
}

void loop()
{
    // This part is for debugging
    // read the state of the pushbutton value:
  FWD = digitalRead(btnA); // Read button A's value
  REV = digitalRead(btnB); // Read button B's value
  Serial.println(FWD);  // print out the state of the button:
  Serial.println(REV);  // print out the state of the button:
  delay(10);   // delay in between reads for stability

  if (FWD == LOW)
  {
    driveArdumoto(MOTOR_A, CW, 255); // Set motor A to CCW at max
  } 
  
  else if (REV == LOW)
  {
    driveArdumoto(MOTOR_A, CCW, 255);  // Set motor A to CCW at max
  }

  else
  {
    stopArdumoto(MOTOR_A);  // STOP motor A
  }    

}

// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
  if (motor == MOTOR_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
  }
  else if (motor == MOTOR_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
  }  
}

// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
  driveArdumoto(motor, 0, 0);
}

// setupArdumoto initialize all pins
void setupArdumoto()
{
  // Button pins setup with internal pullup's
    pinMode(btnA, INPUT_PULLUP);
    pinMode(btnB, INPUT_PULLUP);
  
  // All pins should be setup as outputs:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  
  // Initialize all pins as low:
  digitalWrite(PWMA, LOW);
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
}

Can anyone tell me if there are any obvious errors in my code? I just need a little help in which direction I should be looking.

Ok never mind I solved the problem! hooray!

It seems you cannot just operate one of the motor channels you have too send commands for both regardless if you are using only one. So I just paralleled the motor outputs and send commands to both motor channels and it works. So in the end it was both in the code and hardware!

Thanks for the help everyone
submicro

SOLVED

It seems you cannot just operate one of the motor channels you have too send commands for both regardless if you are using only one. So I just paralleled the motor outputs and send commands to both motor channels and it works. So in the end it was both in the code and hardware!

Thanks for the help everyone
submicro

P.S if anyone is interested here is the working code

/* Ardumoto Example Sketch
  by: Jim Lindblom    
  date: November 8, 2013

  Modified by: Robert Willis
  date: Oct 7 2017
  
  license: Public domain. Please use, reuse, and modify this 
  sketch!
  
  This sketch has been modified to use buttons instead of predefined actions
    
  Three useful functions are defined:
    setupArdumoto() -- Setup the Ardumoto Shield pins
    driveArdumoto([motor], [direction], [speed]) -- Drive [motor] 
      (0 for A, 1 for B) in [direction] (0 or 1) at a [speed]
      between 0 and 255. 
    stopArdumoto([motor]) -- Stop driving [motor] (0 or 1).
  
  setupArdumoto() is called in the setup().
  The loop() demonstrates use of the motor driving functions.
*/

// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW  0
#define CCW 1

// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1

// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3;  // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
const byte btnA = 8;   // FWD button
const byte btnB = 9;   // REV button
int FWD = 0;  // Variable to store btnA value
int REV = 0;  // Variable to store btnB value

void setup()
{
  setupArdumoto(); // Set all pins as outputs
}

void loop()
{
  FWD = digitalRead(btnA); // Read button A's value
  REV = digitalRead(btnB); // Read button B's value
  if (FWD == LOW)
  {
    driveArdumoto(MOTOR_A, CW, 255); // Set motor A to CCW at max
  } 
  
  else if (REV == LOW)
  {
    driveArdumoto(MOTOR_A, CCW, 255);  // Set motor A to CCW at max
  }

  else
  {
    stopArdumoto(MOTOR_A);  // STOP motor A
  }    

}

// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
  if (motor == MOTOR_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
    digitalWrite(DIRB, !dir);
    analogWrite(PWMB, spd);
  }
  else if (motor == MOTOR_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
    digitalWrite(DIRA, !dir);
    analogWrite(PWMA, spd);
  }  
}

// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
  driveArdumoto(motor, 0, 0);
}

// setupArdumoto initialize all pins
void setupArdumoto()
{
  // Buttons set as input and turn on internal pullups
  pinMode(btnA, INPUT_PULLUP);
  pinMode(btnB, INPUT_PULLUP);
  // All pins should be setup as outputs:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  
  // Initialize all pins as low:
  digitalWrite(PWMA, LOW);
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
}