Stepper Motor

Thanks in advance for any assistance.
I have managed to get a stepper motor to rotate backwards and forwards using 2 respective push buttons and it stops in the same position each time, but the trouble is when it powers up (or push reset button) it always rotates about half the normal amount clockwise.
Can anyone assist me how to prevent this 1st unintended movement. Here is my code.

#include <AccelStepper.h>

int cwButton;
int ccwButton;
AccelStepper stepper(1, 11, 10); 
int pos = 1260; //steps (amount of movement)
int cwButtonState = 0;
int ccwButtonState = 0;

void setup() 
{
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(10, OUTPUT); // 10 is direction
  pinMode(11, OUTPUT); //11 is stepping
  stepper.setMaxSpeed(30000);
  stepper.setAcceleration(3000);
}
  
   void loop() 
   {
    cwButton = digitalRead(4);
    ccwButton = digitalRead(5);
    
    if (cwButton == LOW && cwButtonState == 0)
    {
  stepper.runToNewPosition(pos);
  cwButtonState=1;
    }
    
    if (ccwButton == LOW && ccwButtonState == 0)
    {
      stepper.runToNewPosition(-pos);
      ccwButtonState=1;
    }
    
    if (ccwButton == HIGH && ccwButtonState == 1)
    {
      ccwButtonState=0;
    }
    
    if (cwButton == HIGH && cwButtonState == 1)
    {
      cwButtonState=0;
    }
      delay(50);
    }

t always rotates about half the normal amount clockwise.

Why? What condition in your program is true to make that happen? Add Serial.begin() and some Serial.print() and/or Serial.println() statements to find out.

What is between the stepper motor and the Arduino?

At start up the motor is deemed to be at position 0, the first thing you do
is runToPosition (pos), where pos is initialized to 1260.

Initialize pos to zero.

New code including the serial as suggested by Paul S
and the serial monitor prints

"stepper runToNewPosition+Pos"

#include <AccelStepper.h>

int cwButton;
int ccwButton;
AccelStepper stepper(1, 11, 10); 
int pos = 1260; //steps (amount of movement)
int cwButtonState = 0;
int ccwButtonState = 0;

void setup() 
{
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(10, OUTPUT); // 10 is direction
  pinMode(11, OUTPUT); //11 is stepping
  stepper.setMaxSpeed(30000);
  stepper.setAcceleration(3000);
  Serial.begin(9600);
}
  
   void loop() 
   {
    cwButton = digitalRead(4);
    ccwButton = digitalRead(5);
    
    if (cwButton == LOW && cwButtonState == 0)
    {
  stepper.runToNewPosition(pos);
  Serial.println("stepper runToNewPosition+Pos");
  cwButtonState=1;
    }
    
    if (ccwButton == LOW && ccwButtonState == 0)
    {
      stepper.runToNewPosition(-pos);
      Serial.println("stepper runToNewPosition-Pos");
      ccwButtonState=1;
    }
    
    if (ccwButton == HIGH && ccwButtonState == 1)
    {
      ccwButtonState=0;
    }
    
    if (cwButton == HIGH && cwButtonState == 1)
    {
      cwButtonState=0;
    }
      delay(50);
    }

@ Paul S
I have a microstep driver driving the stepper motor i haven't powered that down only the arduino.
The 1st cycle after pushing reset or powering up is roughly only 1/2 of the normal rotation.

@ Mark T Initialize pos to zero how / where do I do that please. If I change the 1260 then nothing happens even when I push the cw or ccw buttons

Serial.println("stepper runToNewPosition+Pos");

This won't work as you expect. I needs to be two lines

Serial.print("stepper runToNewPosition ");
Serial.println(pos);

Initialize pos to zero how / where do I do that please.

You need to think carefully about what your code does. The stepper motor does not remember where it was last time it was used. Every time the Arduino starts the position the motor is in is considered to be 0. I think there is an AccelStepper method that allows you to tell the library to assume the current position is something else. Have a look at the documentation.

...R

OK found the main fault. One of the push buttons I was using, was NC not NO so the reason it was starting was the pin was pulled LOW all the time.
The problem I have is the 1st cycle in either direction is roughly half the desired amount, can anyone assist here?
Another option is just to programme my own ramp up / ramp down, which as I figure is just the same as getting an LED to ramp up & down and the accelstepper library wouldn't be needed. Is it as simple a that or am I missing something?

Just powering up the motor can cause it to move 1/2 step, and there is usually nothing you can do about that.

There are a couple of options: a shaft position encoder or a limit switch. With a limit switch (which can be optical, Hall effect magnetic or electrical), the idea is that at startup, you don't know where the shaft is positioned, so you run the motor until it runs into the switch. That serves as the zero reference for future movements.

If the motor is rotating a disk, a slot, hole or pin in the disk can be used to activate an optical interrupter.

@ jremington, I have it set to travel at 300 steps, after start up of the Arduino, then I pull an input to LOW the 1st cycle moves (approx??) 150 steps.

Sorry, I misunderstood. "Half the normal rotation" is one-half step to me.

If you want better informed help from the forum and especially if you are using a microstep driver, you need to clearly describe the type of driver and how it is wired. A schematic diagram would be very helpful. It is possible that the driver is misconfigured.

theautomationguy:
@ jremington, I have it set to travel at 300 steps, after start up of the Arduino, then I pull an input to LOW the 1st cycle moves (approx??) 150 steps.

I don't see anything in your code that would cause that to happen so I wonder if AccelStepper is incorrectly assuming where the motor starts from. I think there is a method that gets it to report the current position - and another method that allows you to set the current position to any value you like.

I would write a short sketch that uses no buttons and just runs the motor CW and CCW and then stops - and use that to explore the AccelStepper system.

Is your setMaxSpeed very high? What is the recommended maximum?

...R