Thanks, that's good to know. This leads me to another question, the code below has some unexpected behaviour. The code prints the current position when "CPos" is received over serial and sets a position to move to with "CPos x" where x is a number. The unexpected behaviour is that currentPosition() does not match the requested position, but only on odd tries. i.e sending "CPos 1", will not work ("CPos" will still report 0), then sending "CPos 2" will work, then sending "CPos 3" will not work, then sending "CPos 1" will work (it has nothing to do with the actual number requested). Asking for a negative number (CPos -1) will result in currentPosition() reporting an ever increasing number. Any ideas why this is happening?
#include <AccelStepper.h> //library for stepper motor
#include <SerialCommand.h> //library for serial commands
#define EDDIR 19 // PIN Easydriver Direction
#define EDSTEP 18 // PIN Easydriver Step
int SMode =8;
int SPosition=0;
int SPitch=5000;
int SDistance=5000;
int SPercent=0;
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, EDSTEP, EDDIR); //pin 2=step, pin 3=dir
SerialCommand SerialStepper; // SerialCommand object
void setup() {
//Set Pins and baud rate
Serial.begin(9600); // open the serial connection usually at 19200bps
pinMode(EDDIR, OUTPUT); // set pin 12 to output
pinMode(EDSTEP, OUTPUT); // set pin 12 to output
//Set Serial callback functions
SerialStepper.addCommand("CPos", StepperPosition); // Reports or moves motor to position
}
//Stepper Position Callback function used over serial communication, moves to position
void StepperPosition() {
char *arg;
arg = SerialStepper.next(); // Get the next argument from the SerialCommand object buffer
String Cmd=arg;
if (Cmd != NULL) { // if a command option was given move to position
SPercent=Cmd.toInt();
SPosition= SPercent;//map(SPercent,0,100.0,0,SPitch);
Serial.print("req:");
Serial.println(SPosition);
stepper.moveTo(SPosition);}
else {
Serial.print("got:");
Serial.println(stepper.currentPosition());
}
}
void loop()
{
SerialStepper.readSerial();
stepper.run();
}