0

I have a complex sketch with Arduino Uno, Bluetooth Serial Com using altSoftSerial, serial com using SoftwareSerial and Serial communication with PC.

I want to move one stepper motor and stop it when a signal is received. I use A4988 Pololu driver and NEMA 17 bipolar stepper motor.

This code is working fine. MyStepper.moveTo(position); MyStepper.runToPosition();

If I change the code to MyStepper.moveTo(position); MyStepper.run(); so that the running does not block my code the motor does nothing Absolutely no movement.

The ground of the motor power supply is connected with the ground of Arduino. The motor is working fine with blocking movements.

1
  • read the library documentation for the run() function
    – jsotola
    Commented Feb 2, 2019 at 20:13

2 Answers 2

2

You didn't include any code for us to see what the actual issue is, but I'm willing to guess that you didn't take into account that the run command requires repeated firing.

If your program comes to a point where there's an instance of runToPosition, the blocking function stops the program until the stepper reaches the position specified.

When your program comes to an instance of run(), it fires off a pulse and carries on reading. So you have to use a loop.

stepperMotor.moveTo(3000);
stepperMotor.runToPosition();
// reaches destination

stepperMotor.moveTo(3000);
stepperMotor.run();
// moves a single step

stepperMotor.moveTo(3000);
while (stepperMotor.distanceToGo > 0) {
stepperMotor.run();
}
// reaches destination

The reason you'd use run over runToPosition is to allow for continuous operations / parameters to be executed. For example, two stepper motors running simultaneously:

stepperMotor1.moveTo(5000);
stepperMotor2.moveTo(3000);
while ((stepperMotor1.distanceToGo > 0) && (stepperMotor2.distanceToGo > 0)) {
stepperMotor1.run();
stepperMotor2.run();
}
// Both motors will run, but will both stop when either of them reaches their destination.

Depending on the speed of your microprocessor, you can create some sophisticated instruction sets for your motor(s).

-1

The problem is at blocking delays. The Run function is interrupted by any other action of program. So you have to call it all the time. A call to run is required in the loop and I wrote a simple mydelay function which solves the problem if you have to use delay for bluetooth communication or other purpose.

int mydelay(int j) { 
  long endmillis;
  endmillis = millis() + j;
  while (millis() < endmillis) {
  MyStepper.run();
  }

I hope I help someone because it took me a while to figure this out as a newbie to Arduino.

5
  • 2
    i downvoted your answer because you wrote a blocking function to solve a problem caused by another blocking function ..... it may work for you, which is great, but blocking functions should be avoided .... i upvoted your question because you solved it yourself...... also, your solution suffers from the rollover bug and it may mess up after a 49 day run ..... read arduino.stackexchange.com/questions/12587/…
    – jsotola
    Commented Feb 3, 2019 at 4:48
  • In my case I need to wait for bluetooth to send a whole string. If I send the string "12345", The if bluetoothserial.available() catches the 1, maybe 2, loses 3 4 5 which arrive in a few ms. I need a small delay about 10ms. My device is never going to work for 50 days that the clock timer will reset. Anyway this was the solution to my problem and I managed to move the stepper controlled by mobile, it moves smoothly and while it is moving I can change the target position and it will change it's direction with proper accelerations. I really couldn't find a better way and I searched a lot.
    – Dimitris
    Commented Feb 4, 2019 at 6:10
  • that is why i said that it is great that it works for you .... the reason for downvote is because other people will do research for a problem that they may have and they will find your answer ..... it is important that they think about possible problems if they adopt your solution
    – jsotola
    Commented Feb 4, 2019 at 7:19
  • about the bluetooth comm ..... if you send a "12345", is there an end-of-line character like a CR or NL at the end?
    – jsotola
    Commented Feb 4, 2019 at 7:22
  • Yes there is 12345#. Sometimes I send big strings GOTO#1500#. @jsotola Your question triggered my mind and I tried removing the delay and read until # is received. It did not work initially, then I added while incoming!="#", if bluetooth available read incoming and it works. I try to add code in the comment but Ctrl+K does not work so I won't add code for now.
    – Dimitris
    Commented Feb 5, 2019 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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