Stepper Control Code Include Brake using arduino Stepper Motor Sheild R3

I am having problems applying the brake in the code on Brake A - Pin 9 Brake B - Pin 8

FUNCTION REQUIRED.
STEEPPER MOTOR 200 STEPS
ARDUNIO MOTOR SHEILD R3 ON A UNO BOARD

PUSH BUTTON NO1.
ROTATE 90 DEGREES RIGHT HOLD/ (BRAKE 2 SECONDS THEN RELEARSE BRAKE) THEN ROTATE LEFT 90 DEGREES

APPLY BRAKE.

PUSH BUTTON NO2.
ROTATE 90 DEGREES LEFT HOLD/ (BRAKE 2 SECONDS THEN RELARSE BRAKE) THEN ROTATE RIGHT 90 DEGREES

APPLY BRAKE

#include <Stepper.h>
const int stepsPerRevolution = 200;
int delaylegnth = 30;

Stepper myStepper(stepsPerRevolution, 8, 9, 12, 13);

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int bt1 = 4;
int bt2 = 5;
boolean bt1g;
boolean bt2g;


void setup () {
  Serial.begin(9600);

  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, HIGH);
  digitalWrite(brakeB, HIGH);

  Serial.begin(9600);
  pinMode(bt1, INPUT_PULLUP);
  pinMode(bt2, INPUT_PULLUP);
  bt1g = false;
  bt2g = false;

  Serial.begin(9600);

  myStepper.setSpeed(80);
}

void loop() {
  int bt1val1 = digitalRead(bt1);
  int bt1val2 = digitalRead(bt2);
  if (!bt1g){
    if (bt1val1 == 0){
      Serial.println(bt1val1);
      for (int i = 0; i < 13; i++){
  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);

  }
  for (int i = 0; i < 13; i++){
          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(8, HIGH);
  digitalWrite(9, HIGH);
  
      bt1g = true;
    }
  }
  else{
    if (bt1val1 == 1){
      Serial.println(bt1val1);
      bt1g = false;
    }
  }
  delay(15);


  
  if (!bt2g){
    if (bt1val2 == 0){
      Serial.println(bt1val2);
      for (int i = 0; i < 13; i++){
          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);
  }

  for (int i = 0; i < 13; i++){
  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(8, HIGH);
  digitalWrite(9, HIGH);

  bt2g = true;
    }
  }
  else{
    if (bt1val2 == 1){
      Serial.println(bt1val2);
      bt2g = false;
    }
  }
}

jamesr44:
I am having problems applying the brake in the code on Brake A - Pin 9 Brake B - Pin 8

That does not make any sense to me - why would you have a brake with a stepper motor? What sort of a brake?

Also, you program defines myStepper but it never uses it.

You have a lot of this sort of code in your program - what is it supposed to do?

  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

The analogWrite() function has no role for stepper motors.

…R

The myStepper function was left in from a previous code that didn’t work so I removed that and also took out the anologWrite functions and it works fine but still need to need the motor to stop between the two 90 degree rotations for three seconds without moving.This is a problem due to the side heavy piece on the motor.

Code without delay

#include <Stepper.h>
const int stepsPerRevolution = 200;
int delaylegnth = 30;

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int bt1 = 4;
int bt2 = 5;
boolean bt1g;
boolean bt2g;


void setup () {
  Serial.begin(9600);

  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(3, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);

  Serial.begin(9600);
  pinMode(bt1, INPUT_PULLUP);
  pinMode(bt2, INPUT_PULLUP);
  bt1g = false;
  bt2g = false;

  Serial.begin(9600);
}

void loop() {
  int bt1val1 = digitalRead(bt1);
  int bt1val2 = digitalRead(bt2);
  if (!bt1g){
    if (bt1val1 == 0){
      Serial.println(bt1val1);
      for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);

  }
  
  for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  }

  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  
      bt1g = true;
    }
  }
  else{
    if (bt1val1 == 1){
      Serial.println(bt1val1);
      bt1g = false;
    }
  }
  delay(15);


  
  if (!bt2g){
    if (bt1val2 == 0){
      Serial.println(bt1val2);
      for (int i = 0; i < 13; i++){
          digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  }
  
  for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);

  }
  
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  bt2g = true;
    }
  }
  else{
    if (bt1val2 == 1){
      Serial.println(bt1val2);
      bt2g = false;
    }
  }
}

Code with 3 second delay

#include <Stepper.h>
const int stepsPerRevolution = 200;
int delaylegnth = 30;

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int bt1 = 4;
int bt2 = 5;
boolean bt1g;
boolean bt2g;


void setup () {
  Serial.begin(9600);

  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(3, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);

  Serial.begin(9600);
  pinMode(bt1, INPUT_PULLUP);
  pinMode(bt2, INPUT_PULLUP);
  bt1g = false;
  bt2g = false;

  Serial.begin(9600);
}

void loop() {
  int bt1val1 = digitalRead(bt1);
  int bt1val2 = digitalRead(bt2);
  if (!bt1g){
    if (bt1val1 == 0){
      Serial.println(bt1val1);
      for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);

  }
  delay(3000);
  
  for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  }

  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  
      bt1g = true;
    }
  }
  else{
    if (bt1val1 == 1){
      Serial.println(bt1val1);
      bt1g = false;
    }
  }
  delay(15);


  
  if (!bt2g){
    if (bt1val2 == 0){
      Serial.println(bt1val2);
      for (int i = 0; i < 13; i++){
          digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  }
  delay(3000);
  
  for (int i = 0; i < 13; i++){
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  
  delay(delaylegnth);

  }
  
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  bt2g = true;
    }
  }
  else{
    if (bt1val2 == 1){
      Serial.println(bt1val2);
      bt2g = false;
    }
  }
}

jamesr44:
but still need to need the motor to stop between the two 90 degree rotations for three seconds without moving.This is a problem due to the side heavy piece on the motor.

You still have not explained what you mean when you refer to a brake?

As long as it is powered a stepper motor will hold its current position unless the load on it is too high. In that case you need a more powerful motor. A stepper motor exerts its maximum torque when it is stationary.

…R
Stepper Motor Basics
Simple Stepper Code

Hi Robin2 , Thanks for the reply The problem that I am having is that the stepper motor does not hold position during the delay and when it is not moving so I am wondering do I need to edit the code so that the motor is powered at all times and if so how would I go about that

jamesr44: so I am wondering do I need to edit the code so that the motor is powered at all times and if so how would I go about that

You have not answered my question in Reply #1 about your code so I don't know what you are trying to do.

My guess is that you are going about controlling a stepper motor in completely the wrong way. Why not try the Stepper library, or better still the the AccelStepper library. Both of them have example code.

...R