Fair enough. Here's the whole code.
#include <AccelStepper.h>
#define BAUDRATE 115200 //Set the Baud Rate to an appropriate speed
#define BUFFSIZE 256 // buffer one command at a time, 12 bytes is longer than the max length
#define DIR_PIN_A 2 //Set Direction Pin for Stepper A to Digital Pin 2
#define STEP_PIN_A 3 //Set Step Pin for Stepper A to Digital Pin 3
#define DIR_PIN_B 4 //Set Direction Pin for Stepper B to Digital Pin 4
#define STEP_PIN_B 5 //Set Step Pin for Stepper B to Digital Pin 5
#define DIR_PIN_C 6 //Set Direction Pin for Stepper C to Digital Pin 6
#define STEP_PIN_C 7 //Set Step Pin for Stepper C to Digital Pin 7
#define DIR_PIN_D 8 //Set Direction Pin for Stepper D to Digital Pin 8
#define STEP_PIN_D 9 //Set Step Pin for Stepper D to Digital Pin 9
long motorPos[4] = {0,0,0,0};
int motorSpeed = 2000; //Set default speed value
int motorAcc = 1000; //Set default acceleration value
int motorRun = 1; //Set default run value
int motorReset = 0; //Set default reset value
boolean moving_1 = true;
boolean moving_2 = true;
boolean moving_3 = true;
boolean moving_4 = true;
//unsigned long timer = 0;
String msg_char = "";
char *parseptr;
char buffidx;
char buffer[BUFFSIZE]; // this is the double buffer
uint16_t bufferidx = 0; // a type of unsigned integer of length 16 bits
uint16_t p1, s1;
// Define some steppers and the pins they will use: Ex: Accelstepper stepper1(1, 3, 4) <- STEP PIN 3, DIR PIN 4.
AccelStepper stepper1(1, STEP_PIN_A, DIR_PIN_A);
AccelStepper stepper2(1, STEP_PIN_B, DIR_PIN_B);
AccelStepper stepper3(1, STEP_PIN_C, DIR_PIN_C);
AccelStepper stepper4(1, STEP_PIN_D, DIR_PIN_D);
/*==============================================================================
- SETUP() This code runs once
============================================================================/
void setup()
{
stepper1.setCurrentPosition(0); //Move steppers to default position
stepper2.setCurrentPosition(0);
stepper3.setCurrentPosition(0);
stepper4.setCurrentPosition(0);
setStepperSpeeds();
Serial.begin(BAUDRATE); // Start serial communication
establishContact(); // send a byte to establish contact until receiver responds
}
/*==============================================================================
- LOOP() This code loops continuously
============================================================================/
void loop()
{
UpdateStepperValues(); //Get all the incoming values from Firefly
if (motorReset == 1) {
stepper1.setCurrentPosition(motorPos[0]);
stepper2.setCurrentPosition(motorPos[1]);
stepper3.setCurrentPosition(motorPos[2]);
stepper4.setCurrentPosition(motorPos[3]);
}
if (motorRun == 1) {
while(1)
{
moving_1 = stepper1.run(); //tell the motors to move to their positions
moving_2 = stepper2.run();
moving_3 = stepper3.run();
moving_4 = stepper4.run();
if(!moving_1 && !moving_2 && !moving_3 && !moving_4){
motorRun = -1; //reset the MotorRun variable
Serial.println("n"); //send character for the next line from Firefly
break;
}
}
}
}
/*==============================================================================
- UPDATE STEPPER VALUES FUNCTION()
============================================================================/
void UpdateStepperValues(){
char c; // holds one character from the serial port
if (Serial.available() > 0) {
c = Serial.read(); // read one character
buffer[bufferidx] = c; // add to buffer
if (c == '\n') {
buffer[bufferidx+1] = 0; // terminate it
parseptr = buffer; // offload the buffer into temp variable
//********************************************************
motorPos[0] = parsedecimal(parseptr); // parse the first number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorPos[1] = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorPos[2] = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorPos[3] = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorSpeed = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorAcc = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorRun = parsedecimal(parseptr); // parse the next number
parseptr = strchr(parseptr, ',')+1; // move past the ","
motorReset = parsedecimal(parseptr); // parse the next number
setStepperSpeeds();
stepper1.moveTo(motorPos[0]); //set new absolute target position
stepper2.moveTo(motorPos[1]); //set new absolute target position
stepper3.moveTo(motorPos[2]); //set new absolute target position
stepper4.moveTo(motorPos[3]); //set new absolute target position
bufferidx = 0; // reset the buffer for the next read
return; //return so that we don't trigger the index increment below
}
// didn't get newline, need to read more from the buffer
bufferidx++; // increment the index for the next character
if (bufferidx == BUFFSIZE-1) { //if we get to the end of the buffer reset for safety
bufferidx = 0;
}
}
}
double parsedecimal(char *str)
{
return atof(str);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("waiting..."); // send an initial string
delay(200);
}
}
void setStepperSpeeds() {
stepper1.setMaxSpeed(motorSpeed);
stepper1.setAcceleration(motorAcc);
stepper2.setMaxSpeed(motorSpeed);
stepper2.setAcceleration(motorAcc);
stepper3.setMaxSpeed(motorSpeed);
stepper3.setAcceleration(motorAcc);
stepper4.setMaxSpeed(motorSpeed);
stepper4.setAcceleration(motorAcc);
}