Struggling with implementation of for loops using AccelStepper library alongside NEMA17 12v dc motor

Hi there,

just trying to implement a for loop to rotate a stepper motor using AccelStepper.h library. The very simple code we've written up is not working.

we are also using an l298N module with the Arduino to control and output the power supply. However, the wiring is working fine for the example code we've pulled from the internet, so its not a wiring issue.

OUR CODE

#include <AccelStepper.h>

#define DEG_PER_STEP 1.8 // could possibly change to increase speed of the motor 
#define STEP_PER_REVOLUTION (360 / DEG_PER_STEP)

AccelStepper stepper(AccelStepper:: FULL4WIRE, 7, 6, 5, 4);

int stretch1Rotations = 11;
int stretch3Rotations = 33;
int stretch5Rotations = 55;

long moveToPosition = STEP_PER_REVOLUTION;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  stepper.setAcceleration(200.0); // set acceleration
  stepper.setSpeed(200);          // set initial speed
  stepper.setCurrentPosition(0);  // set position to 0

  stepper.moveTo(STEP_PER_REVOLUTION); // move motor one revolution, in clockwise direction
  Serial.println("Motor moving in clockwise direction");
}

void loop() {
  for (int i = 0; i <= stretch1Rotations; i++) {
    Serial.print("Rotation no.: ");
    Serial.print(i);
    stepper.moveTo(moveToPosition);
    stepper.setCurrentPosition(0);
    
  }
  Serial.print("rotation complete");
  stepper.run();
}

EXAMPLE CODE USED AS REFERENCE

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-stepper-motor-using-l298n-driver
 */

#include <AccelStepper.h>

#define DEG_PER_STEP 1.8
#define STEP_PER_REVOLUTION (360 / DEG_PER_STEP)

AccelStepper stepper(AccelStepper::FULL4WIRE, 7, 6, 5, 4);

long moveToPosition = STEP_PER_REVOLUTION;

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

  stepper.setAcceleration(200.0); // set acceleration
  stepper.setSpeed(200);          // set initial speed
  stepper.setCurrentPosition(0);  // set position to 0

  stepper.moveTo(STEP_PER_REVOLUTION); // move motor one revolution, in clockwise direction
  Serial.println("Motor moving in clockwise direction");
}

void loop() {
  if (stepper.distanceToGo() == 0) {
    Serial.println("Motor is stopped");
    delay(5000); // stop 5 seconds
    stepper.setCurrentPosition(0); // reset position to 0
    moveToPosition = -1 * moveToPosition; // reverse direction

    stepper.moveTo(moveToPosition); // move motor one revolution

    if (stepper.distanceToGo() > 0)
      Serial.println("Motor moving in clockwise direction");
    else if (stepper.distanceToGo() < 0)
      Serial.println("Motor moving in anticlockwise direction");
  }

  // Serial.print(F("position: "));
  // Serial.println(stepper.currentPosition());

  stepper.run(); // MUST be called as frequently as possible
}

pls help me why is it not working

Please look at that comment - it is important. If you don't call run() really frequently, your stepper will not move. Within your for loop you never call run(), so the motor will not move.

And moveTo() is not a blocking function. Your for loop will be executed very fast without effectively doing anything. You could use runToNewPosition() instead of moveTo(). This call blocks until the new position is reached.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.