How to clear a string

I am trying to clear the string so as to send another command from Serial, is there any way to do that? Here is my code:

String data ("");
char data1= '7';
int see = 0;
void setup() {
  Serial.begin(9600);
}
void loop(){  
  if (Serial.available()) {
        if (see<7) {
          data1 = Serial.read();

data += data1;
   see ++;
  }
  else {
      see = 0;
      Serial.println (data);
  }

}
}

Just like you did when you first set up the variable! Only don't use the term "String", just name of the string.
Paul

I am returned with this error message:

Arduino: 1.8.13 (Linux), Board: "Arduino Uno"

The sketch name had to be modified.
Sketch names must start with a letter or number, followed by letters,
numbers, dashes, dots and underscores. Maximum length is 63 characters.
/home/pi/Arduino/Sketches/ard_to_ras_/ard_to_ras_.ino: In function ‘void loop()’:
ard_to_ras_:19:15: error: no match for call to ‘(String) (const char [1])’
       data ("");
               ^
exit status 1
no match for call to ‘(String) (const char [1])’


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

did you try data="";?

You might want to create a String buffer which anchors the String to a specific memory location instead of a free-floating memory hungry String.

After declaring the String

String MyStringThingy = "";

in setup() assign a memory size to the String.

MyStringThingy.reserve(100);

To put something into the String

MyStringThingy.concat( "someStringyStuff");

To clear the MyStringThingy.

MyStringThingy="";

What are you NOT using the same code you showed in your first post?

What do you mean?

The simple code line I suggested could not have caused all the error you listed.

This is the output when I send 'Hello!!' 5 times:

Hello!!
Hello!
!
Hello
!!
Hello
o!!
Hell

Are you sure? because all I changed was that line of code.

You asked how to clear a String. That you needed to ADD a line of code, not change a line of code. You already had the code to initially set up a String variable and that was correct and just fine. So where you wanted to clear a String somewhere in your code is where you want to set the String back to empty.
Just doing that could never create that error.
Paul

data = "";

This is the code that I used. It returned the previously mentioned error.

String data ("");
char data1= '7';
int see = 0;
void setup() {
  Serial.begin(9600);
}
void loop(){  
  if (Serial.available()) {
        if (see<7) {
          data1 = Serial.read();

data += data1;
   see ++;
  }
  else {
      see = 0;
      Serial.println (data);
      data ("");
  }

}
}

This new code only changes the previously mentioned code by adding the line after 'Serial.println (data);'. The line that is added is exactly the same as the edit you suggested be made. Try copying this code and compiling it yourself. Please then tell me if it compiles or returns with an error message.

Your idea may have worked killzone except that the serial monitor only prints out one reply, when I send another it doesn't reply. This is my code.

String data ("");
char data1= '7';
int see = 0;
void setup() {
  Serial.begin(9600);
}
void loop(){  
  if (Serial.available()) {
        if (see<7) {
          data1 = Serial.read();

data += data1;
   see ++;
  }
  else {
      see = 0;
      if (data == "Hello!!") {
        Serial.println ("Hi, I'm ready!");
      }
      if (data == "000Left") {
        Serial.println ("Turning Left!");
      }
      if (data == "00Right") {
        Serial.println ("Turning Right!");
      }
      if (data == "TakePic") {
        Serial.println ("Here's your picture!");
      }
      if (data == "IcSpeed") {
        Serial.println ("Increasing Speed!");
      }
      if (data == "DcSpeed") {
        Serial.println ("Decreasing Speed!");
      }
      data = "";
      Serial.println (data);
  }
}
}

Any suggestions?

line ending perhaps

I will never understand where you find inspiration for such "strange" algorithms :sweat_smile: :wink:

If you declare a String, use a String approach instead lose your mind with char lengths and reading bytes one by one.


void setup() {
  Serial.begin(9600);
  Serial.println();
}

void loop(){  
  if (Serial.available()) {
    String data = Serial.readString();
    data.trim();
    if (data == "Hello!!") {
      Serial.println ("Hi, I'm ready!");
    }
    if (data == "000Left") {
      Serial.println ("Turning Left!");
    }
    if (data == "00Right") {
      Serial.println ("Turning Right!");
    }
    if (data == "TakePic") {
      Serial.println ("Here's your picture!");
    }
    if (data == "IcSpeed") {
      Serial.println ("Increasing Speed!");
    }
    if (data == "DcSpeed") {
      Serial.println ("Decreasing Speed!");
    }
  }
  

}

What does that mean?

your console sends line ending which you don’t clear so instead of 7 chars you send 8 or 9