Using Serial to implement a password using other than numbers

I am trying to implement a password for my program. I am a mechanical engineer by trade and am trying to do some small projects using arduino. Programming is not natural so any help would be appreciated. So i want to use a one word input as a password to get to the rest of my program and this is what i have so far. i can get the output i want but how do i use words instead of numbers and get the "ACCESS DENIED" to only print once with wrong input. Thanks again.

void setup()
{

  Serial.begin(9600);
  Serial.println(      "Enter ACCESS KEY to START"    );
}

void loop()
{
  while (Serial.available() == 0);
  if (Serial.available() - '0');
  {
    char ch = Serial.read();
    if (ch == '7')
    {
      Serial.println("ACCESS GRANTED");
    }
    else if (ch != '7')
    {
      Serial.println("ACCESS DENIED");
    }
  }
}
  if (Serial.available() - '0');

What the heck? Why would you be subtracting '0' from the number of bytes available to read? Why do you do nothing if the subtractions yields a true result (whatever that means)?

You can store the characters that you read in a char array, adding a NULL terminator after each addition, and use strcmp() to compare that array to a known value.

Thank you for pointing out the error in my program. That was left over from a write to display program to convert the numbers. anyway what i got from your post doesn't quit work for me. Here is what i got from it. Again any input would be helpful. i dont really know a lot about C++ programming so the more its dumbed down the better.

void setup()
{

  Serial.begin(9600);
  Serial.println(      "Enter ACCESS KEY to START"    );
}

void loop()
{
  while (Serial.available() == 0);
  if (Serial.available());
  {
    char Str1[7] = "aaaaaa"; 
    String.read(Str1[0]);
   
    if (strcmp(Str1,"Hunting") == true)
    {
      Serial.println("ACCESS GRANTED");
    }
    else 
    {
      Serial.println("ACCESS DENIED");
    }
  }
}
void setup()
{
  Serial.begin(9600);
  Serial.println(      "Enter ACCESS KEY to START"    );
}

void loop()
{
  while (Serial.available() == 0); // Useless. You wait while no serial bytes are available, 
  if (Serial.available());  // then you use Serial.available as a conditional
                            // Hint. At this point it will always be true.
                            // But it really doesn't matter, because the semicolon ends the if,
                            // so the following block of code will always execute
  {
    char Str1[7] = "aaaaaa"; 
    String.read(Str1[0]);
   
    if (strcmp(Str1,"Hunting") == true)
    {
      Serial.println("ACCESS GRANTED");
    }
    else 
    {
      Serial.println("ACCESS DENIED");
    }
  }
}

jspayne: Thank you for pointing out the error in my program. That was left over from a write to display program to convert the numbers.... ...

you need to have a very close look at what an if-statement looks like. pay attention to where the semi-colon ';' goes.

No matter if you are dealing with letters or number "if (Serial.available() - '0');" is not a sensible line of code. Also AFAIK

String.read(Str1[0]);

Is not a valid line of code.


Rob

The strings are only equal when the result is 0, not 1 or true.

  while (Serial.available() == 0);

This line needs a comment that explains that YOU know that it blocks until there is at least one character to read.

  if (Serial.available());
  {
    char Str1[7] = "aaaaaa"; 
    String.read(Str1[0]);

Aside from the issue with the semi-colon, that has already been pointed out, and the fact that the last line is pure crap, there is a fundamental problem with your approach to read serial data.

Serial data arrives like I type. Ssslllooowwwlllyyy. The Arduino can read it orders of magnitude faster than it can arrive. You need to send some kind of end of packet marker after the data that you want to read as the password. You need to, on each pass through loop(), see if there is any serial data to read. Do NOT block waiting for some to arrive.

If there is any, read a character. Is it the end of packet marker? If so, set a flag indicating that you can use the data that you have stored in a static of global array, NOT a local array. If not, add the character to the end of the array, and put a NULL after it.

Separately, see if the flag that says you can use the data is set. If it is, see if the entered password matches the stored password. If so, set another flag that says that you can do stuff.

Separately, see if the "you can do stuff" flag is set. If so, do stuff. If not, do nothing.