Here is your code adapted to use SafeStrings.
There are some differencies.
With Strings you assign the result this way
speed = recivedData.substring(2, 5)
With SafeStrings
the SafeString that gets assigned the substring is the first parameter
recivedData.substring(speed, 2, 5); //cutting 3 characters behind ";"
Similar for transforming into integers
speed.toInt(mySpeed); // variable "mySpeed" is the integer-variable
It is done this way because the SafeStrings functions deliver a result if an operation was successfull or not.
And SafeStrings have even inbuild-debug-capabilities that print error-messages to the serial monitor if you activate the debug-printing.
You will have to add error-checking for invalid input like
"99;-129"
"abc;3857" etc.
I have added serial printing that will make it easier to narrow down bugs
because it shows what you really have received
So here is the code
#include <SafeString.h>
//cSF(nameOfVariable, length);
cSF(direction, 8);
cSF(speed, 8);
cSF(recivedData, 16); //cSF = short for createSafeString
int myDirection;
int mySpeed;
void setup() {
Serial.begin(9600);
Serial.println("Type D;SSS");
Serial.println("example Type 1;243");
}
void loop() {
/* my message is in format "D;SSS" where:
D is one letter for direction,
then is pause, which can be any character, but I chose semicolon
SSS is one to three digit wchich means speed */
while (Serial.available() == 0) { } //waiting for data
recivedData = Serial.readStringUntil('\n').c_str(); //saving all as one string
Serial.print("received character-sequence #");
Serial.print(recivedData);
Serial.println("#");
Serial.println();
//direction = recivedData.substring(0, 1); //substring cut part with first character, so before ";"
//speed = recivedData.substring(2, 5); //cutting 3 characters behind ";"
recivedData.substring(direction, 0, 1); //substring cut part with first character, so before ";"
recivedData.substring(speed, 2, 5); //cutting 3 characters behind ";"
Serial.print("direction as string #");
Serial.print(direction);
Serial.println("#");
Serial.print("speed as string #");
Serial.print(speed); //printing data
Serial.println("#");
Serial.println();
/* now, I can transform string type data into other types and work with them */
direction.toInt(myDirection);
speed.toInt(mySpeed);
Serial.print("direction as int:");
Serial.print(myDirection);
Serial.println(":");
Serial.print("speed as int:");
Serial.print(mySpeed); //printing data
Serial.println(":");
}
best regards Stefan