Skip to main content
Delete extra blank lines, indent code
Source Link
user31481
user31481

I wrote following program, it receives 5 character input from user and if user enters input more than 5 characters then program store only 1st 5 characters and if user inputs less than 5 characters long the it rejects than input altogether..

char userInput[5];
 
 

 void setup()
 
{
 
//delay(1000);
 
Serial.begin(9600); // begin serial port with baud rate 9600bps

 
 
}

void loop()
 
{
 
if (Serial.available() < 5)
 
{
 
   Serial.read();
 
}
 
else if (Serial.available() >= 5)
 
{
 
      for (int i = 0; i < 5; i++)
 
      {
 
        userInput[i] = Serial.read();
 
      }
 
 

      while (Serial.available() > 0)
 
     {
 
       Serial.read();
 
     }
 
 

 Serial.print("User Input - ");
 
Serial.println(userInput);
 
}
 
 

 delay(100);
 
}

I wrote following program, it receives 5 character input from user and if user enters input more than 5 characters then program store only 1st 5 characters and if user inputs less than 5 characters long the it rejects than input altogether..

char userInput[5];
 
 

 void setup()
 
{
 
//delay(1000);
 
Serial.begin(9600); // begin serial port with baud rate 9600bps

 
 
}

void loop()
 
{
 
if (Serial.available() < 5)
 
{
 
Serial.read();
 
}
 
else if (Serial.available() >= 5)
 
{
 
for (int i = 0; i < 5; i++)
 
{
 
userInput[i] = Serial.read();
 
}
 
 

 while (Serial.available() > 0)
 
{
 
Serial.read();
 
}
 
 

 Serial.print("User Input - ");
 
Serial.println(userInput);
 
}
 
 

 delay(100);
 
}

I wrote following program, it receives 5 character input from user and if user enters input more than 5 characters then program store only 1st 5 characters and if user inputs less than 5 characters long the it rejects than input altogether..

char userInput[5];
void setup()
{
//delay(1000);
Serial.begin(9600); // begin serial port with baud rate 9600bps
}

void loop()
{
if (Serial.available() < 5)
{
   Serial.read();
}
else if (Serial.available() >= 5)
{
      for (int i = 0; i < 5; i++)
      {
        userInput[i] = Serial.read();
      }
     while (Serial.available() > 0)
     {
       Serial.read();
     }
Serial.print("User Input - ");
Serial.println(userInput);
}
delay(100);
}
Source Link

I wrote following program, it receives 5 character input from user and if user enters input more than 5 characters then program store only 1st 5 characters and if user inputs less than 5 characters long the it rejects than input altogether..

char userInput[5];

 

void setup()

{

//delay(1000);

Serial.begin(9600); // begin serial port with baud rate 9600bps

 

}

void loop()

{

if (Serial.available() < 5)

{

Serial.read();

}

else if (Serial.available() >= 5)

{

for (int i = 0; i < 5; i++)

{

userInput[i] = Serial.read();

}

 

while (Serial.available() > 0)

{

Serial.read();

}

 

Serial.print("User Input - ");

Serial.println(userInput);

}

 

delay(100);

}