See how much easier it is to read code when it is consistently formatted
#include <AccelStepper.h>
//#include <MultiStepper.h>
const byte MOVETO = 0;
const byte LINETO = 1;
const byte ORIGIN = 2;
const float MOTOR_DIFF = 1.3;
const int MICROSTEPS = 16;
#define STEPPERX_DIR_PIN 5
#define STEPPERX_STEP_PIN 2
#define STEPPERY_DIR_PIN 6
#define STEPPERY_STEP_PIN 3
#define STEPPERZ_DIR_PIN 7
#define STEPPERZ_STEP_PIN 4
#define en 8
AccelStepper stepper1(1, STEPPERX_STEP_PIN, STEPPERX_DIR_PIN);
AccelStepper stepper2(1, STEPPERY_STEP_PIN, STEPPERY_DIR_PIN);
AccelStepper stepper3(1, STEPPERZ_STEP_PIN, STEPPERZ_DIR_PIN);
int led = 13;
int xPos = 0;
int yPos = 0;
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
// stepper1.moveTo(100000);
stepper2.setMaxSpeed(1000.0);
stepper2.setAcceleration(100.0);
// stepper2.moveTo(1000000);
stepper3.setMaxSpeed(5000.0);
stepper3.setAcceleration(100.0);
// stepper3.moveTo(-1000000);
pinMode(en, OUTPUT);
digitalWrite(en, LOW);
pinMode(led, OUTPUT);
}
void loop( ) {
//receiving data (bytearrays) from Serial,
if (Serial.available() >= 9) {
byte type = Serial.read();
byte xByte = Serial.read();
byte xByte2 = Serial.read();
byte xByte3 = Serial.read();
byte xByte4 = Serial.read();
byte yByte = Serial.read();
byte yByte2 = Serial.read();
byte yByte3 = Serial.read();
byte yByte4 = Serial.read();
//adding bytes for x and y parameter
int x = (int) xByte + (int) xByte2 + (int) xByte3 + (int) xByte4;
int y = (int) yByte + (int) yByte2 + (int) yByte3 + (int) yByte4;
headToPoint(x, y);
}
stepper3.run();
}
void headToPoint(int x, int y) {
long theta = atan2(y, x);
long thetaPos;
long thetaDiff = theta - thetaPos;
if (stepper3.distanceToGo() == 0) {
stepper3.moveTo( thetaDiff * (180 / PI));
thetaPos = theta;
delay(100);
}
}
That's a terrible way to get the Serial data - it would be amazing if it was interpreted correctly. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
I don't know if AccelStepper can work as fast as 5000 steps per second. For testing start with something very much slower - like 200 steps per second.
You don't seem to have any code to establish the HOME or ZERO position for the stepper motor.
You don't have any print statements so you can verify that your complex maths is producing the values you expect.
...R