Stepper motor FWD and REV using Motor Shield R3

Hi All,

I’ve been playing with an Arduino Uno R3 using the official Motor Shield R3 and am wondering where I’ve gone wrong in my code. For reference I’m using the bounce library and stepper library.

Here’s what I’d like to be able to do.

  1. press Button#1, turn on a led and have a bipolar stepper step forward X # of steps then turn off the led and wait and do nothing until one of the two buttons are pressed.
  2. press Button#2, turn on the same led and have the same bipolar stepper step backward X # of steps then turn off the led and wait and do nothing until one of the two buttons are pressed.

I’ve almost gotten there I think, but the trouble I’m running into is that the stepper and led only move/turn on when the button is held. I’m sure there’s something obvious I’m missing but being new to this I’m just not finding it.

Any help would be most appreciated.

Here’s my code:

//Include the debounce Library
#include <Bounce.h>
// Include the Stepper Library
#include <Stepper.h>
 
const int fwd = 1;     //fwd push button fwd pin 1
const int rev = 2;    //rev push button fwd pin 2
const int led =  7;  //led pin 7

int delaylegnth = 10;
 
Bounce bouncer1 = Bounce(fwd,50);   //setting up the debouncing for the fwd push button
Bounce bouncer2 = Bounce(rev,50);   //setting up the debouncing for the rev push button
 
void setup() {
  pinMode(fwd, INPUT);
  pinMode(rev, INPUT);
  pinMode(led, OUTPUT);
  
  //establish motor direction toggle pins
 pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
 pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
 //establish motor brake pins
 pinMode(9, OUTPUT); //brake (disable) CH A
 pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop() {
  
 bouncer1.update ( );    // Update the debouncer for the fwd push button
 bouncer2.update ( );    // Update the debouncer for the rev push button
  
 int value1 = bouncer1.read();     // Get the update value      
 int value2 = bouncer2.read();     // Get the update value
  
 int reading1 = value1 == HIGH; 
 int reading2 = value2 == HIGH;
 
 if(digitalRead(fwd) && reading1 == HIGH ){
   digitalWrite(led, HIGH);
   digitalWrite(9, LOW); //ENABLE CH A
   digitalWrite(8, HIGH); //DISABLE CH B
   digitalWrite(12, HIGH); //Sets direction of CH A
   analogWrite(3, 255); //Moves CH A
   delay(delaylegnth);
   digitalWrite(9, HIGH); //DISABLE CH A
   digitalWrite(8, LOW); //ENABLE CH B
   digitalWrite(13, LOW); //Sets direction of CH B
   analogWrite(11, 255); //Moves CH B
   delay(delaylegnth);
   digitalWrite(9, LOW); //ENABLE CH A
   digitalWrite(8, HIGH); //DISABLE CH B
   digitalWrite(12, LOW); //Sets direction of CH A
   analogWrite(3, 255); //Moves CH A
   delay(delaylegnth);
   digitalWrite(9, HIGH); //DISABLE CH A
   digitalWrite(8, LOW); //ENABLE CH B
   digitalWrite(13, HIGH); //Sets direction of CH B
   analogWrite(11, 255); //Moves CH B
   delay(delaylegnth);
   }
 digitalWrite(led, LOW);
 
 if(digitalRead(rev) && reading2 == HIGH){
   digitalWrite(led, HIGH);
   digitalWrite(9, LOW);  //ENABLE CH A
   digitalWrite(8, HIGH); //DISABLE CH B
   digitalWrite(12, HIGH);   //Sets direction of CH A
   analogWrite(3, 255);   //Moves CH A
   delay(delaylegnth);
   digitalWrite(9, HIGH);  //DISABLE CH A
   digitalWrite(8, LOW); //ENABLE CH B
   digitalWrite(13, HIGH);   //Sets direction of CH B
   analogWrite(11, 255);   //Moves CH B
   delay(delaylegnth);
   digitalWrite(9, LOW);  //ENABLE CH A
   digitalWrite(8, HIGH); //DISABLE CH B
   digitalWrite(12, LOW);   //Sets direction of CH A
   analogWrite(3, 255);   //Moves CH A
   delay(delaylegnth);
   digitalWrite(9, HIGH);  //DISABLE CH A
   digitalWrite(8, LOW); //ENABLE CH B
   digitalWrite(13, LOW);   //Sets direction of CH B
   analogWrite(11, 255);   //Moves CH B
   delay(delaylegnth);
   }
    digitalWrite(led, LOW);
 }

Pins 0 and 1 are the Serial I/O pins. You should pick a different pin for your 'fwd' button.

If you are using a stepper motor you should probably use the stepper library. Then you can say:

 MyStepper.step(1);  // Take a step forward

and

 MyStepper.step(-1);  // Take a step backward

You can even move X steps forward or backward:

 MyStepper.step(X);
 MyStepper.step(-X);

1 , Take two steps forward , turn to left , face the front, take two steps back,

2 .Take two steps forward , turn to left , face the front, take two steps back,

from your code, it seems you are using the DC motors, but not stepper motor. check it again, the stepper motor has a absolute different usage with the DC motor.

Make a ButtonState for each button. When the button is pressed, set it high. In the loop look at ButtonState, not the button. When stepper action stops, set ButtonState low.