Controling stepper motor by Serial

Hi, I need help whit my problem. :o
I have 2 engines to move from a java swing program using the serial connection,
I send the positions X motor 1, position Y motor 2.
When I send the first time it works, then it no longer works correctly.

I use drive A4988

Thanks… for your time.

Call from Java

arduino.SendData(“1:”+JTCantidad.getText()+"&");

Console Java
Stable Library

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Comenzo la transmision…
Envia a Arduino: 1:200&
Envia a Arduino: 1:200&

Arduino code

#include <AccelStepper.h>

// Parameter for receiving Serial command (add 1 for final 0)
// Calculate based on max input size expected for one command
#define INPUT_SIZE 30
char input[INPUT_SIZE + 1];
boolean bol;
// Define some steppers and the pins the will use
AccelStepper stepper1(1, 5, 4);
AccelStepper stepper2(1, 8, 7);

void setup()
{
  Serial.begin(9600);

  pinMode(6, OUTPUT); // Enable
  pinMode(9, OUTPUT); // Enable
  digitalWrite(6, LOW); // Set Enable low
  digitalWrite(9, LOW); // Set Enable low
  stepper1.setMaxSpeed(1000.0);
  stepper1.setSpeed(1000.0);
  stepper1.setAcceleration(1000.0);

  stepper2.setMaxSpeed(800.0);
  stepper2.setSpeed(1000.0);
  stepper2.setAcceleration(1000.0);

}

void loop()
{
  // We need this counter to simulate Serial.readBytes which SoftwareSerial lacks
  int key = 0;

  // Start receiving command from Serial
  while (Serial.available()) {
    delay(3);  // Delay to allow buffer to fill, code gets unstable on Attiny85 without this for some reason
    // Don't read more characters than defined
    if (key < INPUT_SIZE && Serial.available()) {
      input[key] = Serial.read();
      key += 1;
    }
  }

  if (key > 0) {
    // Add the final 0 to end the C string
    input[key] = 0;
    // Read each command pair
    char* command = strtok(input, "&");
    while (command != 0)
    {
      // Split the command in two values
      char* separator = strchr(command, ':');

      if (separator != 0)
      {
        // Actually split the string in 2: replace ':' with 0
        *separator = 0;
        int servoId = atoi(command);
        ++separator;
        int position = atoi(separator);
        if (servoId == 1) {
          digitalWrite(6, LOW); // Set Enable low
          //stepper1.moveTo(3000);
          stepper1.runToNewPosition(position);
        }
        if (servoId == 2) {
          digitalWrite(9, LOW); // Set Enable low
          //stepper2.moveTo(5000);
          stepper2.runToNewPosition(200 * 5);
        }
      }
      // Find the next command in input string
      command = strtok(0, "&");
    }

  }
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code consistently for easier reading.

And 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.

...R