Serial.read

hello

i just want to know why when i use serial.read to take multiple inputs does not work

for example,

if (Serial.available() > 0) {

 if ( Serial.read() == 110)  // n in ASCII
     {  
        
       
        
      }



      
    if (Serial.read() == 104)  // h in ASCII
    {

      
       
          
    }


 if (Serial.read() == 108)  // l in ASCII
    {

      
       
          
    }


}

in this code i need first to insert n, then hh, and then lll to make it work

and i can not start by h then n and so on

any help here !

The read() method removes the character from the buffer. Every time you call read(), another character is gone.

So, if you send 'n', that gets read, and matches the if statement.

If you send 'h', that is read, and compared to 'n'. No match, so another character is read, and compared to 'h'.

I'm sure that you can see that that is NOT what you want to do.

   char c = Serial.read();
   if(c == 'n') // No useless comment needed
   {
   }
   else if(c == 'h')
   {
   }
   else if(c == 'l')
   {
   }

Read ONCE. Compare often.

PaulS:
The read() method removes the character from the buffer. Every time you call read(), another character is gone.

So, if you send 'n', that gets read, and matches the if statement.

If you send 'h', that is read, and compared to 'n'. No match, so another character is read, and compared to 'h'.

I'm sure that you can see that that is NOT what you want to do.

   char c = Serial.read();

if(c == 'n') // No useless comment needed
  {
  }
  else if(c == 'h')
  {
  }
  else if(c == 'l')
  {
  }



Read ONCE. Compare often.

so how can i read more than one time ? this is what i need

my purpose is to let the user to enter these three letters in different times, and depending on that choice i will write my code

my purpose is to let the user to enter these three letters in different times, and depending on that choice i will write my code

You need to write your code before it makes any sense for the user to enter anything.

If the user is supposed to enter some number of letters, read them, and act on them, in a for loop or a while loop.

while(Serial.available() > 0)
{
   char c = Serial.read();
   if(c == 'n') // No useless comment needed
   {
   }
   else if(c == 'h')
   {
   }
   else if(c == 'l')
   {
   }
}

Code snippets aren't nearly as helpful as the entire sketch. Also, in your code, you're going out of your way to make it hard to read by using the ASCII numbers. Also, what if the character comes in as 'N' rather than 'n'? Finally, if you do get an 'n' you still perform checks to see if it's something else...not good. What about:

while(Serial.available() > 0)
{
   c = tolower(Serial.read());      // Make sure it's lower case
   switch (c) {
      case 'n':
         // whatever...
         break;

      case 'h':
         // whatever...
         break;

      case 'l':
         // whatever...
         break;

      default:
         Serial.print("I shouldn't be here. c = ");
         Serial.println(c);
         break;
   }
}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R