Are there some techniques for threading-smiliar behaivor? (Stepper Motors)

Hi there,

I wonder how to solve this problem.

I have a steppermotor wit A4988 Driver. Basically I'am running the motor with Stepcommands:

void loop() {

void speedCommand(float f_speed) {
  digitalWrite(xStepperStepOut, HIGH);
  digitalWrite(xStepperStepOut, LOW);

But now I want to read from the Serial Port a String like this:

void getValues() {

  if (Serial.available()) {
    dataIn = Serial.readString();
    setSpeedval = dataIn.toFloat();

Obviously this takes time. In that time the stepper stops of course.

Now my question is, how is this beeing solved generally? I mean there has to be a solution
because when I look into 3D Printers they do not stop for every command :wink:

The trick is to not stop the whole program to read the whole command. Look at the Serial Input Basics thread. You want to read one char at a time as they come in so you can keep up with your other code. Then only when you have the complete message do you stop and do something else.

Two key issues revealed in the code snippets you posted; the use of delay (a process stopper) and the use of String (uppercase S). Neither of these is useful in performing real-time tasks on a resource-restricted platform.

Get rid of delay by creatively using the available timers to execute tasks based on clock values, see the very first post ‘Using millis() for timing. A beginners guide’ for more info. Get rid of String by using character arrays (see DG advice in #1).

It takes about 40 clock cycles to send a one micro second pulse to a stepper motor. A full-step 1.8 degree stepper operating at 300RPM requires a pulse every millisecond. That’s 16,000 clock cycles. Take 40 out to send the pulse, do you think there’s time left for other things?

Yes this solution is good and its working. For further understandings however I still wonder what happens when the programm is getting more complex. For example a 3d Printer. 3 Axis, Serial Communicatio, PID Controller for heating the nozzle and so on...

How do those developer solve this problems? Do they split this into more CPU's ? Or one faster CPU?

I cannot imagine that i can handle all this at one time...

Yes you can handle that. To be honest, that's not even really a lot to try to handle. You just have to write the code the right way so that nothing blocks execution.