Arduino Trigger help

Hello, I am working on a project that will be integrated within matlab to analyze some data that I will get using alternate components. I am using an arduino board to turn a stepper motor a predetermined distance which will then gather more data at each position. Trying to run the arduino board using matlab took too long for the program to send all commands to the arduino board and it needed to run for 23 iterations. I then decided to program an arduino board to wait for a "trigger" from another arduino board that will be connected to the matlab code. That way when we send a command using to the first arduino board, it only sends a simple code to it that will then communicate to the other arduino using the SoftwareSerial library.

I am not very strong in coding arduino, but I managed to get information from various examples I have found and pieced them together to create my current examples. I am not looking to have someone do the project for me I would just like some help or guidance to know whether what i am doing is correct and why it will or will not work.

To simplify the explanation I will call the arduino board communicating with matlab as board 1 and the arduino board running the stepper motor as board 2.

For board 1, I am trying to program it using arduino IDE just to create a loop, that way I will know if what I am attempting in terms of using it as a "trigger" will work. If I can get it working properly, then I will integrate it into matlab with the same idea. Here is the code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,3); // RX, TX
String incoming = " ";

void setup() {
  // open serial communications and wait for signal
  Serial.begin(57600);
  
  // wait for connection from serial port
  while(!Serial){
    ;
  }
  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  Serial.println("begin");
}

void loop() {
  // Write to the serial port which will be read by second arduino board
  if (Serial.available())
  {
    incoming = Serial.readString();
    
    if (incoming == "begin");
    {
      mySerial.write("turn");
    }
  
  } 
  delay(10000);
  Serial.println("begin");

}

For board 2, I am trying to have it read from the serial port that I created in board 1 and recognize what was read to trigger the stepper motion. Here is the code:

#include <SoftwareSerial.h> // including Serial Port Library
#include <Stepper.h> // including stepper motor library

// defining pins section
int stepIN1Pin = 8;
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;
int stepsPerRotation = 1000; // amount of steps per revolution
String ComIn = " ";

SoftwareSerial mySerial(2, 3); // RX, TX
Stepper myStepper (stepsPerRotation, stepIN1Pin, stepIN2Pin, stepIN3Pin, stepIN4Pin);

void setup() 
{
  // Open serial communications
  Serial.begin(57600);

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);

  // Set stepper motor speed
  myStepper.setSpeed(25);
}

void loop() 
{ // run over and over
  
  if (mySerial.available()) 
  {
    ComIn = mySerial.readString();
    if (ComIn == "turn")
    {
      myStepper.step(stepsPerRotation);
      mySerial.write("Moving\n");
    }
    
  }
  
}

I hope someone understands what I am attempting to do and is able to help. Thank you so much for your time.

Trying to run the arduino board using matlab took too long for the program to send all commands to the arduino board

How long is too long ?

You have to understand that Serial is S L O W !

The Arduino can do thousands of things in between each character arriving. So constructs like readString() just wait for absurdly long periods, like seconds, to see if there's just one more character to come in the string.

The program should be able to run and do its thing without serial data. It should be controlling the motor and reading inputs like switches and buttons thousands of times per second. IF a character arrives, then see what it is and decide if you need to do anything. That decision will take just a few nanoseconds, so it doesn't take much time away from the main work.

If you're waiting for a command like "begin" then when you see a 'b' you want to see an 'e' next. Keep doing your thing but check if any new character arrives. If you get something which is not an 'e' then you go back to waiting for a 'b'. There could be milliseconds or hours between the 'b' and 'e' from the Arduino's point of view. Only when you've seen all the characters up to 'n' do you actually do something.

Often single-character commands are best. 'b' for begin, 's' for stop, whatever.