How to get stepper motor to move a carriage on rails N steps at a time?

Hi guys,

I've been playing around with a stepper motor using the Arduino motor shield. So far i have this piece of code (see below), which indefinitely moves the carriage in a set direction as long as it's connected to power, given that the rails don't come to an end. How do i get my stepper motor to, for example, move N steps in forward/backward direction? Is there a function i can call for that (something like: moveStepper(forwards, 300 steps) ? And how do you determine the distance the stepper moves - in terms of lead screw rotations or in terms of time? Currently, when i want to reverse the direction, i have to swap HIGH and LOW in the indicated positions in the code (marked: change HIGH/LOW for direction!). I would really appreciate it if someone with a wide stepper motor knowledge could help me out!

int delaylength = 30;

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylength);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B - **change HIGH/LOW for direction!**
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylength);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylength);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B - **change HIGH/LOW for direction!**
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylength);

}

I tried the code below and it gets the stepper motor buzzing but the carriage moves nowhere. I'm not sure what pins to put as the inputs for the myStepper function, so i just guessed based on this:

"The pin breakdown is as follows:

Function Channel A Channel B
Direction Digital 12 Digital 13
Speed (PWM) Digital 3 Digital 11
Brake Digital 9 Digital 8
Current Sensing Analog 0 Analog 1".

Can anyone tell whether i'm approaching this problem from the right direction or am i doing it completely wrong?

#include <Stepper.h>

const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins:
Stepper myStepper(stepsPerRevolution, 3, 11, 12, 13);

void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-10*stepsPerRevolution);
delay(500);
}

youngmike:
I've been playing around with a stepper motor using the Arduino motor shield.

Which stepper motor.
An Arduino motor shield (L298 chip) is a poor brushed motor driver, and can't drive most stepper motors.
Leo..

These links may help
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

...R

And how do you determine the distance the stepper moves - in terms of lead screw rotations or in terms of time?

Steppers move in steps, checkout the AccelStepper library examples perhaps in the first instance.

Most steppers these days are low impedance and require a stepper driver, not a motor shield, note.

From the Arduino store, motorshield Rev3:

"The Arduino Motor Shield allows your arduino to drive DC and stepper motors"

This confuses newbies (but increases useless sales).
Leo..

MarkT and Wawa, are you two suggesting that it's not possible using the motor shield?

Did you trying swapping the unknown motor wires around? Maybe the unknown motor needs more current than the shield can supply.

youngmike:
MarkT and Wawa, are you two suggesting that it's not possible using the motor shield?

You didn't tell us what motor you're using.
Should we guess?

OK, lets guess, its a 1.7 ohm, 1.7A NEMA17 and its totally unsuitable for a motor shield, requiring
a DRV8825 or similar?

But maybe its a 40 ohm 0.3A high impedance stepper than can be driven from a motor shield,
but only at low rpm, since there is no voltage overhead available for fast operations?

Probably one of the two...

the stepper motor is this one from farnell:

ST4209S1006-B, and it says 4.0V 0.95A

4.2ohm/phase.
Not a good match for a brushed motor driver.
Could be very hot, or shut down.
Leo..