Controlling the speed of nema 23 stepper motor using arduino serial monitor

Hi, I am a very beginner in programming. I need help regarding an issue in the code below.
when I enter an integer like 2, or 5 as input on the Arduino serial monitor, the motor starts moving at the speed of 200 or 500 (microseconds delay) respectively. but when I again enter some other integer as input on the same serial monitor window, no change in the speed. What I want is to move the stepper motor with the speed according to the input value. but must be on the same serial monitor. I want to change the speed of the motor while it is moving.
thanks in advance.
Here is the code.

#define pulPin 9
#define dirPin 8
long delayValue;
void setup() 
{
Serial.begin(9600);
pinMode(pulPin,OUTPUT); // set Pin9 as PUL
pinMode(dirPin,OUTPUT); // set Pin8 as DIR

while (Serial.available() == 0){};
  
    delayValue = Serial.parseInt();
    delayValue = delayValue*100;
    Serial.println(delayValue);
    
    }
    
void loop() 
{ 
    
digitalWrite(dirPin,LOW); // set high level direction
digitalWrite(pulPin,HIGH);
delayMicroseconds(delayValue);
digitalWrite(pulPin,LOW);
delayMicroseconds(delayValue); 


}

Because eventually, you’ll want to do other things while the motor is running (like check buttons and limits without latency) - I strongly suggest you start reading about millis() timing rather than using any delay() techniques.

What you’ll end up with is a millis() or micros() timer that calls a function which steps the motor every time it’s called.
You can adjust that calling frequency (the step rate) any time while the motor is stopped or running.

When I write stuff like this, I define a target position, and let the step function count the number of steps to determine the ‘current’ position.
— then you really don’t have to do anything other than adjust the (millis) frequency if needed, letting the millis() event trigger each step.

(I’ve used this method to get over 10kHz step rates in multiple motors simultaneously.)

The difference between the current and target position is handled by the ‘step’ function to determine the direction, and how many steps it needs to make, otherwise the function does nothing and returns - waiting until the ‘global’ target position is changed in your main program code… then the millis event and step function will resolve the movement automatically without any intervention.

As you get more experienced, you might start using one or more of the hardware timers to step the motor drivers.)

Thanks for your kind suggestions. I will study your recommended technique.

Steppers are usually 200 steps per rev or an integer multiple thereof so I don't see where your 500 is coming from.

That is because you are asking for the speed from the monitor in the void setup() part of the code.
Void setup() is only executed ONCE at power up, that is at the start of the code.
After that the program runs continuously around the void loop() .

So you are only asking for the speed once.

Tom.. :smiley: :+1: :coffee: :australia:

consider

#define pulPin 9
#define dirPin 8

long delayValue;
void setup ()
{
    Serial.begin (9600);
    pinMode (pulPin, OUTPUT); // set Pin9 as PUL
    pinMode (dirPin, OUTPUT); // set Pin8 as DIR

    digitalWrite (dirPin, LOW); // set high level direction
}

void loop ()
{
    if (Serial.available ())  {
        delayValue = 100 * Serial.parseInt ();
        Serial.println (delayValue);
    }

    digitalWrite (pulPin, HIGH);
    delayMicroseconds (delayValue);
    digitalWrite (pulPin, LOW);
    delayMicroseconds (delayValue);
}

Thank you so much. You solved my isssue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.