#include <AccelStepper.h>
#include <MultiStepper.h>
#define MaxSpeed 3600
#define MaxAcceleration 3600
long int moveTo1;
long positions[6];
char id;
String exe;
int idNum;
String msg = "Please Enter Motor ID Followed By The Rotation Value:";
String msg2 = " Rotating To: ";
AccelStepper stepper1(1, 2, 3);
AccelStepper stepper2(1, 4, 5);
AccelStepper stepper3(1, 6, 7);
AccelStepper stepper4(1, 8, 9);
AccelStepper stepper5(1, 10, 11);
AccelStepper stepper6(1, 12, 13);
MultiStepper steppers;
void setup() {
Serial.begin(9600);
Serial.setTimeout(10);
motor1();
motor2();
motor3();
motor4();
motor5();
motor6();
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
steppers.addStepper(stepper3);
steppers.addStepper(stepper4);
steppers.addStepper(stepper5);
steppers.addStepper(stepper6);
}
void loop() {
Serial.println(msg);
while (Serial.available() == 0) {
}
while (exe != "exe") {
idNum = Serial.peek();
moveTo1 = Serial.parseInt();
id = idNum;
Serial.print("Motor: ");
Serial.print(id);
Serial.print(msg2);
Serial.println(moveTo1);
break;
}
exe = Serial.readString();
motor1();
motor2();
motor3();
motor4();
motor5();
motor6();
if (exe == "exe") {
Serial.println("Executing");
steppers.moveTo(positions);
steppers.runSpeedToPosition();
}
}
void motor1() {
if (idNum == 65) { //set motor id to A
positions[0] = moveTo1;
stepper1.setMaxSpeed(MaxSpeed);
stepper1.setAcceleration(MaxAcceleration);
}
}
void motor2() {
if (idNum == 66) { //set motor id to B
positions[1] = moveTo1 / 5;
stepper2.setMaxSpeed(MaxSpeed / 5);
stepper2.setAcceleration(MaxAcceleration / 5);
}
}
void motor3() {
if (idNum == 67) { //set motor id to C
positions[2] = moveTo1 / 5;
stepper3.setMaxSpeed(MaxSpeed / 5);
stepper3.setAcceleration(MaxAcceleration / 5);
}
}
void motor4() {
if (idNum == 68) { //set motor id to D
positions[3] = moveTo1 / 5;
stepper4.setMaxSpeed(MaxSpeed / 5);
stepper4.setAcceleration(MaxAcceleration / 5);
}
}
void motor5() {
if (idNum == 69) { //set motor id to E
positions[4] = moveTo1 / 5;
stepper5.setMaxSpeed(MaxSpeed / 5);
stepper5.setAcceleration(MaxAcceleration / 5);
}
}
void motor6() {
if (idNum == 70) { //set motor id to F
positions[5] = moveTo1 / 5;
stepper6.setMaxSpeed(MaxSpeed / 5);
stepper6.setAcceleration(MaxAcceleration / 5);
}
}
I'm trying to control a series of stepper motors for a project. I'm trying to make it work by having me input a series of instructions (ex. A8000, A being the motor id and 8000 being the amount) and sending them to the Arduino via serial monitor. I want all of the motors to be able to move at the same time if I tell them to, so I setup the run command with a trigger separate from the motor id and rotation value.
Upon testing I managed to get it to work, but only if I set the moveto value before uploading the code. Whenever I try to have both the execution function and the moveto function run at the same time it doesn't work and depending on the order I have them in the code it will either think the "e" in "exe" is a motor id, or it will not understand my motor ids at all. How could I make them work together and not block each other?
Sorry for the rushed text, I'm in a hurry to go somewhere and I wanted to post this before I left.