Input Serial monitor

Hello, I have a question about a numeric input into the serial monitor.

If I have a password(numbers) in a character array and want to compare that with the input from the serial monitor.

Do I need to convert the input from the serial monitor to a character array. In other words, do I need the ASSCI character from the serial monitor to put into a char string? Then I compare the input with a char array that has the password in it?

Thnx in advance.

Is your password char array zero terminated so that it can be used as a C style string ? If so, then if you store the user input into a similarly terminated array of chars then you can use strcmp() to compare them

Take a look at Serial input basics - updated

The password will be sent and received as ASCII characters.
How you treat them after that is entirely up to you.

Thank you for your answers: I managed (I think to read the input from the serial monitor and compare it to a password). However, when Someone is using code 1234567 will also result in "Congratulation".
I cannot seem to figure out how to read only the first 4 characters from the Serial input and discard the rest.
Hope you can help me further.

int index = 0; 
char password[5] = "1234";
const int MaxChars = 4; 
char strValue[MaxChars+1]; 


void setup()
{
 Serial.begin(9600);
 
}
void loop()
{
  if( Serial.available())
 {
 char ch = Serial.read();
 if( index < MaxChars && isDigit(ch) ){
 strValue[index++] = ch; 
 }
 else
 {
 
 strValue[index] = 0;
 Serial.println(strValue);
  if(strcmp(strValue, password) == 0){
    Serial.println("Congratulations");
  }
  else{
    
     Serial.println("Try Again");
    
    }
 index = 0;
 }
 }
}
strValue[index] = 0;

Should that be '\0' to null terminate the string?

groundFungus:

strValue[index] = 0;

Should that be '\0' to null terminate the string?

Unfortunately, that doesn't solve the problem.

See this modification to your sketch which clears the input buffer after 4 characters

int index = 0;
char password[5] = "1234";
const int MaxChars = 4;
char strValue[MaxChars + 1];


void setup()
{
  Serial.begin(115200);

}
void loop()
{
  if ( Serial.available())
  {
    char ch = Serial.read();
    if ( index < MaxChars && isDigit(ch) ) {
      strValue[index++] = ch;
    }
    else
    {
       
      strValue[index] = '\0';
      Serial.println(strValue);
      if (strcmp(strValue, password) == 0) {
        Serial.println("Congratulations");
        //clear input buffer
        while(Serial.available()>0)
        {
          delay(5);//let all characters arrive
          Serial.read();
        }
      }
      else {

        Serial.println("Try Again");
         //clear input buffer
        while(Serial.available()>0)
        {
          delay(5);//let all characters arrive
          Serial.read();
        }

      }
      index = 0;
    }
  }
}

Are you sure you want to filter out non numeric keystrokes?

if( index < MaxChars && isDigit(ch) ){

That's kinda' like giving a hint. Let them put in anything. Because that means more area to guess at.

-jim lee

quoting someone somewhere, just in case you wonder…

“ there is no difference between

0

and

'\0'

except that they are different notations used in different contexts to denote conceptually different things even though both are stored as just 0 underneath the hood (in memory).”

I can’t begin to imagine how much code will break if that should ever become untrue.

a7

Here is a version using (my) SafeString library

#include <SafeString.h>
char password[] = "1234";
createSafeString(input, 10); //  to read input password, large enough to hold longest password + trailing delimiters
createSafeString(token, 10); // for parsing, capacity should be >= input
bool skipToDelimiter = false; // bool variable to hold the skipToDelimiter state across calls to readUntilToken()
// set skipToDelimiter = true to skip initial data upto first delimiter.
// skipToDelimiter = true can be set at any time to next delimiter.

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println();
  // SafeString::setOutput(Serial); // enable error and debug msgs
  Serial.println("Enter password");
}

bool passwordOK = false;
bool msgDisplayed = false;
void loop() {
  if (!skipToDelimiter) {
    msgDisplayed = false;  // reset try again after delimiter found
  }
  // returns true if delimiter found or if input is full, times out looking for delimiter in 0.5sec
  if (input.readUntilToken(Serial, token, "\r\n", skipToDelimiter, false, 500)) {
    token.debug();
    if (token == password) {
      passwordOK = true;
      Serial.println("Success!!");
    } else {
      Serial.println("Try Again");
    }
  } else {
    if ((skipToDelimiter) && (!msgDisplayed) ) {
      msgDisplayed = true;
      Serial.println("Try Again");
    }
  }
}

It avoids possible coding errors handling char arrays, indices and C-string methods. SafeStrings are completely safe and have detailed error checking, error messages and debugging.
It handles unlimited length inputs.
It is non-blocking and has no delay()'s
It handles all line terminations including none. If no chars received for 0.5sec the current input is auto terminated.

See detailed Arduino Serial I/O for the Real World and the SafeString tutorials and library examples for more details