1

I am using an Arduino Mega2560 While my motor is moving I can not receive anything from the UARTs. When the motor ends movement, I receive data from UART.

How can I make my motor move without stopping the reception of data from UART?

  • 1
    Why should we take the time to help you if you won't make a little effort with your question? Edit your post to have proper spelling and punctuation. Format your code (hint: Tools > Auto Format if using the standard Arduino IDE). – per1234 Aug 26 '17 at 0:59
1

If you look at AccelStepper.cpp code of the library you're using, you'll notice that runToNewPosition() is:

void AccelStepper::runToNewPosition(long position)
{
    moveTo(position);
    runToPosition();
}

which, in turns calls:

// Blocks until the target position is reached
void AccelStepper::runToPosition()
{
    while (run())
    ;
}

In turn, run() returns false only when the position is reached, as shown here:

boolean AccelStepper::run()
{
    if (_targetPos == _currentPos)
    return false;

    if (runSpeed())
    computeNewSpeed();
    return true;
}

As you can see, your program is blocked until the position is not reached.

Data can be still received by the serial port (until buffer is exceed), but you won't be able to read it until that while ends.

To make the motor running without blocking the program, you should implement the AccelStepper functions in the loop function, in a non-blocking way.

In particular, (I only help you to set the position to 1100. You'll make the effort to bring it back to 0, with small modifications), in rotate_motor() call only:

stepper1.moveTo(1100);

Then in the loop, write somewhere:

stepper1.run();

EDIT: Hint for having the motor moving forward and then backward as in your original code: Add a state variable (say you call it "m"), where you store if you need to move forward (e.g. 2), backward (e.g. 1) or stop (0). Then, before calling stepper1.run(), check if you actually must move (if (m != 0)). When you call the stepper1.run() method, check if it returns false. In this case it means it reached the position, then you must change the value of m accordingly.

  • when I write stepper1.moveTo(1100); stepper1.run(); the motor do not make any movement . it is static. what problem?? – mark Aug 26 '17 at 7:58
  • Because, as you can see in the code, that method only sets the target position and compute the speed. (github.com/adafruit/AccelStepper/blob/master/AccelStepper.cpp). You must also write in the loop stepper.run(). That method is responsible for the actual movement, and it must be continuously called (in the loop function), as written in the cpp source. – next-hack Aug 26 '17 at 8:12
  • ok . but I need to bring it to zero position when it finish rotate. in other words . go to 90 degree then go back to zero . that will be continuous – mark Aug 26 '17 at 9:13
  • I wrote in the answer (I only help you to set the position to 1100. You'll make the effort to bring it back to 0, with small modifications). :) Hint: add a state variable (say you call it "m"), where you store if you need to move forward (e.g. 2), backward (e.g. 1) or stop (0). Then, before calling run, check if you actually must move (if (m != 0)). When you call the run() method, check if it returns false. In this case it means it reached the position, then you must change the value of m accordingly. I added this on the answer. – next-hack Aug 26 '17 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.