Problems controlling 42BYG stepper motor through usb por

Hi,

i am currently trying to control a 42BYG stepper motor through the computer using an Uno, using a piece of code
i received and that i know previously worked.

The problem is that the motor doesn’t run after reading steps from serial.
when i try running the motor using a simple loop such as the one in the end of this post it works fine.
i attach the code at the end.
any suggestions?

#include <AccelStepper.h>

bool rotate;
long current_position;
long steps = 0;

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 13, 12); // 13-PUL,12-DIR

void setup()
{
// Set Stepper Parameters	
stepper.setAcceleration(80000);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(4000);

// Start serial connection
Serial.begin(9600);

// LED_BUILTIN pin is connected to a led on the UNO
pinMode(LED_BUILTIN,OUTPUT);

// No rotation by default
rotate = false;
}

void loop() 
{
	// Check if recieved a message through serial port
   if (Serial.available() > 0) {
		// Turn on led
        digitalWrite(LED_BUILTIN,HIGH);
		// Parse the serial message as int (4-bytes)
        steps = Serial.parseInt();
		// Move the given of steps
        stepper.move(steps);
        rotate = true;
   }
   if (rotate)
   {
	 // Check if the motor still need to rotate to reach target steps
     if ((stepper.distanceToGo() != 0)) 
     {
      stepper.run(); 	  // Move Stepper into position
     }
	 // If reached target
     else
     {
      // Turn off led	  
      digitalWrite(LED_BUILTIN,LOW);
	  // send "Done!" back to the computer
      Serial.print("Done!\n");
	  // Stop rotating
      rotate = false;
     }
   }
}

Lior_Mon: The problem is that the motor doesn't run after reading steps from serial.

I don't see any code in your program to print out the value of steps that the program receives?

My guess is that there is more data in the input buffer than is needed for a single parseInt() so it reads the rest of the data next time and sets steps to 0 or -1. All that would happen so fast that you would not notice.

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

i managed to make it work using RunToNewPosition:

#include <AccelStepper.h>

bool rotate;
long current_position;
long steps = 0;

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 13, 12); // 13-PUL,12-DIR

void setup()
{
// Set Stepper Parameters	
stepper.setAcceleration(80000);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(4000);

// Start serial connection
Serial.begin(9600);

// LED_BUILTIN pin is connected to a led on the UNO
pinMode(LED_BUILTIN,OUTPUT);

// No rotation by default
rotate = false;
}

void loop() 
{
	// Check if recieved a message through serial port
   if (Serial.available() > 0) {
		// Turn on led
        digitalWrite(LED_BUILTIN,HIGH);
		// Parse the serial message as int (4-bytes)
        steps = Serial.parseInt();
		// Move the given of steps
        stepper.move(steps);
        rotate = true;
   }
   if (rotate)
   {
	 // Check if the motor still need to rotate to reach target steps
     if ((stepper.distanceToGo() != 0)) 
     {
      stepper.runToNewPosition(steps); 	  // Move Stepper into position
     }
	 // If reached target
     else
     {
      // Turn off led	  
      digitalWrite(LED_BUILTIN,LOW);
	  // send "Done!" back to the computer
      Serial.print("Done!\n");
      Serial.print("Done!\n");
	  // Stop rotating
      rotate = false;
     }
   }

  
  
}