HobbyCNC Easy Driver Sketch

I was having trouble using the stepper.h library to turn a stepper motor a designated # of turns at a designated speed. I ended up writing my own sketch to do this without the benefit of a library. The HobbyCNC board takes High/Low signals for step and direction, much like PWM. The sketch I wrote works, but being new to all this stuff (including programming).

Can you guys look over this and give me any pointers to improve it?

I noticed it does “burp” a little when it has to print to Serial Monitor, but I just put this in to test the motor output turns.

I’m writing this for a guitar pickup winder I’m building, and will eventually have a sketch for auto-traverse for another stepper motor. My plan will be complete interaction with an LCD screen and keypad. I’m new to all this, so I’m sure more questions will follow as I proceed.

/*  
    Sketch to rotate a stepper motor a number of designated revolutions (turns) and output
    the number of turns to Serial Monitor.
    This sketch was designed for the HobbyCNC 4AUCP REV 0 driver board that accepts digital
    I/O (high/low) signals for step and direction.

     Created by William Begg 
     December 8, 2011
*/

#define stepPin 8      // Sets step pin to Digital pin 8
#define dirPin 9       // Sets Direction pin to digital pin 9

const long StepSeconds(75000);    // Microsecond Factor for setting actual speed. 1/2 of time to move 1 RPM. (Hi and Low will be 150,000 microseconds )
long NumberTurns(500);            // Number of turns to complete
int MotorSpeed(200);              // Speed in RPMs
const int StepsPerRev(400);       // Motor steps per revolution. Most will be 200, but my driver operates 1/2 Step.
long NumberSteps(0);              // Variable for counting steps
long CurrentTurns(0);             // Tracks current # of turns

void setup() 
{
  Serial.begin(9600);
  
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(dirPin, HIGH);  // Set direction to clockwise
  digitalWrite(stepPin, LOW);  // Initializes stepper as Idle
}

void loop() 
{
 
  if (CurrentTurns < NumberTurns)          //Continues loop as long as designated number of turns has not been reached
  {  
  int StepRate(StepSeconds/MotorSpeed);    //Sets the microseconds between high and low signals for speed calculation.
  if (NumberSteps <= StepsPerRev)          // Goes to empty routine when number of turns is reached.
    {   digitalWrite(stepPin, HIGH);       // Next 4 lines moves 1 step at designated speed (MotorSpeed) 
        delayMicroseconds(StepRate);       // Similar to PWM    
        digitalWrite(stepPin, LOW);        //
        delayMicroseconds(StepRate);       // 
        NumberSteps++;                     // Increments variable i for designating number of steps for 1 revolution.
    }
   else {
        NumberSteps = 0;                     // Re-initialize variable 
        CurrentTurns = CurrentTurns++;       // Increments current # of turns by 1
        Serial.println(CurrentTurns);        // Prints the current # of turns to Serial Monitor
        }
  }
  else 
  {}                                        // Starts an empty loop when # of turns (NumberTurns) has been reached.
}
const long StepSeconds(75000);    // Microsecond Factor for setting actual speed. 1/2 of time to move 1 RPM. (Hi and Low will be 150,000 microseconds )

StepSeconds contains a value in microseconds? Why doesn’t the name reflect this?

I know that assigning a value this way works, but I prefer the conventional

const long StepMicroSeconds = 75000;
  else 
  {}                                        // Starts an empty loop when # of turns (NumberTurns) has been reached.

No, it doesn’t. Get rid of the empty else. It is doing absolutely nothing.

        CurrentTurns = CurrentTurns++;       // Increments current # of turns by 1

CurrentTurns++;
is equivalent to
CurrentTurns = CurrentTurns + 1;

So, your code is equivalent to:
CurrentTurns = CurrentTurns = CurrentTurns + 1; // Increments current # of turns by 1
Looks a little silly written that way, doesn’t it?

Thanks PaulS for the input. I just put the empty function to get out of the loop to wait for a reset. There will eventually be a function to call after the # of turns is reached, but just open for now. The last time I programmed anything was 25 years ago in college. (Cobol, Fortran) and always got gigged for improper layout and flow.

Here's a video of the code in action.