How to display the text entered via serial monitor

This is simple but I don't know how to. I just want to send some text to process, for example, i wanna send the text "on" to switch the led and "off" to turn it off but when i do this with this code

const int LEDPIN 7;
String myTxt;

void setup() {
   Serial.begin(9600);
   pinMode(LEDPIN, OUTPUT);
}

void loop() {
   if(Serial.available() > 0) {
      char c = Serial.read();
      myTxt += c
      Serial.println(myTxt);

      if(myTxt == "on") {
         digitalWrite(LEDPIN, HIGH);
      }

      if(myTxt == "off") {
         digitalWrite(LEDPIN, LOW);
      }
   }
}

all I always get from the serial monitor is something like this. Suppose that I enter "12345"

1
12
123
1234
12345

The string get bigger in size, so it is useless...

please help me get through this...

if( myTxt == "on")
Evaluates the myTxt for string "on" and will be true if the string is ONLY "on".
Using Monitor enter "on" together then do Send, the Serial.read() will read one character at a time and your code will add them together doing myTxt +=c .

You may want to experiment with substring so you can "pick out" your "on" string from other characters.
Such as "123on456".

BTW your code as posted did not compile.

Cheers Vaclav

int LEDPIN = 7;
String myTxt;

void setup() {
   Serial.begin(9600);
   pinMode(LEDPIN, OUTPUT);
}

void loop() {
   if(Serial.available() > 0) {
      char c = Serial.read();
      myTxt += c;
      Serial.println(myTxt);

      if(myTxt == "on") {
        Serial.print("Turn on ");
         digitalWrite(LEDPIN, HIGH);
      }

      if(myTxt == "off") {
         digitalWrite(LEDPIN, LOW);
      }
   }
}

I'm not a big fan of the String class, as it adds a lot of bloat to most programs. Here's an alternative; my guess is that it's smaller than the alternative:

#define MAXBUFF  20

const int LEDPIN = 13;
char message[MAXBUFF];

void setup() {
  Serial.begin(9600);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  int charsRead;
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', message, MAXBUFF - 1);   // Gather the input
    message[charsRead] = '\0';      // Make it a C string
    Serial.println(message);
  }
  if (strcmp(message, "on") == 0)      // Did it say on?
  {
    digitalWrite(LEDPIN, HIGH);
  } else {  
    if (strcmp(message, "off") == 0)      // or off
      digitalWrite(LEDPIN, LOW);
  }
}

Thank you very much for helps... I have tried and it works.

An alternative.

// 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() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}