0

I have an ESP32 Wrover module with an ILI9341 QVGA Color TFT LCD Display with touch screen (the whole board was purchased from HackerBox, Specifically HackerBox50). I have setup a program that has a main menu of options and one of the options opens a Keypad function that requires a passkey to enter. when Prompted loop() calls the Canary_Setup() function, which will call the touchscreen calibration file and draw keypad() function. the keypad works fine and will send the entered passcode to the serial monitor but for some reason it won't trigger the if statement regardless of the passcode entered. the variable "numberBuffer" of type char is where the passcode is stored. when I check the Len of "numberBuffer it returns with the correct length yet it doesn't register properly in the if statement.

Variable declaration:

char numberBuffer[NUM_LEN + 1] = "";

If statement:

        if (b == 2) {
      status("Sent value to serial port");
      Serial.print(numberBuffer);
      if (numberBuffer == "2991") {
        Serial.println("hello");
        menu_return();
        break;
      } else {
        Serial.println("wrong");
      }

All other aspects of the code work without fault any ideas on what the problem could be; I am somewhat new to the Arduino/Hardware world so sorry if my code isn't clean.

1 Answers1

2

This line doesn't do what you think it does:

      if (numberBuffer == "2991") {

You cannot compare character arrays (which are also often referred to as lower case strings, and in this case this is how you're using them) in C/C++ using the == operator. You can compare String objects using it, but this is not a String object.

When you compare character arrays in C/C++ using == what you're comparing is the address of the array, not the contents. This will only be true if they are the exact same array - not just the exact same contents. Which when one is a string literal is unlikely, especially when the other is a character array (the value of that variable will be a constant memory address).

To compare character arrays you'll use the strcmp() function. This takes two character pointer arguments, assumes they point to null-terminated C strings, and compares what they point to. It returns 0 if they're the same, -1 if the left side is "less" than the right side (the way that "a" is less than "b"), and 1 if the left side is "greater" than the right side (the way "b" is greater than "a").

So this line of code should be:

      if (strcmp(numberBuffer, "2991") == 0) {
romkey
  • 6,218
  • 3
  • 16
  • 12