Need advice on converting serial to string

What I'm trying to do is change a variable through incoming serial data, right now I'm only able to add to an ever growing string. In the code below I'm trying to send serial data to my arduino reading "Red" or "Green" or "Blue" to change a string of leds between red, green and blue. However, using the code below the string will go from "Red" to "RedBlue" and so on. Any advice on this would be much appreciated, I'm stumped on how to proceed with this. Thanks!

//Serial variables String content = ""; String input = ""; char character;

// Main loop void loop() { //Provide serial data in Serial monitor if (Serial.available()) { character =; content.concat(character); Serial1.write(character); } if (Serial1.available()) { character =; content.concat(character); Serial.write(character); }

if (content != ""){ input = content; }

if (input == "Red"){ Visor.ColorSet(Visor.Color(255, 0, 0)); } if (input == "Green"){ Visor.ColorSet(Visor.Color(0, 255, 0)); } if (input == "Blue"){ Visor.ColorSet(Visor.Color(0, 0, 255)); } }

First, welcome to the Forum. Second, please read the two posts by Nick Gammon at the top of the Forum for guidelines on posting here, especially the use of code tags when posting source code. Third, get rid of the String class. It is a crutch and uses more memory than need be.

Work through this code until you understand it:

void setup() {
  // put your setup code here, to run once:


void loop() {
  char buffer[10];
  int charsRead;

  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    buffer[charsRead] = '\0';    // Now it's a string
    Serial.print("Input is ");

    if (strcmp(buffer, "Red") == 0) {
      //Visor.ColorSet(Visor.Color(255, 0, 0)); 
    } else 
    if  (strcmp(buffer, "Green") == 0) {
     // Visor.ColorSet(Visor.Color(0, 255, 0));    
     } else 
     // Visor.ColorSet(Visor.Color(0, 0, 255));    

I commented out the Visor class calls because I don't have that library. After you understand it, change it so it still works if someone types in "RED" instead of "Red".

The examples in Serial Input Basics are simple and reliable. They read the incoming data into a char array (a string - small s). There is also a parse example.


At some point, you probably want to set content back to an empty string. It's silly to make a copy of content that you test. There is no reason not to test content.

What I'm trying to do is change a variable through incoming serial data, right now I'm only able to add to an ever growing string.

A very simple example of capturing a string of characters sent from the serial monitor into a String for further processing.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track

void loop() {

  while (Serial.available()) {
    char c =;
    readString += c; 

  if (readString.length() >0) {

    if (readString == "on")     
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    if (readString == "off")
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");