For some reason my motor moves a certain amount every time I open the serial port without giving it any other instructions. It will move further by the same amount if I close and reopen the serial port. Please could someone tell me how to avoid this? I haven't had this problem before.
I am using two stepper motors with a A4988 driver each.
Here is my code so far
#include <AccelStepper.h>
#include <MultiStepper.h>
// Define pin connections
const int dirPin1 = 2;
const int stepPin1 = 3;
const int dirPin2 = 4;
const int stepPin2 = 5;
String incomingByte; // for incoming serial data
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper stepper1(motorInterfaceType, stepPin1, dirPin1);
AccelStepper stepper2(motorInterfaceType, stepPin2, dirPin2);
MultiStepper steppers;
void setup() {
Serial.begin(9600);
String excess1 = Serial.readString();
// Configure each stepper
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
stepper1.setMaxSpeed(100);
stepper2.setMaxSpeed(100);
stepper1.setAcceleration(10);
stepper2.setAcceleration(10);
// Then give them to MultiStepper to manage
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.readString();
// say what you got:
Serial.print("I received: ");
//Serial.println(incomingByte, DEC);
Serial.println(incomingByte);
int x = incomingByte.toInt();
long positions[2];
positions[0] = x;
positions[1] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition();
}
}