How to make the keypad work for multiple variables

If the title is vague, I'm sorry. It's the best I can come up with. I'm trying to have my sketch take in an IP address and a subnet mask from the keypad. The pseudo-code looks sorta like this:

Input IP address
- If IP address is invalid (based off some validation code), re-input IP
- Else If IP address is valid, prompt for Subnet
Input Subnet Mask
- If Subnet is invalid (based off some validation code), re-input subnet
- Else If Subnet is valid, prompt for Default Gateway
Input Default Gateway
- If Default is invalid (based off some validation code), re-input gateway
- Else If Default is valid, do something else (like change IP address on Arduino)

The problem is, I only see one instance of KeypadEvent available to me, and between that and the getKey() section I'm pretty lost. I have the validation code in functions as IPValidator, SubnetValidator, and GatewayValidator.

Any ideas? :)

Post the code. Use code tags. Can't help you without details.

It might also help to know what kind of keypad you are using, and how it is wired up.

Unless you have more than one keypad, you don't need multiple instances of KeypadEvent. I don't know what library you are using, so that's about all I can help with now.

ckiick:
Post the code. Use code tags. Can’t help you without details.

It might also help to know what kind of keypad you are using, and how it is wired up.

Unless you have more than one keypad, you don’t need multiple instances of KeypadEvent. I don’t know what library you are using, so that’s about all I can help with now.

In order:
First, the code. Please keep in mind it’s me fooling around with it. I haven’t implemented the default gateway section yet.

/*  keypad and lcd
 *
 * this will go and take input from the keypad and show it on the LCD screen.
 *
 */
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <String.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>

// initialize keypad
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'.','0','E'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 17, 18, 19, 20 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 16, 15, 14 }; 
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Set up the LCD
LiquidCrystal lcd(22, 23, 24, 25, 26, 27);
// initialize ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xC1, 0x92 };

// #define ledpin 13
int count=0;
char NewIPAddress[16];
char enteredIPAddress[16];

void setup()
{
//  pinMode(ledpin, OUTPUT);
//  digitalWrite(ledpin, HIGH);
  Serial.begin(19200);
  pinMode (14, INPUT);//Column 1
  pinMode (15, INPUT);//Column 2
  pinMode (16, INPUT);//Column 3
  pinMode (17, INPUT);//Row 1
  pinMode (18, INPUT);//Row 2
  pinMode (19, INPUT);//Row 3
  pinMode (20, INPUT);//Row 4
  lcd.begin(16, 2);
  lcd.print("Obtaining IP...");
  Ethernet.begin(mac);
  lcd.clear();
  lcd.print("Enter IP:");
  lcd.setCursor(0, 1);
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.gatewayIP());
  Serial.println(Ethernet.subnetMask());
  kpd.addEventListener(keypadEvent);
  
}

void loop()
{
char key = kpd.getKey();
  if (key != NO_KEY)
  {
    switch(key){
    case 'E': break;
    default:
    
    
    lcd.print(key);
//    Serial.print(key);
//    count++;
    enteredIPAddress[count] = key;
    if (count==17)
    {
      lcd.clear();
      count=0;
      memset(enteredIPAddress, 0, (sizeof(enteredIPAddress)/sizeof(enteredIPAddress[0])));
      lcd.print("Enter IP:");
      lcd.setCursor(0, 1);
    }
   count++;
  }
  }

}

void keypadEvent(KeypadEvent key){
  switch (kpd.getState()){
    case PRESSED:
      switch (key){
        case 'E': 
         for(int i=0; i<16; i++)
   {
     Serial.print(enteredIPAddress[i]);
   }
     Serial.println();
           lcd.clear();
      count=0;
      lcd.print("Enter IP:");
      lcd.setCursor(0, 1);
      for(int i=0; i<16; i++)
   {
     Serial.print(enteredIPAddress[i]);

   }
String IPAddressString(enteredIPAddress);
Serial.print("New String = ");
Serial.println(IPAddressString);

   Serial.println();
       int IPTest = IPValidator(IPAddressString);
       Serial.print("IP Validator: ");
       Serial.print(IPTest);
       if (IPTest==0){
         Serial.println("Passed");
       }
       else {
         Serial.println("Failed");
       }
        break;
        break;
      }
  }
}
int IPValidator(String IPAddress)
// This will confirm the validity of a given IP address.
// It checks for octets and if the number is under 255.
// returns 1 if a problem, 0 if OK
{
// first, look for octets. Distinguished by a period.
// if we don't have three periods, bail. 
// If we do, record location. We'll need it later.
  int FirstOctetLoc = IPAddress.indexOf('.');
  int SecondOctetLoc = IPAddress.indexOf('.', FirstOctetLoc+1);
  int ThirdOctetLoc = IPAddress.indexOf('.',SecondOctetLoc+1);
  if (FirstOctetLoc == -1 || SecondOctetLoc == -1 || ThirdOctetLoc == -1) {
   return 1;
  }
  char IPAddressBuffer[IPAddress.length()];
  IPAddress.toCharArray(IPAddressBuffer,IPAddress.length());
// First Octet
  String FirstOctetString = IPAddress.substring(0,FirstOctetLoc);
  char FirstOctetChar[4];
  FirstOctetString.toCharArray(FirstOctetChar,6);
  int FirstOctet=atoi(FirstOctetChar);
// Second Octet
  String SecondOctetString = IPAddress.substring(FirstOctetLoc+1,SecondOctetLoc);
  char SecondOctetChar[4];
  SecondOctetString.toCharArray(SecondOctetChar,6);
  int SecondOctet=atoi(SecondOctetChar);
// Third Octet
  String ThirdOctetString = IPAddress.substring(SecondOctetLoc+1,ThirdOctetLoc);
  char ThirdOctetChar[4];
  ThirdOctetString.toCharArray(ThirdOctetChar,6);
  int ThirdOctet=atoi(ThirdOctetChar);
// Fourth Octet
  String FourthOctetString = IPAddress.substring(ThirdOctetLoc+1,IPAddress.length());
  char FourthOctetChar[4];
  FourthOctetString.toCharArray(FourthOctetChar,6);
  int FourthOctet=atoi(FourthOctetChar);
  if (FirstOctet > 255 || SecondOctet > 255 || ThirdOctet > 255 || FourthOctet > 255){
    return 1;
  }
  else{
    return 0;
  }
}

int SubnetValidator(String SubnetMask)
{
  // Subnet checking
// First see if we have 4 octets of information.
  int FirstSubnetOctetLoc = SubnetMask.indexOf('.');
  int SecondSubnetOctetLoc = SubnetMask.indexOf('.', FirstSubnetOctetLoc+1);
  int ThirdSubnetOctetLoc = SubnetMask.indexOf('.',SecondSubnetOctetLoc+1);
  if (FirstSubnetOctetLoc == -1 || SecondSubnetOctetLoc == -1 || ThirdSubnetOctetLoc == -1) {
    return 1;
  }
  char SubnetMaskBuffer[SubnetMask.length()];
  SubnetMask.toCharArray(SubnetMaskBuffer,SubnetMask.length());
// First Octet
  String FirstSubnetOctetString = SubnetMask.substring(0,FirstSubnetOctetLoc);
  char FirstSubnetOctetChar[4];
  FirstSubnetOctetString.toCharArray(FirstSubnetOctetChar,4);
  int FirstSubnetOctet=atoi(FirstSubnetOctetChar);
// Second Octet
  String SecondSubnetOctetString = SubnetMask.substring(FirstSubnetOctetLoc+1,SecondSubnetOctetLoc);
  char SecondSubnetOctetChar[4];
  SecondSubnetOctetString.toCharArray(SecondSubnetOctetChar,4);
  int SecondSubnetOctet=atoi(SecondSubnetOctetChar);
// Third Octet
  String ThirdSubnetOctetString = SubnetMask.substring(SecondSubnetOctetLoc+1,ThirdSubnetOctetLoc);
  char ThirdSubnetOctetChar[4];
  ThirdSubnetOctetString.toCharArray(ThirdSubnetOctetChar,6);
  int ThirdSubnetOctet=atoi(ThirdSubnetOctetChar);
// Fourth Octet
  String FourthSubnetOctetString = SubnetMask.substring(ThirdSubnetOctetLoc+1,SubnetMask.length());
  char FourthSubnetOctetChar[4];
  FourthSubnetOctetString.toCharArray(FourthSubnetOctetChar,6);
  int FourthSubnetOctet=atoi(FourthSubnetOctetChar);
// check against subnet list
  int firstsubnetreturnvalue = subnetchecker(FirstSubnetOctet);
  int secondsubnetreturnvalue = subnetchecker(SecondSubnetOctet);
  int thirdsubnetreturnvalue = subnetchecker(ThirdSubnetOctet);
  int fourthsubnetreturnvalue = subnetchecker(FourthSubnetOctet);
  if (firstsubnetreturnvalue == 1 || secondsubnetreturnvalue == 1 || thirdsubnetreturnvalue == 1 || fourthsubnetreturnvalue == 1) {
    return 1;
  }
  else{
    return 0;
  }
}
int subnetchecker(int TestNumber) 
// 0 if OK, 1 if failed
{ 
switch(TestNumber)
{
case 0: 
  return 0;
  break;

case 128: 
  return 0;
  break;

case 192: 
  return 0;
  break;

case 224: 
  return 0;
  break;
  
case 240: 
  return 0;
  break;
  
case 248: 
  return 0;
  break;

case 252: 
  return 0;
  break;

case 254: 
  return 0;
  break;

case 255: 
  return 0;
  break;
default:
  return 1;
}
}

And as for what library, it’s the defaults and are unmodified.

I noticed your using the case statements to see whether or not the test number is valid, why not just use the snippet MarkT gave you.

if (testNumber >= 0 && testNumber < 0x100)  // optionally check in 8 bit unsigned range
  {
    byte n = (byte) testNumber ;
    if (((n & -n) + n) == 256)   // clever part EDIT tweaked it
    {
      return 0;
    }
   else return 1;
}

HazardsMind: I noticed your using the case statements to see whether or not the test number is valid, why not just use the snippet MarkT gave you.

Heh... hadn't gotten around to adding it to the code. It's in, and fixed now. :)

    if( (byte)((n & -n) + n) == 0) // Now takes into account zeroes

is what the thing needed to be. :)