Servo and Stepper control through Serial Port problem (need solution asap)

So, as the title says i want to control through Serial Port a Stepper and a Servo attached to the same board.

The “project” is to control via a GUI (which is ready) both the Stepper position and the Servo as a switch, one at a time.

Individually they work ok ( when i upload code for only the servo or only the stepper).

When i combine the two codes only one of them works.

Combined Code:

#include <AccelStepper.h>
#include <Servo.h>

Servo myservo;
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 9, 8);

int steps = 0;

void setup()
  myservo.attach(11);  // attaches the servo on pin 11 
  myservo.write(90); //set servo to midpoint as default

void loop(){;
  if (Serial.available() > 0) {
    steps = Serial.parseInt();
    // only actually set the target if the stepper is stopped
    if (stepper.distanceToGo() == 0) {
   String value = Serial.readString(); 


When i upload it like above only stepper works , and if i have my String value = Serial.readString(); above steps = Serial.parseInt(); only servo works.

Now I understand why this is happening , but what is the solution to this?

Stepper even takes ‘a’ as a command and moves back to 0 position.

How can i distinguish the servo and stepper serial communication ?

Any change to the code would be really appreciated!! Thank you in advance!

Have you read Robin2's Serial Input Basics?


I ll try to read it again and not get confused this time.. I thought that i had to do something much simpler .. but i was mistaken i guess.

Do i have to do many changes to get my code going ? As a first thing i ll make different fuctions for servo and stepper and call them in loop(). I dont want a neat code right now, just want it to work ..

Will Robins guide solve my issue on distinguishing the int values sent to stepper and servo ? ( I am running out time else i would not ask that)

Check the input first, if it's 'a' or 'b', do what you have to do, else parse the input as an int.


@Sparkgap, your arrangements for reading the serial data are not robust. For example the two values could easily get mixed up. And both parseInt() and readString() are blocking functions which will interfere with AccelStepper by preventing loop() from repeating quickly.

Ideally use the third example and a variation of the parse example from Serial Input Basics