Arduino for loop iterator randomly turns back 0

  arduino, arduino-ide, c++, for-loop, stepper

I am currently trying to move five Step Motors at the "same time" with an Arduino Mega 2560. Therefore I move every motor one step at a time using a for loop with 800 iterations. Every motor itself moves without problems in both directions. When motor_1 and motor_2 should move both the iterator of the for loop resets back to 0 at a random point, which causes the motor to move infinetly, cause the break condition nerver fullfills. Every other combination works perfectly fine.
This is the function:

   void move_motors() {
     for (long i = 0; i < 800; i++) {
        Serial.println(i);
        motor_1.step(1);
        delay(5);
        motor_2.step(1);
        delay(5);
        motor_3.step(1);
        delay(5);
        motor_4.step(1);
        delay(5);
        motor_5.step(1);
        delay(5);
      }
   }

This is a sample output of Serial.println(i) :

...
176
177
178
179
180
181
182
0
1
2
3
4
5
...

Source: Windows Questions C++

LEAVE A COMMENT