user31185: I am trying to run two stepper motor at same time with different RPM.
I decided to use two rotary encoder for change the RPM separately.
But,
I am in problem with driving stepper at same time due to the delay time of the step pulse.
How can I generate separate(different duty cycle) step pulses for stepper motors? It's the main problem that I faced.
I have used some Arduino libraries and all of that library has same system for step pulse generation. But, I couldn't understand those well.
void AH_EasyDriver::setSpeedRPM(long RPM)
{
   float DIV = RPM*this->MOTOR_RESOLUTION;  
    this->STEP_DELAY =  60000000.0L/DIV;
 }

This function calculate the step time for given specific RPM
void AH_EasyDriver::move(int NUMBER_OF_STEPS)
{  
   while(STEPS > 0) {
    if (micros() - this->LAST_STEP_TIME >= this->STEP_DELAY) {
      this->LAST_STEP_TIME = micros();
      STEPS--;
      stepMotor();
}
}

}

This function rotate the motor the motor in STEP_DELAY using stepMotor() function.
void AH_EasyDriver::stepMotor()
{
  digitalWrite(this->STEP_pin, HIGH);
  delayMicroseconds(100);
  digitalWrite(this->STEP_pin, LOW);
  delayMicroseconds(100);
}

This function makes CONSTANT step pulses.
I removed some lines in the function. This is the link for original .cpp file.
All of stepper controlling libraries has this functions.
I couldn't understand how this functions can change the RPM. Can someone explain this?

user77857: The method to produce simultaneous pulses is simple, but requires a different approach similar to Direct Digital Synthesis (DDS).

Keep one counter variable per motor
Increment each counter variable by a set amount in a fixed time loop
When a counter variable reaches a threshold value, generate a very short step pulse
When a threshold is met, subtract the threshold value from the corresponding counter

The amount that each counter variable is incremented sets the generated pulse rate, reaching the threshold faster or slower, therefore setting the motor speed. A value of 0 stops the motor, a value equal to the threshold generates a step pulse for each iteration of the loop, reaching the highest speed. An arbitrary number of motors may be controlled simultaneously as long as the code can run within the fixed loop time. Since a step can only occur at fixed intervals, the rate of the timed loop should be several times the fastest step rate you intend to use to minimize timing jitter.
This style of control is often implemented using a timed interrupt service routine (ISR) which is a short function set to repeat at a several kHz frequency, interleaving with the rest of the program. This allows a slower loop to run a control algorithm simultaneous to the step generation.
If not using a timed interrupt function, the step generation loop must busy-wait until a set time has elapsed before repeating again. This means you cannot do much else than drive the motors until you stop and break out of the loop.

user42015: When using a normal stepper driver, you say the driver to do one step, by sending him one pulse. The length of these pulses does not have a meaning. they are constructed, so that the driver can sense them. The driver does not make smaller steps than 1 full step, if you don't tell him on extra pins (The library you use can utilize those pins too). So the stepMotor() function is correct. You can drive the motors with different speeds by doing a step for each motor in different intervals (different number of steps per time).
Your move() function blocks the execution of other code, until the motion is completed. You are doing this with the while( STEPS > 0 ) loop. You need to modify your code to be non-blocking. This is normally done in the way, that is shown by the BlinkWithoutDelay example of the Arduino IDE. You are using it too: You have to use the millis() or micros() function, to check if it is time to do a step.
Replace the while with an ´if´-statement:
if( STEPS > 0 )

and make the STEPS variable a member variable of the class instance. Then make sure you execute the function often enough, so that it will not miss the time to do a step. Do the same with the second motor. For example something that looks like this:
void setup(){
  ...
  stepper1.doSteps(100);
  stepper2.doSteps(200);
}

void loop(){
  stepper1.move();
  stepper2.move();
}

Here in the setup function the steppers are told to move 100 and 200 steps. (You would have to write the function doSteps(int number_of_steps) yourself). The motion is then done by the move() functions, when it is time to do so. Since these two functions are looped very fast, they shouldn't miss the time for a step.
This is also the way the AccelStepper library works, which is also a common stepper library.

31185: <p>I am trying to run two stepper motor at same time with different RPM.
I decided to use two rotary encoder for change the RPM separately.</p>

<p>But,
I am in problem with driving stepper at same time due to the delay time of the step pulse.</p>

<p>How can I generate separate(different duty cycle) step pulses for stepper motors? It's the main problem that I faced.</p>

<p>I have used some Arduino libraries and all of that library has same system for step pulse generation. But, I couldn't understand those well.</p>

<pre><code>void AH_EasyDriver::setSpeedRPM(long RPM)
{
   float DIV = RPM*this-&gt;MOTOR_RESOLUTION;  
    this-&gt;STEP_DELAY =  60000000.0L/DIV;
 }
</code></pre>

<p>This function calculate the step time for given specific RPM</p>

<pre><code>void AH_EasyDriver::move(int NUMBER_OF_STEPS)
{  
   while(STEPS &gt; 0) {
    if (micros() - this-&gt;LAST_STEP_TIME &gt;= this-&gt;STEP_DELAY) {
      this-&gt;LAST_STEP_TIME = micros();
      STEPS--;
      stepMotor();
}
}

}
</code></pre>

<p>This function rotate the motor the motor in STEP_DELAY using stepMotor() function.</p>

<pre><code>void AH_EasyDriver::stepMotor()
{
  digitalWrite(this-&gt;STEP_pin, HIGH);
  delayMicroseconds(100);
  digitalWrite(this-&gt;STEP_pin, LOW);
  delayMicroseconds(100);
}
</code></pre>

<p>This function makes <strong>CONSTANT</strong> step pulses.</p>

<p>I removed some lines in the function. <a href="https://github.com/ramonidea/Icing-Food-printer/blob/705f0ba2f09132a0ba9c169a89e9b2fd0e2478dc/AH_EasyDriver_20120512/AH_EasyDriver/AH_EasyDriver.cpp" rel="nofollow noreferrer">This</a> is the link for original .cpp file.
All of stepper controlling libraries has this functions.
I couldn't understand how this functions can change the RPM. Can someone explain this?</p>
 77857: <p>The method to produce simultaneous pulses is simple, but requires a different approach similar to Direct Digital Synthesis (DDS).</p>
<ol>
<li>Keep one counter variable per motor</li>
<li>Increment each counter variable by a set amount in a fixed time loop</li>
<li>When a counter variable reaches a threshold value, generate a very short step pulse</li>
<li>When a threshold is met, subtract the threshold value from the corresponding counter</li>
</ol>
<p>The amount that each counter variable is incremented sets the generated pulse rate, reaching the threshold faster or slower, therefore setting the motor speed. A value of 0 stops the motor, a value equal to the threshold generates a step pulse for each iteration of the loop, reaching the highest speed. An arbitrary number of motors may be controlled simultaneously as long as the code can run within the fixed loop time. Since a step can only occur at fixed intervals, the rate of the timed loop should be several times the fastest step rate you intend to use to minimize timing jitter.</p>
<p>This style of control is often implemented using a timed interrupt service routine (ISR) which is a short function set to repeat at a several kHz frequency, interleaving with the rest of the program. This allows a slower loop to run a control algorithm simultaneous to the step generation.</p>
<p>If not using a timed interrupt function, the step generation loop must busy-wait until a set time has elapsed before repeating again. This means you cannot do much else than drive the motors until you stop and break out of the loop.</p>
 42015: <p>When using a normal stepper driver, you say the driver to do one step, by sending him one pulse. The length of these pulses does not have a meaning. they are constructed, so that the driver can sense them. The driver does not make smaller steps than 1 full step, if you don't tell him on extra pins (The library you use can utilize those pins too). So the <code>stepMotor()</code> function is correct. You can drive the motors with different speeds by doing a step for each motor in different intervals (different number of steps per time).</p>

<p>Your <code>move()</code> function blocks the execution of other code, until the motion is completed. You are doing this with the <code>while( STEPS &gt; 0 )</code> loop. You need to modify your code to be non-blocking. This is normally done in the way, that is shown by the <code>BlinkWithoutDelay</code> example of the Arduino IDE. You are using it too: You have to use the <code>millis()</code> or <code>micros()</code> function, to check if it is time to do a step.</p>

<p>Replace the <code>while</code> with an ´if´-statement:</p>

<pre><code>if( STEPS &gt; 0 )
</code></pre>

<p>and make the <code>STEPS</code> variable a member variable of the class instance. Then make sure you execute the function often enough, so that it will not miss the time to do a step. Do the same with the second motor. For example something that looks like this:</p>

<pre><code>void setup(){
  ...
  stepper1.doSteps(100);
  stepper2.doSteps(200);
}

void loop(){
  stepper1.move();
  stepper2.move();
}
</code></pre>

<p>Here in the <code>setup</code> function the steppers are told to move <code>100</code> and <code>200</code> steps. (You would have to write the function <code>doSteps(int number_of_steps)</code> yourself). The motion is then done by the <code>move()</code> functions, when it is time to do so. Since these two functions are looped very fast, they shouldn't miss the time for a step.</p>

<p>This is also the way the <code>AccelStepper</code> library works, which is also a common stepper library.</p>
