 }
 
 // full stepping 4 steps :
-void do4Steps(int cnt, boolean forwards){
-  //for (int i = 0; i < cnt; i++)
-    duty = 50;
- 
-    if (forwards)
-      {//for (int j = 1; j <= 4; j++){doStep(j);}
-         int j=1;
-         int i=1;
-          while (j<=4 && i<=cnt) {  
-          doStep(j);
-          i++;
-           j++;}
-      }
-    else
-      {for (int j = 4; j >= 1; j--){doStep(j);}}
-
-  //}
+void do4Steps(int cnt, boolean forwards)
+{
+  duty = 50;
+  unsigned count = 0;
+  const int list[] = {1,2,3,4};
+  if(forwards)
+  {
+    while(count<cnt)
+      doStep(list[count++ %4]);
+  }
+  else
+  {
+    int index = 3;
+    while(count<cnt)
+    {
+      doStep(list[index--]);
+      if(index<0)
+        index=3;
+      ++count;
+    }
+  }
 }
 
 // half stepping 8 steps :
 void do8Steps(int cnt, boolean forwards){
-  const int list[] = {1,11,2,12,3,13,4,14};
-  for (int i = 0; i < cnt; i++){
-    duty = 50;
-    if (forwards)
-      {for (int j = 0; j <= 7; j++){doStep(list[j]);}}
-    else
-      {for (int j = 7; j >= 0; j--){doStep(list[j]);}}
+  //const int list[] = {1,11,2,12,3,13,4,14};
+  //for (int i = 0; i < cnt; i++){
+  //duty = 50;
+  //if (forwards)
+  //{for (int j = 0; j <= 7; j++)
+  // {doStep(list[j]);}}
+
+
+  //else
+  // {for (int j = 7; j >= 0; j--){doStep(list[j]);}}
+  //
+  unsigned count = 0;
+  const int list[] = {
+    1,11,2,12,3,13,4,14  };
+  if(forwards){
+    while(count<cnt)
+      doStep(list[count++ %8]);
+  }
+  else{
+    int index = 7;
+    while(count<cnt){
+      doStep(list[index--]);
+      if(index<0)
+        index=7;
+      ++count;
+    }
   }
 }
 

  pinMode(B, OUTPUT);     
  pinMode(C, OUTPUT);     
  pinMode(D, OUTPUT);     

  //pinMode(trigger, OUTPUT);     
  
}

void one(){


// 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);    
  }
}

// disable motor
void motorOff(){
  /* Important note:
   Turning off the motor will make it go into a 'rest' state. 
   When using microsteps (or even full steps), this may not be the last active step. 
   So using this routine may change the position of the motor a bit.
   */

  digitalWrite(A, LOW);   
  digitalWrite(B, LOW);   
  digitalWrite(C, LOW);   
