Motor control

I am really new to Arduino and coding, I just need to build linear actuator with a stepper motor. So I got a promising code online:

  • Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino without a library: number of revolutions, speed and direction. More info: https://www.makerguides.com */
    // Define stepper motor connections and steps per revolution:
    #define dirPin 2
    #define stepPin 3
    #define stepsPerRevolution 1600
    void setup() {
    // Declare pins as output:
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    }
    void loop() {
    // Set the spinning direction clockwise:
    digitalWrite(dirPin, HIGH);
    // Spin the stepper motor 1 revolution slowly:
    for (int i = 0; i < stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
    }
    delay(1000);
    // Set the spinning direction counterclockwise:
    digitalWrite(dirPin, LOW);
    // Spin the stepper motor 1 revolution quickly:
    for (int i = 0; i < stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
    }
    delay(1000);
    // Set the spinning direction clockwise:
    digitalWrite(dirPin, HIGH);
    // Spin the stepper motor 5 revolutions fast:
    for (int i = 0; i < 5 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    }
    delay(1000);
    // Set the spinning direction counterclockwise:
    digitalWrite(dirPin, LOW);
    // Spin the stepper motor 5 revolutions fast:
    for (int i = 0; i < 5 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
    }
    delay(1000);
    }

It worked fine to 20 revolutions, but my target is 70. Beyond 20 it stopped working. Please what should I do?

Moved your topic to it's current location as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

have you tried increasing your step per revolution from 1600 to another value to see what happens

It worked fine to 20 revolutions, but my target is 70. Beyond 20 it stopped working.

not sure what you mean that it works fine for 20 revolutions. (perhaps the stepsPerRevolution is not correct for the motor you are using)

looks like your code rotates it one revolution in one direction, then one revolution in the other direction and then attempts to rotate it 5 revolutions and back 5.

during the first revolution, it delays each step by 2 msec (2000 usec). it uses a delay of 1 msec for the 2nd revolution, but then delays by only 0.5 msec.

0.5 msec may be too fast.

"I am really new to Arduino and coding, I just need to build linear actuator with a stepper motor." If that's "all you need", you have no interest in writing your own low level code, why not use an existing stepper library?

21 * 1600 = 33600, too big for an "int" variable (32767), try:

for (long i = 0; i < 70 * stepsPerRevolution; i++) // 112000, a long can hold over 2 billion