Micostepping multi steppermotor using arduino

hello everyone I am using "Arduino UNO r3" and 3 "L298 driver". I want to use microstepping for 3 stepper motors, I have a code for microstepping 1 stepper motor but I can't write the code for multitasking Arduino so that each stepper motor rotate with different speed using microstepps(96steps or higher). here is my code for 1 stepper motor(port 8,9,10,11 Arduino):

int A = 11;
int B = 10;
int C = 9;
int D = 8;
long duty = 50;
int waitMicroSeconds = 500;
int pulseCount = 5;

void setup() {                

}

void one(){
  digitalWrite(A, HIGH);   
  digitalWrite(B, LOW);   
  digitalWrite(C, HIGH);   
  digitalWrite(D, LOW);   
}

void two(){
  digitalWrite(A, HIGH);   
  digitalWrite(B, LOW);   
  digitalWrite(C, LOW);   
  digitalWrite(D, HIGH);   
}

void three(){
  digitalWrite(A, LOW);   
  digitalWrite(B, HIGH);   
  digitalWrite(C, LOW);   
  digitalWrite(D, HIGH);   
}

void four(){
  digitalWrite(A, LOW);   
  digitalWrite(B, HIGH);   
  digitalWrite(C, HIGH);   
  digitalWrite(D, LOW);   
}


void oneB(){
  digitalWrite(A, HIGH);   
  digitalWrite(B, LOW);   
  digitalWrite(C, LOW);   
  digitalWrite(D, LOW);   
}

void twoB(){
  digitalWrite(A, LOW);   
  digitalWrite(B, LOW);   
  digitalWrite(C, LOW);   
  digitalWrite(D, HIGH);   
}

void threeB(){
  digitalWrite(A, LOW);   
  digitalWrite(B, HIGH);   
  digitalWrite(C, LOW);   
  digitalWrite(D, LOW);   
}

void fourB(){
  digitalWrite(A, LOW);   
  digitalWrite(B, LOW);   
  digitalWrite(C, HIGH);   
  digitalWrite(D, LOW);   
}


// main routine to microstep
void doStep(int st){
  
  long dt1 = waitMicroSeconds * duty / 100;
  long dt2 = waitMicroSeconds * (100-duty) / 100;

  for (int j = 0; j < pulseCount; j++){
    switch (st){
    case 1: one();break;
    case 2: two();break;
    case 3: three();break;
    case 4: four();break;
    case 11: oneB();break;
    case 12: twoB();break;
    case 13: threeB();break;
    case 14: fourB();break;

    case 21: one();break;
    case 22: two();break;
    case 23: three();break;
    case 24: four();break;
    case 31: oneB();break;
    case 32: twoB();break;
    case 33: threeB();break;
    case 34: fourB();break;

    }

    delayMicroseconds(dt1);

    switch (st){
    case 1: one();break;
    case 2: two();break;
    case 3: three();break;
    case 4: four();break;
    case 11: oneB();break;
    case 12: twoB();break;
    case 13: threeB();break;
    case 14: fourB();break;

    case 21: oneB();break;
    case 22: twoB();break;
    case 23: threeB();break;
    case 24: fourB();break;
    case 31: two();break;
    case 32: three();break;
    case 33: four();break;
    case 34: one();break;
    }
    delayMicroseconds(dt2);
    
  }
}

void doMoreSteps(int cnt, boolean forwards){
  const int list1[] = {1,11,2,12,3,13,4,14};
  const int list2[] = {21,31,22,32,23,33,24,34};
  
  for (int i = 0; i < cnt; i++){

    duty = 50;
    if (forwards)
      {for (int j = 7; j >= 0; j--){doSteps(list2[j], forwards); doStep(list1[j]);}}
    else
      {for (int j = 1; j <= 7; j++){doSteps(list2[j], forwards); doStep(list1[j]);}}
     
  }
}

void doSteps(int st, boolean forwards){
     
// ********* 96 steps 
  if (forwards){
    for (int i = 9; i >= 1; i--){duty = 10 * i; doStep(st);}
  }
  else{
    for (int i = 1; i <= 9; i++){duty = 10 * i; doStep(st);}
  }

}

// the loop routine runs over and over again forever:
void loop() {

  waitMicroSeconds = 65000; 
  pulseCount = 20;

 //waitMicroseconds and pulseCount to adapt speed when changing modes

  doMoreSteps(24, true);
}

looking at your code, IMHO there is no straight solution to achieve that unfortunately.

I suggest you look into creating a FSM flow and use millis()/micros() to allow you multitask

hope that helps...

The L298 is an ancient driver, that doesn't allow microstepping ( at least not without additional hardware ).

Please show a schematic of your project and a link to your stepper motor.

To drive mutliple steppers you need an completely other approach as @sherzaad already stated. Or you use a suitable library for your steppers.

the posted code is very redundant

consider

#define MyHW
#ifdef MyHW
const byte pins0 [] = { 10, 11, 12, 13 };
#else
const byte pins0 [] = { 8, 9, 10, 11 };
#endif


#define N_PINS  sizeof(pins0)

const byte seq [][N_PINS] =  {
     { HIGH, LOW,  LOW,  LOW  },
     { HIGH, LOW,  HIGH, LOW  },

     { LOW,  LOW,  HIGH, LOW  },
     { LOW,  HIGH, HIGH, LOW  },

     { LOW,  HIGH, LOW,  LOW  },
     { LOW,  HIGH, LOW,  HIGH },

     { LOW,  LOW,  LOW,  HIGH },
     { HIGH, LOW,  LOW,  HIGH },
};
#define N_SEQ   (sizeof(seq)/N_PINS)

int  idx;

unsigned long msecLst;
unsigned long Pause = 200;

char s [80];

// -----------------------------------------------------------------------------
void
step (
    const byte *pins,
    int        &idx,
    int         dir )
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (pins [n], seq [idx][n]);
    }

    idx += dir;
    if ((int) N_SEQ <= idx)
        idx = 0;
    if (0 > idx)
        idx = N_SEQ - 1;
}

// -----------------------------------------------------------------------------
void loop() {
    unsigned long msec = millis ();

    if (msec > msecLst)  {
        msecLst += Pause;
        step (pins0, idx, -1);
    }
}

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

    for (unsigned n = 0; n < N_PINS; n++)
        pinMode (pins0 [n], OUTPUT);
}

however, the above needs a method to determine the next interval using fractions if you want more precise timing.

the other aspect of the above is different sets of pins for different motors can be passed to step, along with a separate idx.

Hi @akhter_sion
The best you can get with the L298 is HALF/FULL STEP MODES but for that you need to use the L297 as well.

RV mineirin

It would be hard to find a worse driver for steppers than the ancient L298. Not only will the L298 not allow microstepping it will waste a lot of energy as heat.

Post a data sheet for the stepper motor and we can help you find a proper driver that will allow microstepping and efficiently drive your motor.

I imagine that you might be able to do better than that by doing your own PWM, but I don't see that happening in the OP's code.
I agree with the others - if you want microstepping, you should look into a better motor control chip. Many of the newer chips have microstepping built in, are smaller and easier to use, require fewer pin, are significantly more efficient, AND are cheaper as well. (Hmm. Maybe not, once you take "clones" into play.)

It would be difficult to convince me not to use one of those 0.6x0.8inch "modules" that have become common for use in 3D printers, if I needed to control a stepper or several steppers. They're available in quite a variety of specs and "authenticity."

Hard to help when there are (literally) thousands of different stepper motors with different voltage, current and number of steps per revolution, and you have "3 stepper motors". Could you provide a few details?