Keypad events returning different data

I’m trying to work on a sketch that you can enter IP information like address, subnet, and gateway, into an Arduino via keypad. I also have validation code in so the IP address is valid. It also has an LCD to show which one needs to be entered.

Because I needed the KeypadEvent and the getKey() functions to work with all three items, I have a bunch of if-else statements in (see attached. I’m up to 300 lines now) and now, after I put in the IP address, it provides the prompt from the first if statement, but the program takes in the subnet mask. It’s “shifted by one”, effectively. And, if I intentionally put in a bad IP address, it rips through the rest of the validation steps, without even prompting on the LCD screen.

Why did this happen, and how do I fix it?

keypad_lcd7.ino (10.1 KB)

My first question is whether you want the keypad to trigger a callback or if you want to explicitly manage events yourself. You are doing both in the code, which is possibly why you are having problems. Decide on ONE approach, and delete the other method of handling events.

You need a state variable that defines whether you are entering an IP address, a subnet mask, or a gateway mask. That state variables does NOT depend on having entered a valid IP address, subnet mask, or gateway mask, unless you reset the state and array index when an invalid value for one of the fields is entered.

To answer your questions:

  • callback or explicitly manage. I think I want to explicitly manage the events, mainly because of what my end result is going to be.
  • delete the other method. I think I just want to run the KeypadEvent, and take the getKey code and work it into the KeypadEvent. Am I correct?

I was going to ask about what a state variable was, but I just read up on it. :slight_smile:

I think I want to explicitly manage the events, mainly because of what my end result is going to be.

Then, you don't want keypadEvent (the callback that you registered).

I think I just want to run the KeypadEvent, and take the getKey code and work it into the KeypadEvent. Am I correct?

No.

I’m now integrating the components one by one into the getKey section. I’m attaching my validated, but not particularly working, code. The next two questions are:

  • I don’t think I have the concept of “state variables” down. I just used three boolean variables to set the stuff up.
  • I can’t seem to get the program to either prompt again if it fails its validation test, or move to the next variable if the first variable is OK.

keypad_lcd8.ino (10.2 KB)

    while (IPAddressCheck == false) {
      Serial.print("Inside IP address check. E pressed. IP info is: ");
      Serial.println(enteredIPAddress);
      IPAddressString = enteredIPAddress;
      lcd.clear();
      count=0;
      Serial.println("Inside getKey case / switch");
      IPAddressCheck = IPValidator(IPAddressString);
      Serial.print("IP Validator: ");
      Serial.println(IPAddressCheck);
      if (IPAddressCheck == true) {
        lcd.print("IP Address Valid");
        delay(250);
        lcd.clear();
        lcd.print("Enter Subnet2:");
        lcd.setCursor(0, 1);
      }
      break;
    }

What is the point of using a while loop that contains an unconditional break that will be encountered on the first iteration?

I'm attaching my validated, but not particularly working, code.

But not the serial output or an explanation of what the problem really is.

In the "E" case, you should have another switch statement, based on the state variable (entering IPAddr mode, entering gateway mode, or entering subnet mode).

When you successfully enter an IP address, change the state variable's value to the next mode.

What do you do when IPValidator returns false when checking the IP address? Is that sufficient?

Use Tools + Auto Format, please. That indenting is awful.