Lag before motor starts - Arduino Motor Shield

Hello,

We are using the Arduino Motor Shield to control a two-wire linear actuator. We noticed that there is always a 1 second lag between the time that the pins are activated and the time motor actually starts moving. I tested the response time of the actuator by connecting it straight to a battery supply and the motor starts moving right away. What could be causing this lag issue?

The following is the simple sketch being used:

String serialCommand;
String ACTExtend("act_extend");
String ACTRetract("act_retract");
String ACTStop("act_stop");
bool bACTIsMoving = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop() {
  // put your main code here, to run repeatedly:
  // if there's any serial available, read it:

  while (Serial.available() > 0) {
    serialCommand = Serial.readString();
    serialCommand.trim();
    
    if(serialCommand == ACTExtend)
    {
      //forward @ full speed
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A
      digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      analogWrite(3, 255);   //Spins the motor on Channel A at full speed
      digitalWrite (LED_BUILTIN, LOW);
    }
    else if(serialCommand == ACTRetract)
    {
      //reverse @ full speed
      digitalWrite(12, LOW); //Establishes backward direction of Channel A
      digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      analogWrite(3, 255);   //Spins the motor on Channel A at half speed
    }
    else if(serialCommand == ACTStop)
    {
      //stop moving
      digitalWrite(9, HIGH); //Engage the Brake for Channel A
    }
  }
}

Thanks,

-brian

I can guess already.

Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).

Serial.setTimeout() sets the maximum milliseconds to wait for serial data. It defaults to 1000 milliseconds.

1000 milliseconds = 1 second

Try

serialCommand = Serial.readStringUntil('\n');

Thanks, this worked great!

Great. It's important to us that you understand why it worked and your original code had that lag. Please ask if you need any further explanation.