Keypad input into a string?

I'm looking to take keypad input and have the Arduino process it as a string. I can get individual keypad buttons to work, thanks to everyone here, but I want to be able to take a full string (specifically, an IP address, a subnet mask, and a default gateway), pass the info through some logic to make sure it's a valid IP, subnet, and gateway, and eventually have the Arduino run on that IP address.

The max string length would be 15 characters, if a predefined string length is required. Also, I've redefined the keypad so the "*" is a period, and the "#" is the "Enter key".

// 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'}
};

Any ideas? :)

A string is just a NULL terminated array of characters. With an index variable, where you keep track of the last position you wrote to, storing data in an array is trivial.

What have you tried?

PaulS:
A string is just a NULL terminated array of characters. With an index variable, where you keep track of the last position you wrote to, storing data in an array is trivial.

What have you tried?

I tried, and failed, doing the index variable thing. Here it is:

/*  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;
int NewIPAddress[16];
int 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());
  
  
}

void loop()
{
char key = kpd.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    Serial.print(key);
    count++;
    enteredIPAddress[count] = key;
    if (count==17)
    {
      lcd.clear();
      count=0;
      lcd.print("Enter IP:");
      lcd.setCursor(0, 1);
    }
  }
  else if (key='E')
  {
    Serial.println();
    for(int i=0; i<16; i++)
    {
      Serial.print(enteredIPAddress[i]);
      Serial.println();
    }
  }
}

What I wanted it to do was print to serial first, just so I can see the string.

if (key != NO_KEY) { lcd.print(key); Serial.print(key); count++; //No this will add 1 to count, and start enteredIPAddress at 1 and not 0 like it should/ ** enteredIPAddress[count] = key;** if (count==17) { lcd.clear(); count=0; lcd.print("Enter IP:"); lcd.setCursor(0, 1); } //count++ should go here }

    lcd.print(key);
    Serial.print(key);
    count++;
    enteredIPAddress[count] = key;

What is the value of count when you store the [u]first[/u] character in the array? (1) Is that what it should be? (No)

Since key is a char, why is the type of the array int? The values being stored in the array may be integral values in the proper range, but they really represent characters.

PaulS:
Since key is a char, why is the type of the array int?

Because I’m a noob and out of practice. :slight_smile: Last time I voluntarily worked with a C program was 1991, so I’m trying to catch up. :slight_smile:

Changing it to char worked. Putting the count++ at the end of that loop works as well. Here’s the code:

/*  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());
  
  
}

void loop()
{
char key = kpd.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    Serial.print(key);
//    count++;
    enteredIPAddress[count] = key;
    if (count==17)
    {
      lcd.clear();
      count=0;
      lcd.print("Enter IP:");
      lcd.setCursor(0, 1);
    }
   count++;
  }
//  else if (key='E')
//  {
//    break;
//  }
    Serial.println();
   for(int i=0; i<16; i++)
   {
     Serial.print(enteredIPAddress[i]);
   }
     Serial.println();


}

If you run this, you get serial output and see the string “expand”, and it matches the numbers on the LCD screen, but it just continually loops the entered IP address. I’m going to fool around with this a little more and see where I get. :slight_smile:

but it just continually loops the entered IP address.

So, once you get an IP address, you need to change where you are saving the data. Sounds like a finite state machine (that's a search term...) in the making.

but it just continually loops the entered IP address.

Ok so try this.

if (count==17)
    {
      lcd.clear();
      count=0;
      lcd.print("Enter IP:");
      lcd.setCursor(0, 1);
    }
   else count++; // note the change

Not that the change really matters, but once count does equal 17, it needs a way to get to here.

for(int i=0; i<16; i++)
{
Serial.print(enteredIPAddress*);*

  • }*
  • Serial.println();*
    [/quote]
    If anything, put it inside the IF statement.

I too have tried this and here’s how i could do it. I am trying to build a digital ic tester. For that i need to take the IC number from the matrix keypad as input. The code uploaded (blog.ino) shows the same.
Note: I tried only 4 digit IC numbers so far. And this is the first time i have replid to a post her. So if i have breached any protocol, xcuse me :stuck_out_tongue:

/*
The keypad library author:
Authors: Mark Stanley, Alexander Brevig
Contact: mstanley@technologist.com
Contact: alexanderbrevig@gmail.com */

#include <Keypad.h>

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’,’#’}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 24,25,26,27 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 30,31,32 };

int i=0;
char name[10]; //this stores the entered IC number

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{

Serial.begin(9600);
}
//lcd.clear();
void loop()
{
char key= kpd.getKey();

if(key!=NO_KEY) // Check for a valid key.
{
switch (key)
{
case ‘#’:
Serial.println();
name*=’\0’;*

  • Serial.println(“The number entered is”);*
  • for(i=0;i<4;i++)*
  • {*
    _ Serial.print(name*);_
    _
    }_
    _
    break;_
    _
    default:_
    _ name=key;
    i++;
    }
    }
    }
    Blog.ino (1.22 KB)*_