help on programming stepper

Hello everyone I don’t know if this the place to post this but I finally figured out how to that program to work the way I wanted it to. If anyone needs
a program to control a stepper with direction and step pulse this is a good one to use. I made it so it would wait for a button to be pushed before it executes the line of code then stops once it has moved the number of steps you tell it to.

 =       Project: S curve
 =      Language: Arduiino r12
 =          Date: January 2008
 =        Author: C. Eckert

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 3500;    // steady state velocity (pulse/sec)
long Pt = 14400;    // total number of pulses for move (7000 steps per rev)

// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0
//                             so you don't calculate to long of a delay
long t12;           // time during constant velocity (microsec)

int count = 0;      // count the number of pulses
int val=0;         // val will be used to store the state of the input pin
int old_val=0;     // this variable stores the previous value of "val"
int state=0;       // 0 =off 1=on

// Arduino pins
#define dirPin 3
#define stepPin 12
#define checkPin 7
void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(checkPin, INPUT);
  // Calculate the time at constant velocity
  t12 = (Pt/(Vm/1e6))-0.5*(ta+td);

  Serial.println("Setup Done");
void loop()

  val=digitalRead(checkPin); //read input val and store it
  if((val==HIGH)&&(old_val==LOW)){ //check if there is a transition
  old_val=val;  // val is now old, let's store it

    digitalWrite(dirPin, HIGH);  // Set the stepper direction

    // Decide which part of the velocity curve your at
    if (t<ta)                                        // Acceleration
      //Serial.println ("Acceleration Curve");
      dly = (ta)/(2*(Vm/1e6)*t);

    else if (t>=ta && t<(ta+t12))                    // Constant velocity
      //Serial.println ("Constant Velocity");
      dly = 1/(2*(Vm/1e6));

    else if (t>=(ta+t12) && t<(ta+t12+td))          // Deceleration
      //Serial.println ("Deceleration Curve");
      dly = 1/(2*((Vm/1e6)-(Vm/(1e6*td))*(t-ta-t12)));

    t = t+2*dly; // update the current time
    //Serial.print("dly: "); Serial.print (dly); Serial.println(" microsec");
    //Serial.print ("Current time: "); Serial.print(t); Serial.println(" microsec");

    // Move stepper one pulse using delay just calculated
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
    count ++;

    // The move is finished
    if (t>(ta+t12+td)){
      Serial.println ("Move Complete");
      Serial.print ("Total steps indexed: "); 
      Serial.println (count);
      t = td/9;

      delay (1000);

      dly = (ta)/(2*(Vm/1e6)*t);

Vm is an integer. 1e6 should be interpreted as an int (although it will overflow an int). The result of the division, if no overflow occurs, is 0. The result of the division, if overflow occurs, is not going to be what you expect.

Any chance that you could explain how this works ? I mean, is very well documented, but some things elude me. I don't understand what's with the calculations. A pulse it's a pulse .. send it .. motor moves one step. Do you need to synchronize frequencies ? Is it like the Arduino sends pulses at a certain speed and the controller accepts pulses at another speed ? Where do you set that ? How do you find that ? DIR I get it is direction. STEP is .. PULSE ? checkPin is what ? Do I need to attach a button between gnd and checkPin ? I know this is total basic stuff .. but if you could throw some schematic in that will be nice. What about ENABLE ? Is there a place for an ENABLE pin in your routine ? What is 1e6 ? 3e6 ? microsec I get it .. why 3e6 and not something else ? Is this 1sec / 3sec or .1sec / .3 sec ? I know I am a super greenhorn .. don't be to hard on me.

PaulS, you are saying that this long dly; should be int dly; ?

I am using this: with Arduino Mega and various stepper motors, 1.8 and 7.5 step res. Do I need to change something in the program to drive either one or another of the two ?

I've been having some trouble making my SD02B work through UART .. no luck at all actually.

Thanks !