Control of two steppers simultaneously using an arduino and h bridges

I am trying to control two motors simultaneously with both moving in opposite directions. Unfortunately, I am not able to control RPM or step size with this. It runs at the same speed no matter what I input. Can someone help me with this?

#include<Stepper.h>

//definitions for each command to be received through serial port
#define COMMAND_STRETCH 's'
#define COMMAND_UNSTRETCH 'u'

//enter the steps per revolution for the motor here
int stepsInRev = 21600;

//this sets the value for the for loops and therefore sets the amount of steps in each call
int num_of_steps = 1;
//setup pins for each driver motor motor1 = IN1. IN2, IN3, IN4; motor2 = IN1, IN2, IN3, IN4
Stepper myStepper1(stepsInRev, 4, 5, 6, 7);
Stepper myStepper2(stepsInRev, 8, 9, 10, 11);
//variable to store the last call to the serial port
char lastCall = ' ';

// void stretch();
// void unstretch();
// void allStop();


void stretch(int steps)
{
  Serial.println("stretch");
  //motor 1 steps clockwise and motor 2 steps anticlockwise
  myStepper1.step(-1);
  myStepper2.step(1);
  delay(10);
}
void unstretch(int steps){
  Serial.println("unstretch");
  //motor 1 moves anticlockwise and motor 2 steps clockwise
  myStepper1.step(1);
  myStepper2.step(-1);
  delay(10);
}
//to power down the motor drivers and stop the motors
void allStop(){
  Serial.println("stop");
  //steppers stop
  PORTD = B00000000; //sets all pins from 0 to 7 as LOW to power off stepper1
  PORTB = B00000000; //sets all pins from 8 to 13 as LOW to power off stepper2 
}
void setup() {
 //start the bluetooth serial port = send and receive at 9600 baud rate
 //set the speed at 60 rpm;
 myStepper1.setSpeed(60);
 myStepper2.setSpeed(60);
 Serial.begin(9600);
 //initialise the serial port;
}
void loop() {
  // check if there is serial communication and if so read the data
  if(Serial.available()){
    char data = (char)Serial.read();
    // switch to set the char via serial to a command
    switch(data) {
      case COMMAND_STRETCH:
         stretch(num_of_steps);
         break;
      case COMMAND_UNSTRETCH:
         unstretch(num_of_steps);
         break; 
    } 
  //set the lastCall variable to the last call from the serial
  lastCall = data;
  }
else{
char data = lastCall;
switch(data) 
{
      case COMMAND_STRETCH:
         stretch(num_of_steps);
         break;
      case COMMAND_UNSTRETCH:
         unstretch(num_of_steps);
         break;
}
lastCall = data;
}
}

Hello.

What H-bridge, stepper motor, and Arduino are you using? How are you powering your system? How do you have everything connected? Can you post pictures that show your connections?

-Nathan

As you can see in the images above,I’m using a basic Dual H Bridge as the motor driver for both the motors. I’m also using two power supplies for each of the motors and one supply for the Arduino (5V). I’m using a pololu stepper motor #1472 and an Arduino Uno to run the code. My application is stretching and unstretching something mounted on the motor shafts. I wish to run the motors simultaneously in different directions to stretch for certain distance like ( 20mm) and then unstretch by reversing the motor directions to certain distance like 10 mm. With the code I’m using I’m not able to give distances the motor should go and have no control over the RPM either.

Hello,

Generally, we recommend using a current limiting stepper driver with a step/dir interface for controlling stepper motors. It is possible to get basic motion using a pair of H-bridges, but we do not have any specific advice for doing that. You might try to post on the Arduino forum for more help with the library you are using.

-Nathan