Problem with accelStepper, coding and processing

Hi, I’m doing a project with a robot arm(with three steppers) and processing. Actually, it’s a kind of drawing machine, the robot will be controlled by a webcam. A moving dot is captured by the cam and then transit the coordinate of dot to Arduino.

For now, I just want to control one stepper, and let it rotate by xy position. The problem is it always move by the same angle and same frequency. I guess there is something wrong with headToPoin function, is anybody could help me, please? Thank you.

This is the code:

#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);
}

}

Why is it so very hard to read few simple guidelines before posting?

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

AWOL: Why is it so very hard to read few simple guidelines before posting?

I'm sorry for confusing you, I'm a fresher for coding. and later I'll add guidelines to this code!

Robin2:
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](http://forum.arduino.cc/index.php?topic=396450.0) - 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

Thank you for your response. It seems like If I use processing to transmit data to Arduino, and then the port will be busy. I used a led to show the state of thetaDiff(if thetaDiff is negative, the led will turn off, else it will turn on), it looks like good.

void headToPoint(int x, int y) {
    long theta = atan2(y, x);
    long thetaPos;    // declare an uninitialized local variable
    long thetaDiff = theta - thetaPos;

What is the value of thetaPos after it is declared?

hannibalh: It seems like If I use processing to transmit data to Arduino, and then the port will be busy. I used a led to show the state of thetaDiff(if thetaDiff is negative, the led will turn off, else it will turn on), it looks like good.

I don't understand that, or more accurately, I don't know what was in your mind that gave rise to that comment.

...R

groundFungus: void headToPoint(int x, int y) {     long theta = atan2(y, x);     long thetaPos;    // declare an uninitialized local variable     long thetaDiff = theta - thetaPos;

What is the value of thetaPos after it is declared?

thetaPos = theta; Isn't it correct? thetaPos stores the value of theta in the end of the function.

Robin2:
I don’t understand that, or more accurately, I don’t know what was in your mind that gave rise to that comment.

…R

I’m sorry about my English…

Somebody said “You can’t have the Serial Monitor and your Processing code running at the same time. The COM can only work for one at a time.”

Instead of the port monitor, I use a led for showing the status of x and y, and there is no problem with it.

hannibalh: Somebody said "You can't have the Serial Monitor and your Processing code running at the same time. The COM can only work for one at a time."

Yes, that is correct.

It would be a good idea to include in your Processing code the ability to display messages that come from the Arduino.

And your English seems fine. My inability to understand was not due to your ability with English :)

...R