Accelstepper .run() does not work correctly for me

Hi!
I have a Nema17 stepper motor connected to a Arduino motorshield rev 3. I'm trying to make a automated blind and i want to control the number of steps by writing it in the serial monitor.

I have written a code in the Setup part that sets a "Home position". The thought is that the stepper is supposed to spinn untill the limitswitch is pushed/switched and that sets the current position to 0.

The problem for me comes after that... Because .run() works in the Setup part. But when i try to use it again in the Void Loop() part nothing happens...

The code:

#include <AccelStepper.h>

AccelStepper stepper(2,12,13);

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;

const int limitSwitch = 6;
int limitVal;
long int travelX = 0;

void setup()
{
Serial.begin(9600);
Serial.println("Power on");

pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(limitSwitch, INPUT_PULLUP);

digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);

stepper.setMaxSpeed(200);
stepper.setSpeed(200);
stepper.setAcceleration(200);

Serial.println("Stepper is homing...");
do{
limitVal = digitalRead(limitSwitch);
stepper.moveTo(-2000);
stepper.run();
}while(limitVal == LOW);

do{
limitVal = digitalRead(limitSwitch);
stepper.moveTo(500);
stepper.run();
}while(limitVal == HIGH);

Serial.println("Stepper home");
Serial.println("");

stepper.setCurrentPosition(0);
stepper.setMaxSpeed(200);
stepper.setAcceleration(200);

}

void loop() {

while (Serial.available()){
Serial.println("Enter the number of steps: ");
travelX = Serial.parseInt();
delay(10);
Serial.println(travelX);

stepper.moveTo(travelX);
stepper.run();
}
}

Thanks in advance for any help!!

You seem to have a couple of different things completely confused.

You don't use PWM to control a stepper motor.

If the motorshield is just a h-bridge then you are not creating your Accelstepper instance correctly with

AccelStepper stepper(2,12,13);

You need to specify all 4 pins that control the h-bridge

Have a look at and try the examples on the Accelstepper website.

Please post a link to the datasheet for your stepper motor.

When posting code please use the code button </>
codeButton.png

so your code looks like this

and is easy to copy to a text editor See How to use the Forum

...R
Stepper Motor Basics

The call to stepper.run() is the only time the library can do a step. Right now it is called once for each number sent. Move it to the top of loop(), OUTSIDE the while loop.

Hi guys,
Thanks for the reply! This is my first post on the forum, so sorry for being a total noob :confused: ...

I tried putting .run() outside the While loop and at the top of loop() but it still didn't work.
I'm pretty sure i'm getting a lot wrong here...

The thing that also confuses me is that when i wrote .moveTo(travelX), instead of .run(), it worked for me. The motor moved the given steps, but then moved back to where it started.

Robin2 i looked at your link "Stepper Motor Basics" and read through it, i also looked at your "Simple Stepper Program" to see if i understood the code. But right at the start i got stuck :sweat_smile: I honestly don't understand how to get the pins connected correctly...

On the motorshield it says: pin 3 = PWM A, pin 8 = Brake B, pin 9 = Brake A, pin 11 = PWM B, pin 12 = DIR A, pin 13 = DIR B.

Is something like:

AccelStepper stepper(AccelStepper::FULL4WIRE, 12, 9, 11, 8);

correct?

(I've attached the datasheet for the stepper motor and also a picture of the motor shield)

Thanks!
Regards
P

90776_datasheet.pdf (301 KB)

Petulio:
I honestly don't understand how to get the pins connected correctly...

On the motorshield it says: pin 3 = PWM A, pin 8 = Brake B, pin 9 = Brake A, pin 11 = PWM B, pin 12 = DIR A, pin 13 = DIR B.

Is something like:

AccelStepper stepper(AccelStepper::FULL4WIRE, 12, 9, 11, 8);

correct?

The problem is that your shield is intended for 2 DC motors rather than a single stepper motor - hence the printing on the shield.

Your FULL4WIRE is correct - but I just don't know what are the correct pin references - sorry.

If it was a simple L298 driver (rather than part of a shield) I would expect that the ENx pins need to be ON (not sure if that is HIGH or LOW) and then the current in the motor coils would be controlled by the INx pins.

...R