2 stepper motors running indipendantly

hello everyone,

im having a bit of trouble getting 2 stepper motors to run at variable speeds independently

this is the sketch im using at the moment

#define pin1  6//these are the Arduino pins that we use to activate coils 1-4 of the stepper motor
#define pin2  7
#define pin3  8
#define pin4  9
#define pin5  2
#define pin6  3
#define pin7  4
#define pin8  5

#define delaytime 2   //delay time in ms to control the stepper motor delaytime.
                      //Our tests showed that 8 is about the fastest that can yield reliable operation w/o missing steps
                   

void setup() {
  // initialize the 8 pin as an output:
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT); 
  pinMode(pin3, OUTPUT); 
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT); 
  pinMode(pin7, OUTPUT); 
  pinMode(pin8, OUTPUT);

}

void loop(){
  int numberOfSteps = 512;
  step_OFF();         //turning all coils off
  while(numberOfSteps>0){
    forward();        //going forward
    numberOfSteps -- ;//counting down the number of steps
  }
  delay(100);
  
  step_OFF();         //turning all coils off
  numberOfSteps = 512;
  while(numberOfSteps>0){
    backward();       //going backward
    numberOfSteps -- ;//counting down the number of steps
  }
  delay(100);
  
   int numberOfSteps1 = 512;
  step_OFF();         //turning all coils off
  while(numberOfSteps1>0){
    forward1();        //going forward
    numberOfSteps -- ;//counting down the number of steps
  }
  delay(100);
  
  step_OFF();         //turning all coils off
  numberOfSteps = 512;
  while(numberOfSteps1>0){
    backward1();       //going backward
    numberOfSteps -- ;//counting down the number of steps
  }
  delay(100);
}

with is function tab

void Step_A(){
  digitalWrite(pin1, HIGH);//turn on coil 1 
  digitalWrite(pin2, LOW); 
  digitalWrite(pin3, LOW); 
  digitalWrite(pin4, LOW); 
}
void Step_B(){
  digitalWrite(pin1, LOW); 
  digitalWrite(pin2, HIGH);//turn on coil 2
  digitalWrite(pin3, LOW); 
  digitalWrite(pin4, LOW); 
}
void Step_C(){
  digitalWrite(pin1, LOW); 
  digitalWrite(pin2, LOW); 
  digitalWrite(pin3, HIGH); //turn on coil 3
  digitalWrite(pin4, LOW); 
}
void Step_D(){
  digitalWrite(pin1, LOW); 
  digitalWrite(pin2, LOW); 
  digitalWrite(pin3, LOW); 
  digitalWrite(pin4, HIGH); //turn on coil 4
}
void step_OFF(){
  digitalWrite(pin1, LOW); //power all coils down
  digitalWrite(pin2, LOW); 
  digitalWrite(pin3, LOW); 
  digitalWrite(pin4, LOW); 
}
void Step_E(){
  digitalWrite(pin5, HIGH);//turn on coil 1 
  digitalWrite(pin6, LOW); 
  digitalWrite(pin7, LOW); 
  digitalWrite(pin8, LOW); 
}
void Step_F(){
  digitalWrite(pin5, LOW); 
  digitalWrite(pin6, HIGH);//turn on coil 2
  digitalWrite(pin7, LOW); 
  digitalWrite(pin8, LOW); 
}
void Step_G(){
  digitalWrite(pin5, LOW); 
  digitalWrite(pin6, LOW); 
  digitalWrite(pin7, HIGH); //turn on coil 3
  digitalWrite(pin8, LOW); 
}
void Step_H(){
  digitalWrite(pin5, LOW); 
  digitalWrite(pin6, LOW); 
  digitalWrite(pin7, LOW); 
  digitalWrite(pin8, HIGH); //turn on coil 4
}
void step_OFFF(){
  digitalWrite(pin5, LOW); //power all coils down
  digitalWrite(pin6, LOW); 
  digitalWrite(pin7, LOW); 
  digitalWrite(pin8, LOW); 
}


//these functions run the above configurations in forward and reverse order
//the direction of a stepper motor depends on the order in which the coils are turned on.
void forward(){//one tooth forward
  Step_A();
  delay(3);
  Step_B();
  delay(3);
  Step_C();
  delay(3);
  Step_D();
  delay(3);
}

void backward(){//one tooth backward
  Step_A();
  delay(delaytime);
  Step_B();
  delay(delaytime);
  Step_C();
  delay(delaytime);
  Step_D();
  delay(delaytime);
}

void forward1(){//one tooth forward
  Step_E();
  delay(2);
  Step_F();
  delay(2);
  Step_G();
  delay(2);
  Step_H();
  delay(2);
}

void backward1(){//one tooth backward
  Step_E();
  delay(delaytime);
  Step_F();
  delay(delaytime);
  Step_G();
  delay(delaytime);
  Step_H();
  delay(delaytime);
}

i got it from this youtube video and edited it - Tutorial: Stepper Motors Controlled with the Arduino - YouTube

it works great at moving one stepper a full rotation at one speed then another rotation at another speed then moving to the second stepper and doing the same then back to the first but what i need is them both to run constantly independent of each other

thank you for listening and any advice will be greatly appreciated

consider using the Arduino stepper library .

amine2:
consider using the Arduino stepper library .

If you want the two steppers to move at the same time, you'll need to consider the AccelStepper library, instead.

Why not replace your 8 separate step routines with:

void Step_1(int coil){
  digitalWrite(pin1, coil == 1 ? HIGH : LOW);
  digitalWrite(pin2, coil == 2 ? HIGH : LOW);
  digitalWrite(pin3, coil == 3 ? HIGH : LOW);
  digitalWrite(pin4, coil == 4 ? HIGH : LOW);
}

void Step_2(int coil){
  digitalWrite(pin5, coil == 1 ? HIGH : LOW);
  digitalWrite(pin6, coil == 2 ? HIGH : LOW);
  digitalWrite(pin7, coil == 3 ? HIGH : LOW);
  digitalWrite(pin8, coil == 4 ? HIGH : LOW);
}

Try this

loop() {
  unsigned long ms = millis();

  Step_1((ms/250) % 4 + 1); // step 4 times a second
  Step_2((ms/333) % 4 + 1); // step 3 times a second

}

The reason your motors won't run at the same time is your use of while(numberOfSteps>0){ because that makes one motor move through all its steps.

If you want the motors to move at the same time you have to interleave the steps.

And don't use delay() anywhere as it blocks the Arduino from doing other things. Use millis() to manage timing as illustrated in Several Things at a Time

You can also see how millis() is used to manage the timing of a stepper motor in the second example in this Simple Stepper Code. That code won't work directly with your arrangement because you do not appear to be using a specialized stepper driver. However it could be adapted if you write a function that causes a single step.

Your code needs to figure out when the next step is needed by a motor and then call the step() function for that motor. The interval between steps governs the speed.

Your variable naming is appalling. You should use meaningful names for functions, variables and I/O pins. It will make your program much easier to maintain and debug.

...R
Stepper Motor Basics