Stepper motor control

I’m using Arduino IDE 1.8.13 with an Uno SMD edition
I have a Geckodrive G203V G203V Vampire Digital Stepper Drive | USA Made | Geckodrive
and a Moons stepper motor 60VDC MAX, 3000RPM MAX PL23HS8L4360 - NEMA 23 PowerPlus Hybrid Stepper Motors | MOONS'

I’m powering the stepper system with a bench top powersupply (RS-6005D, 0-60V, 0-5A)

I have adapted code that found on line and the stepper turns fine and if I adjust the value of speedVariable the motors speed changes. So that’s all fine.

const int dirPin = 6;
const int stepPin = 8;

int interval = 1000*5;
unsigned long previousMicro = 0;

int speedPot = 0;
int widthPot = 1;

unsigned long speedVariable = 0;
float widthVariable = 6/2;

float maxWidth = 9.5/2;
int MaxSpeed = 1000;

int Accel = 100;
int curSpeed = 100;
int Stop = 0;
void setup()

void loop()

 speedVariable = 4000;  //0-4000ish
 widthVariable =.1*200; //up to 250 I think

 unsigned long currentMicro = micros();

 int a = (25*speedVariable)/100;
 if ((a*100)<100) MaxSpeed=5000;{  

  interval = 5000-curSpeed; 
  if (interval<0) interval = 0;{
    if (currentMicro - previousMicro > interval){
        previousMicro = currentMicro;

      if (curSpeed!= MaxSpeed && Stop ==0){
            if (curSpeed<MaxSpeed){
             curSpeed = curSpeed + Accel;

I have stripped out a fair bit of other code that was not needed but can’t seem to remove/rewrite/slim down any other lines without stopping speedVariable from working and changing the motors speed but could you take a look at the code and help me understand what this section does? If I remove it the speed control stops working and yet I cant see how curSpeed, MaxSpeed, Stop or Accel do anything.

     if (curSpeed!= MaxSpeed && Stop ==0){
            if (curSpeed<MaxSpeed){
             curSpeed = curSpeed + Accel;

I appreciate your time,

The code in the last snippet in your Post is what accelerates the motor by changing the value of curSpeed. Earlier in the program (at Line 41) the value of interval is adjusted by the value of curSpeed. At line 45 the value of interval is used to control how often steps are produced.

If you don’t want acceleration then have a look at the second example in this Simple Stepper Code - it is much the same as what you have, but without aceleration.

This simple acceleration code is also fairly similar to your acceleration code.

Note a BIG error on this line

 if ((a*100)<100) MaxSpeed=5000;{

there should be no semi-colon. It has the effect of immediately terminating the IF clause.


That’s a big stepper, you definitely need acceleration to get it up to full speed, there’s a lot of

Have you looked at the AccelStepper library, that might be the simplest starting point.

Note that constant acceleration requires the time between steps to vary as the square root
of the reciprocal of time - AccelStepper hides this detail from your code.

Sorry for the delay.
Thank you both for your guidance and suggestions.
I found the solution through your code Robin2 so thank you!!
Here are the values which worked.

// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;

long interval = 250; //Starting half pulse length in ms
long interval2 = 50; //ms to wait before reducing pulse length by 'askel'. It works like an acceloration control.
long previousMillis = 0;
long askel = 1; //ammount to reduce nterval by for each loop of the (currentMillis - previousMillis > interval2)
long minimumInt = 15; //max speed limter

/*for "minimumInt" a lower number is faster
Adjust for maximum speed limit; it should be half the pulse width in micro seconds but according to my scope it need to be adjusted to achieve an accurate rpm.
Dropping the minimumInt to 55 nailed it at 0.125ms or 8000 pulses per second, 
at 1/4 stepping it'll be 800 steps per rev so that's 10 revs per second / 600 revs per min.

void setup(){
pinMode (stepPin, OUTPUT);
pinMode (dirPin, OUTPUT);

void loop(){
digitalWrite(dirPin,HIGH);  // Set motor direction clockwise
unsigned long currentMillis=millis();

digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);

if (interval > 1) {  
interval = interval - askel;
if (interval <= minimumInt) {  
interval = minimumInt;