Slow Strings :(

Hi, I've finally got some code working to accept mixed string commands from serial - they take the format A123 - where the letter indicates the destination of the numeric value at the end.
The code separates the letter and number and then assigns the value to one of 3 variables - depending on the letter.

However the code takes over 1000ms to run even though its short - I'm assuming that this is because im using strings....?

Id like it to be much faster (but not massively complicated - I'm not so hot on coding so far) - if anyone has any suggestions on how to speed it up I'd love to hear them.

Thanks in advance - Andrew

String incoming;    // Data received from the serial port
String letter;    //resulting letter from the string
String valString;   //value before its converted to an int
int incomingVal = 0;    //incoming value before its assigned to another variable

int valA;   //variables for motorspeed, pos etc
int valB;
int valC;

int startTime;    //for calc speed

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 bps

void loop() {
  if (Serial.available()) {
    startTime = millis();
    incoming = Serial.readString();
    valString = incoming;   //copy incoming string so we can delete the number and letter seperately

    incoming.remove(1);   // remove everything but the first character to get the letter
    letter = incoming;

    valString.remove(0, 1);   //remove the first character from the string to get the number
    incomingVal = valString.toInt();    //convert string to int

    if (letter == "A") {      //assign the value to the correct variable
      Serial.println("Found A");
      valA = incomingVal;
    else if (letter == "B") {
      Serial.println("Found B");
      valB = incomingVal;
    else if (letter == "C") {
      Serial.println("Found C");
      valC = incomingVal;
    else {
      Serial.println("Incorrect Code!");

    Serial.println(millis() - startTime); //how long does the calculation take?
  incoming = Serial.readString();

How long is the timeout for readString?

Your program will actually work, as well as be much faster, if you dump Strings and do this with character arrays (C-strings) instead.

See Serial Input Basics.


  incoming = Serial.readString();

How long is the timeout for readString?

Cheers, just had a search around and added that to my program, works super fast now!
Feel a bit daft for not doing a proper search first! Sorry and thanks!