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();
}
parseInt()
: it will wait for one second to make sure there are no digits pending. – Edgar Bonet Nov 4 '16 at 11:13