Stepper Motor stopping while Arduino is Serial communicating

Recently I started work on a project that requires my laptop to send serial communicate with my arduino. I got the communication working, where if a press a key on my keyboard it will send a corresponding message to my arduino, but whenever I send a message the arduino will pause causing the attached stepper motor to stop for a second and then start. If I hold down the key to continuously send messages the stepper motor will only do 1 step at a time with pauses in between instead of continuously rotating. Does anyone know what is causing this, I've attached the code for my arduino below. Right now for testing purposes the motor will continuously rotate if not given a command.

#define relay 13
#define dir 8 //Stepper motor direction pin
#define step 9 //Stepper motor step pin
String command = ""; //Current action to be run
String direction = "null"; //Direction of stepper motor

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.write(1);
  pinMode(step, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() {
  if(Serial.available()) //Checks to see if a message was sent to the arduino, if so, sets the command    var to it
  {
    command = Serial.readString();
    Serial.write(1);
  }
  else command = "";
  
  if (command.equals("FIRE"))  digitalWrite(relay, HIGH); //Runs corresponding actions for commands
  if (command.equals("RIGHT")) stepRight();
  if (command.equals("LEFT"))  stepLeft();

  if(direction.equals("RIGHT") || direction.equals("LEFT"))
  {
    stepMotor();
  }
}

void stepRight()
{
  direction = "RIGHT";
  digitalWrite(dir, LOW);
}

void stepLeft()
{
  direction = "LEFT";
  digitalWrite(dir, HIGH);
}

void stepMotor()
{
    digitalWrite(step, HIGH);
    delayMicroseconds(500);
    digitalWrite(step, LOW);
    delayMicroseconds(500);
}

Quadruple the Baud rate and see if that makes a difference. If it does, then that will point to the problem.

I quadrupled the baud rate and even set it to max on both the laptop program and my arduino, and nothing changed.

What do you think this sends to Serial Monitor?

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Enter data:");
  while (Serial.available() == 0) {}     //wait for data available
  String teststr = Serial.readString();  //read until timeout
  teststr.trim();                        // remove any \r \n whitespace at the end of the String
  if (teststr == "red") {
    Serial.println("A primary color");
  } else {
    Serial.println("Something else");
  }
}

Above is the example from the Serial.readString() section of the Arduino Reference.
Take specific note of the comment on the Serial.readString() line. What does that imply?

readString() waits until no further input is seen.

if you want it to immediately return after one char try using Serial.read() and either buffer that char or process a single char command

This piece of code will start with the first character received and waits until the end of the input is reached or timeout occurs. During that time all other code is blocked.

Better wait for the minimal number of characters available before you start reading the entire input.

I see now, thank y'all. I didn't realize readString() would wait for timeout, probably should have done more digging into how Serial communication actually works.

There are MANY ways to read serial data . You chose only one.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.