Receive String from Serial Monitor

Hello I am new here to this programming world
and I really want to learn
can someone explain to me what this programs means

while(Serial.available())
{
  //read the input
    delay(50);
    char c=Serial.read();
    readString+=c;
}
  if(readString.length()>0){
    Serial.println(readString);
if (readString =="ON"){
     digitalWrite(led, HIGH);
    }
    
    readString="";
  }

I am using it to light an LED using Bluetooth and I really want to figure how it works

while(Serial.available()) //all the time there is something to read
{
  //read the input
    delay(50);  //waste some time to allow characters to arrive
    char c=Serial.read(); //read a character
    readString+=c;  //add it to a String
} //keep reading until there is nothing more to read
  if(readString.length()>0){  //if something was actually read and added to the String
    Serial.println(readString); //print the String
if (readString =="ON"){ //if the String is "ON"
     digitalWrite(led, HIGH); //make the led pin HIGH
    }
   
    readString="";  //reset the value of the String to nothing
  }

The comments explain what it does.

Whether it is a good idea to do it this way is another matter. Expect more comments suggesting better ways to do this

UKHeliBob:
The comments explain what it does.

...and should have been there in the first place...

and should have been there in the first place...

Why ?

The code was, I assume, copied from somewhere. Do you comment every line of your code ? I think not

Expect more comments suggesting better ways to do this

The serial input basics tutorial has better ways to do serial receive. The methods in the tutorial do not use delay() (blocking code) nor do they use the String class that can cause hard to find memory bugs. Data is read into null terminated character arrays (strings) as they arrive. Then use the functions in string.h to compare, search, etc the strings.

HizonPastor:
Hello I am new here to this programming world
and I really want to learn
can someone explain to me what this programs means

while(Serial.available())

{
 //read the input
   delay(50);
   char c=Serial.read();
   readString+=c;
}
 if(readString.length()>0){
   Serial.println(readString);
if (readString =="ON"){
    digitalWrite(led, HIGH);
   }
   
   readString="";
 }



I am using it to light an LED using Bluetooth and I really want to figure how it works

I will take the help of the following conceptual level diagram (Fig-1) to explain how L (the built-in LED of Arduino UNO) becomes ignited when UNO receives the ON string/message coming from the InputBox of the Serial Monitor.
uartblk.png
Figure-1:

1. You enter ON in the InputBox of the Serial Monitor of Fig-1 and then click on the Send button. As a result, two asynchronous (async) frames (one after another) are transmitted towards UNO over the TXD line. An async frame is a 10-bit data frame that contains a STARTBit (LOW), 7-bit ASCII code for a character (1001111 for O), 1-bit padding bit (LOW), and a STOPBit (HIGH). In Fig-2, we have depicted the async frame for the character O.
asyncFrameX.png
Figure-2:

2. When a frame arrives at the UNO/MCU, the MCU discards the START and STOP bits and the remaining 8-bit data (01001111 = 0x4F) of the 1st frame is automatically saved in the 1st location (location-0) of the FIFO (first-in first-out) type BUFFer of the UNO/MCU.

3. Because the message/string ON has been sent from the InputBox of the Serial Monitor, the BUFF will contains ASCII codes of two charcaters which are O and N.

4. The Arduino IDE has the following instruction to check if one or more charcaters has/have arrived/stored in the BUFFer of the UNO/MCU.

byte n = Serial.available();   //n = 0 or non-zero

5. Because 2 charcaters have been sent from the Serial Monitor, the value of n in Step-4 would be 2. If the value of n = 2, then we will check that the arrived string is ON; if so, we will ignite L.

6. The task of Step-5 can be done by executing the following codes:

byte n = Serial.available();
if(n == 2)
{
     myData[0] = Serial.read(); //bring out O from BEFF and save in a char type array named myData[]
     myData[1] = Serial.read(); //bring out N from BUFF
     myData[2] = '\0';               //add null character with the array
     //---------check if the received string is ON----------------------
     if((strcmp(myData, "ON") == 0)  //if matching occurs strcmp() returns 0
     {
         digitalWrite(13, HIGH);   //ignite L
         memset(myData, 0, 4);  //reset the BUFFer
     }
}

uartblk.png

asyncFrameX.png

"can someone explain to me what this programs means"

Basically what is being done is the bytes/characters are being removed from the serial buffer one at a time and being put into a String so they can be back in the same form sent from the serial monitor. When you send "hello" from the serial monitor, each letter is sent individually to the arduino. At the arduino, they are reassembled back into "hello" for use.