user27124: I wrote a code that controls a stepper motor using the AccelStepper library. When it receives a serial signal from Visual Basic that contains a coordinate value, the Arduino will control the stepper motor by this value. The code works very well but it runs 1 second after receiving the value over the serial port. I want the code to run faster. Here is the code:
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);

int pos = 0;

void setup()
{ 
  Serial.begin(115200); 
  stepper.setMaxSpeed(90);
  stepper.setAcceleration(90);
}

void loop()
{
  if (Serial.available()) {
    int steps = Serial.parseInt();
    stepper.moveTo(steps);

    if (stepper.distanceToGo() == 0) {
      stepper.moveTo(stepper.currentPosition());
      stepper.setSpeed(100);
    }
  }
  stepper.run();
}


user3152: As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000 ms (1 second). There are a couple of ways to fix this, order most difficult to least:

Instead of just sending a number, send a number and then a letter at the end. So instead of sending "123", you would send "123x".  parseInt() will immediately return when it hits the x.  However, you'll still have the x in the stream so you'll need to manually read it and discard it.
Make the value that is sent a set number of digits - say 4 for example.  Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying.  If you read in digit1, digit2, digit3, digit4 then the number is 1000 * digit1 + 100 * digit2 + 10 * digit3 + digit4.
Make the timeout value for parseInt() smaller.  parseInt() inherits from the Stream class so you would use Serial.setTimeout(50) in the setup function to change it to a 50 ms timeout.


27124: <p>I wrote a code that controls a stepper motor using the AccelStepper library. When it receives a serial signal from Visual Basic that contains a coordinate value, the Arduino will control the stepper motor by this value. The code works very well but it runs 1 second after receiving the value over the serial port. I want the code to run faster. Here is the code:</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;AccelStepper.h&gt;
#include &lt;MultiStepper.h&gt;
#include &lt;AccelStepper.h&gt;
#include &lt;MultiStepper.h&gt;
#include &lt;AccelStepper.h&gt;

// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);

int pos = 0;

void setup()
{ 
  Serial.begin(115200); 
  stepper.setMaxSpeed(90);
  stepper.setAcceleration(90);
}

void loop()
{
  if (Serial.available()) {
    int steps = Serial.parseInt();
    stepper.moveTo(steps);

    if (stepper.distanceToGo() == 0) {
      stepper.moveTo(stepper.currentPosition());
      stepper.setSpeed(100);
    }
  }
  stepper.run();
}
</code></pre>
 3152: <p>As others have said, this issue is created because the parseInt function reads in characters until it receives a non numeric character or until it reaches the timeout value, which is set at 1000 ms (1 second). There are a couple of ways to fix this, order most difficult to least:</p>

<ol>
<li>Instead of just sending a number, send a number and then a letter at the end. So instead of sending "123", you would send "123x".  <code>parseInt()</code> will immediately return when it hits the x.  However, you'll still have the x in the stream so you'll need to manually read it and discard it.</li>
<li>Make the value that is sent a set number of digits - say 4 for example.  Since you know it will be four digits, use a for loop to read in four characters and build it into a number by multiplying.  If you read in digit1, digit2, digit3, digit4 then the number is <code>1000 * digit1 + 100 * digit2 + 10 * digit3 + digit4</code>.</li>
<li>Make the timeout value for <code>parseInt()</code> smaller.  <code>parseInt()</code> inherits from the Stream class so you would use <code>Serial.setTimeout(50)</code> in the setup function to change it to a 50 ms timeout.</li>
</ol>
