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?
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
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.
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.
#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.