Computer to Arduino Serial Interface with Stepper Motors

I’m trying to send commands via USB serial from the computer to the arduino using serial.parse(), but for some reason they stepper motors are not moving, despite receiving integers. Basically, i send a set of 3 (or 4) integers which control 2 stepper motors and a solenoid(which is only on/off). The arduino properly echo’s back the integers i send it (meaning they are stored in the memory as variables) but no movement on the motors. Any ideas how to make this work?

// MultiStepper.pde

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(1,9,8); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(1,5,6);

int xMove = 3;
int yMove = 5;
int solMove = 6;

void setup()
{  
    stepper1.setMaxSpeed(200.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(24);
    
    stepper2.setMaxSpeed(500.0);
    stepper2.setAcceleration(200.0);
    stepper2.moveTo(240);
    
    Serial.begin(9600);
    
}

void loop()
{while (Serial.available() > 0) {
    
 
    int xMove = Serial.parseInt();
    int yMove = Serial.parseInt();
    int solMove = Serial.parseInt();
    Serial.print(xMove);
    
    Serial.print(yMove);
    
    Serial.print(solMove);
    
    
    
    
    
    
}
  stepper1.moveTo(xMove*100);
  stepper2.moveTo(yMove*100);
  stepper1.run();
  stepper2.run();
    

}

Do the examples that come with the library work?

    int xMove = Serial.parseInt();
    int yMove = Serial.parseInt();
    int solMove = Serial.parseInt();

Your problem is with the use of "int" in these lines. This creates three local variables that (confusingly) have the same names as the variables you declared before setup(). The values in the local variables are not visible outside the loop so they can't be used with stepper.moveto(). Just remove the "int" from these three lines and it should work.

...R

The examples with the library work fine for moving the steppers, but the library doesn't address any serial communication.

I removed the int from the variables, and now this is the behavior i'm getting:

  1. The steppers move to a specific location upon powering up (i assume they have some way of knowing they are at zero degrees)

  2. i enter the commands in the serial window, i.e. (10,10,10) and the steppers move the corresponding number of steps.

  3. After that, there is no further movement from the steppers regardless of the commands that are sent.

Could this be some sort of buffer or timing issue? Communicating via serial is quite convoluted.

After #2 when things don't work any more, are the numbers echoed back in the serial monitor?

The reason the steppers move at startup is because the code in setup() tells them to.

Steppers have no means to know where they are. You have to write code to figure that out.

If you send a second (or third) lot of commands are the values correctly echoed to the Serial Monitor?

If so you will need to study the AccelStepper library in case you are not using it correctly. I haven't used it myself.

I wonder if you need to wait for a bit after stepper.run to give things time to move?

It's probably a good idea to include some control code in what you send to the Arduino. For example I would send a special start byte. The Arduino would ignore everything that arrives until it sees that. Then the Arduino would save all the following bytes until it detects a special byte that indicates the end of the valid transmission. When a valid transmission has been received the Arduino would then interpret the data and send it to the motors.

...R