[PROBLEM] String manipulation

Hi ,

I have a problem with string manipulation. It's not that I don't know how to do it but I would like to know the best way to do this.

This is the problem :

So I'm currently using the Arduino Genuino/Uno ( starter kit ). I made A pcb with an analog stick , according how I move the analog stick It sends out a value with a transmitter. Example :

If I move the analog stick forward it will send "V123" , backwards it will send "A10" , left it will send "L98" and right "102"

The numbers are not constant ofcourse like I said they change according how much I move the analog stick :).

So I want to control a motor with these values. But I want to split the string into V(Direction wich the motor turns) and the value ( the numbers that come behind). This value I would like to analogwrite to the motor.

I hope you understand what I mean and I apologies for my bad english.

The code :

String inputString = "";         
boolean stringComplete = false;  
String BeepString="";
void setup() {
  // initialize serial:
  Serial.begin(2400);
   pinMode(LED_BUILTIN, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
}

void loop() {

  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }


if (inputString.startsWith("BEEP", 1)) {
  digitalWrite(LED_BUILTIN,LOW);
  tone(8,1000);
  delay(100);
  noTone(8);
  inputString="";
}


if (inputString.startsWith("V", 1)) {
  digitalWrite(2,HIGH);
  digitalWrite(2,LOW);
   inputString="";
}

if (inputString.startsWith("A", 1)) {
  digitalWrite(3,HIGH);
  digitalWrite(3,LOW);
   inputString="";
}

if (inputString.startsWith("L", 1)) {
  digitalWrite(4,HIGH);
  digitalWrite(4,LOW);
   inputString="";
}

if (inputString.startsWith("R", 1)) {
  digitalWrite(5,HIGH);
  digitalWrite(5,LOW);
   inputString="";
}
}


void serialEvent() {
  while (Serial.available()) {

    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;

    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

according how I move the analog stick It sends out a value with a transmitter.

I am sorry to point this out, but your code does not seem to read an analogue value

Yes , but that is the point I want to split the string example : “V123” to V + 123 and the number 123 I want to convert to an integer and then put this value to the motor

So what does the code you posted do ?

Note that you refer to both strings (a zero terminated array of chars) and Strings (an object created using the String library)

If I move the analog stick forward it will send “V123” , backwards it will send “A10” , left it will send “L98” and right “102”

Why? Sending “<V, 123>” or “<A, 10>” or “<L, 98>” or “<R, 102>” would make reading, storing, and parsing the data so much easier.

First, it's usually not a good idea to mix the String class (note capital 'S') with C strings (lowercase 's'). Also, while convenient and easy to use, they likely chew up more resources that regular C strings would. I would change the definition of inputString to:

  • char inputString[10]; // It could be smaller: DXXX, so 5 bytes?*

You would likely have to change the processing throughout the program. This might be an alternative for your serialEvent() function:

void serialEvent() {
  int charsRead;

  while (Serial.available()) {
    charsRead = (char)Serial.readBytesUntil('\n', inputString, sizeof(inputString) - 1);
    inputString[charsRead] = '\0';  //Overwrite the newline to make it a C string
   } 
   Serial.println(inputString);     // Debugging so you can see what was entered
}

Now you can use inputString[0] as the direction and the rest as the vector size, You could use:

  • vectorSize = atoi(&inputString[1]); // The numeric part*

Finally, use Ctrl-T on the source code while it is in the IDE. It will reformat your code to a popular C coding style that's easier for us to read.

Thank you PaulS I'm considering to send "," to.

Thank you econjack your way is working fine I can finally send PWM to my motor