Is my stepper motor doing what it should?

Hi!

I’m trying to learn to use a stepper motor with Arduino and easy driver.

After I upload my code, the motor starts but it moves in a very strange way. Here’s a video to illustrate better:

Is it moving like it should or is something wrong/broken?

Here is the code I’m using:

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Using the easy stepper with your arduino
//use rotate and/or rotateDeg to controll stepper motor
//speed is any number from .01 -> 1 with 1 being fastest - 
//Slower Speed == Stronger movement
/////////////////////////////////////////////////////////////////


#define DIR_PIN 2
#define STEP_PIN 3

void setup() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
} 

void loop(){ 

  //rotate a specific number of degrees 
  rotateDeg(360, 1); 
  delay(1000);

  rotateDeg(-360, .1);  //reverse
  delay(1000); 


  //rotate a specific number of microsteps (8 microsteps per step)
  //a 200 step stepper would take 1600 micro steps for one full revolution
  rotate(1600, .5); 
  delay(1000); 

  rotate(-1600, .25); //reverse
  delay(1000); 
}



void rotate(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  digitalWrite(DIR_PIN,dir); 

  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
} 

void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir); 

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

If the motor is behaving as expected with this code, then can anyone tell me how I could make it simply rotate in one direction forever (without any fancy stuff)?

Yes that code looks like it will produce that sort of movement.

then can anyone tell me how I could make it simply rotate in one direction forever

Replace your loop function with:-

void loop(){ 

  //rotate a specific number of degrees 
  rotateDeg(360, 1); 
}

Try reading the comments in your sketch, that's what they are there for.

It looks like it does something close to what it is supposed to do. It rotates 360 degrees, waits, then rotates in the other direction. The microstepping looks like it is off. But it looks to me all you need to do is use the command rotate instead of rotateDeg.

Can't tell you more as i haven't been playing with steppers myself yet.

Thanks guys, it worked with Grumpy_Mike's code!

And do you know why (or how) this works ? If you want to do some other stuff while using your stepper, knowing how this works might be of interest.