Keypad

I'm new to this forum, but have some understanding of C.

First of all, can you program the Arduino(duemilanove) to read the keypad (basic number keypad as pictured on the site) from each pin?

If so, can I do this when I program to write the "code" send out a high? (to unlock whatever it is with the keypad)

int code0 = digitalRead(10)==HIGH; int code1 = digitalRead(2)==HIGH; int code2 = digitalRead(4)==HIGH; int code3 = digitalRead(6)==HIGH; int code4 = digitalRead(8)==HIGH; int code5 = digitalRead(12)==HIGH;

Entire code is attached if someone doesn't mind giving me any ideas.

Tomit: First of all, can you program the Arduino(duemilanove) to read the keypad (basic number keypad as pictured on the site) from each pin?

Btw, I know it doesn't seem logical, considering the way it is done on the site, but I don't have a multi meter on hand...

If you intend that code to unlock something, it will not work. There are several issues.

First, the int codeN statements in setup() are useless. They declare a set of local variables that contain the state of the button presses when setup runs. Since they are local variables, they go out of scope when setup ends, and the data in them is lost.

The loop function is called in an endless loop. When it is called, the state of all 6 buttons is checked very quickly. The code is not waiting for a key press to occur. It simply observes that some (0 to 6) keys are pressed. Then, it loops through all the pins to see if the current state of the button matches the previous state.

This all happens so fast that the answer is almost guaranteed to be yes. If it is, it turns pin 13 on and waits 10 seconds. In the rare event that it is not, it turns pin 12 off. Since pin 12 is never turned on, this seems like a useless thing to do.

Tomit: ...basic number keypad as pictured on the site...

What site? What keypad?

Is it something like the one shown here: http://arduino.cc/playground/Main/KeypadTutorial and here: http://www.arduino.cc/playground/Code/Keypad

There is code to show how to read individual key values. There is code to show how to "debounce" the key press events. You can use that as a starting point for writing a loop that does specific stuff for a particular sequence of key presses. The "Arduino" language is C++, so any good experience in C can be a great help in visualizing and implementing the code that you need.

If you have some other kind of keypad, give us some more information about what it is and how you have connected it.

Regards,

Dave

PaulS: If you intend that code to unlock something, it will not work. There are several issues.

First, the int codeN statements in setup() are useless. They declare a set of local variables that contain the state of the button presses when setup runs. Since they are local variables, they go out of scope when setup ends, and the data in them is lost.

Ok, I had a feeling they might be useless in that other scope.

PaulS: The loop function is called in an endless loop. When it is called, the state of all 6 buttons is checked very quickly. The code is not waiting for a key press to occur. It simply observes that some (0 to 6) keys are pressed. Then, it loops through all the pins to see if the current state of the button matches the previous state.

This all happens so fast that the answer is almost guaranteed to be yes. If it is, it turns pin 13 on and waits 10 seconds. In the rare event that it is not, it turns pin 12 off. Since pin 12 is never turned on, this seems like a useless thing to do.

Ok, so I understand what you're saying, and that makes sense as to why pin 13 is always on. Could you give me any clues as to what I would fix, and how to slow down the read, or to make the arduino wait for the input of the code to send out the HIGH only when the correct code is punched in? Thanks

davekw7x: What site? What keypad? Is it something like the one shown here: http://arduino.cc/playground/Main/KeypadTutorial and here: http://www.arduino.cc/playground/Code/Keypad

yes that's the exact one. sorry for the confusion.

davekw7x: There is code to show how to read individual key values. There is code to show how to "debounce" the key press events. You can use that as a starting point for writing a loop that does specific stuff for a particular sequence of key presses. The "Arduino" language is C++, so any good experience in C can be a great help in visualizing and implementing the code that you need.

In terms of reading individual key values, wouldn't digitalRead(6) work if each pin from the keyboard is going to a separate pin on the arduino? As for "debouncing" a key, I understand how the logic works, basically like discharging a capacitor when you press the key, and charging after you release. But how would this help in the programming? to give the user more time(by slowing down the code) to press the next key? and actually my small amount of experience is C++ I just wasn't thinking when I typed it. =P

Tomit: yes that's the exact one.

So: it is a matrix of switches. Read the playground articles to see how they really work.

Tomit: In terms of reading individual key values, wouldn't digitalRead(6) work if each pin from the keyboard is going to a separate pin on the arduino?

No. That would work if each switch had a separate contact to the Arduino and one signal that was a common connection to all of the switches. So, a 10-key keyboard would have ten input signal connections to the Arduino (plus the ground). A 12-key keypad would have 12 Arduino input pin connections. A 16-key keypad would require 16 Arduino input connections. Etc. The "matrix" approach requires fewer signal pin connections, but makes the software a little more "interesting."

With a matrix of switches, you have an Arduino I/O pin connection for each row of switches and a pin for each column of switches. So a 4x3 12-switch matrix requires a total of seven Arduino pins (number of rows + number of columns = 7).

Tomit: As for "debouncing" a key, I understand how the logic works, basically like discharging a capacitor when you press the key, and charging after you release. But how would this help in the programming?

If the keypad had some hardware debouncing circuitry, software wouldn't have to do anything special to detect keypress events. Simple keypads like the one shown in the playground articles do not have any such circuitry. They are simply a bunch of switches.

When you physically press or release a key, because of the physical properties of a given keyboard, the signal that the software uses to detect the state of the key might go from "inactive" to "active" a number of times (over a period of, typically, tens of milliseconds) before the signal settles down to its new state, so, unless the program takes this into account, a single keypress event might appear to be several "press-release-press..." events. The library code attempts to take care of that for you.

Regards,

Dave

davekw7x:
So: it is a matrix of switches. Read the playground articles to see how they really work.

Dang, I was hoping I could get away with the individual, guess not. Thanks for the help.

davekw7x:
No. That would work if each switch had a separate contact to the Arduino and one signal that was a common connection to all of the switches. So, a 10-key keyboard would have ten input signal connections to the Arduino (plus the ground). A 12-key keypad would have 12 Arduino input pin connections. A 16-key keypad would require 16 Arduino input connections. Etc. The “matrix” approach requires fewer signal pin connections, but makes the software a little more “interesting.”

With a matrix of switches, you have an Arduino I/O pin connection for each row of switches and a pin for each column of switches. So a 4x3 12-switch matrix requires a total of seven Arduino pins (number of rows + number of columns = 7).

I do have one other question about the matrix. How then would I read what “input code” I want the keypad to have, so the keypad will output a HIGH to a pin when the code is input correctly from the keypad?

davekw7x:
If the keypad had some hardware debouncing circuitry, software wouldn’t have to do anything special to detect keypress events. Simple keypads like the one shown in the playground articles do not have any such circuitry. They are simply a bunch of switches.

When you physically press or release a key, because of the physical properties of a given keyboard, the signal that the software uses to detect the state of the key might go from “inactive” to “active” a number of times (over a period of, typically, tens of milliseconds) before the signal settles down to its new state, so, unless the program takes this into account, a single keypress event might appear to be several “press-release-press…” events. The library code attempts to take care of that for you.

The library code you’re referring to that “attempts to take of that for” me would be the keypad library, correct? If so is there any way to view the keypad library? (I know this may be a stupid quesiton) Thanks.

You'll need to download it :)

Thanks I did download it. Still a bit confused about these cases…

char key = kpd.getKey();
if(key) // same as if(key != NO_KEY)
{
switch (key)
{
case ‘*’:
// does this mean that if I press the * key on the keypad, that this event below will occur?

digitalWrite(ledpin, LOW);
break;
case ‘#’:
// in that case, then this would mean the same for this case if I pressed the # key on the keypad then the event below this would occur

digitalWrite(ledpin, HIGH);
break;
default:
so the default will only occur if the first key pressed is not either the * or the # keys on the keypad?
Serial.println(key);
}

If I’m wrong about the way I’m understanding the code, please enlighten me. =) also how would I implement a “multiple key” code such as ‘*2468’ ?

does this mean that if I press the * key on the keypad, that this event below will occur?

Yes.

so the default will only occur if the first key pressed is not either the * or the # keys on the keypad?

Yes.

You could add cases for ‘0’, ‘1’, ‘2’, etc. if you wanted to do something other than return the key to the calling program.

also how would I implement a “multiple key” code such as ‘*2468’ ?

You need to have an array to store the keys in, and a counter for how many keys have been pressed, so you store the next key in the proper place.

char keys[5];
int keyCnt = 0;

while(keyCnt < 4)
{
   char key = GetKey();
   if(key != NoKey) // Check this value. It's been a while since I used the class
   {
      keys[keyCnt] = key;
      keyCnt++;
      keys[keyCnt] = '\0'; // Keep array NULL terminated
   }
}

// Now, see if the right sequence was entered
if(strcmp(keys, "1457") == 0)
{
   // Entre vous, por favor
}
else
{
   // Verbotten
}

The GetKey() function is a wrapper for the code you had in your previous post.

Note that you may want to have a reset key and a backspace key.

PaulS:

does this mean that if I press the * key on the keypad, that this event below will occur?

Yes.

so the default will only occur if the first key pressed is not either the * or the # keys on the keypad?

Yes.

You could add cases for ‘0’, ‘1’, ‘2’, etc. if you wanted to do something other than return the key to the calling program.

also how would I implement a “multiple key” code such as ‘*2468’ ?

You need to have an array to store the keys in, and a counter for how many keys have been pressed, so you store the next key in the proper place.

char keys[5];

int keyCnt = 0;

while(keyCnt < 4)
{
   char key = GetKey();
   if(key != NoKey) // Check this value. It’s been a while since I used the class
   {
      keys[keyCnt] = key;
      keyCnt++;
      keys[keyCnt] = ‘\0’; // Keep array NULL terminated
   }
}

// Now, see if the right sequence was entered
if(strcmp(keys, “1457”) == 0)
{
   // Entre vous, por favor
}
else
{
   // Verbotten
}




The GetKey() function is a wrapper for the code you had in your previous post.

Note that you may want to have a reset key and a backspace key.

Paul and everyone else who replied to this thread:

Thank you guys so much I appreciate all of your help, and Im sorry that my appreciation is a long time coming, but Im finally getting to it now. Im almost done with my capstone project for school which this will be included in. If i have any other questions, I’ll definitely let you know. Thanks again guys!

PaulS:

also how would I implement a “multiple key” code such as ‘*2468’ ?

You need to have an array to store the keys in, and a counter for how many keys have been pressed, so you store the next key in the proper place.

char keys[5];

int keyCnt = 0;

while(keyCnt < 4)
{
  char key = GetKey();
  if(key != NoKey) // Check this value. It’s been a while since I used the class
  {
     keys[keyCnt] = key;
     keyCnt++;
     keys[keyCnt] = ‘\0’; // Keep array NULL terminated
  }
}
[/quote]

[quote]
// Now, see if the right sequence was entered
if(strcmp(keys, “1457”) == 0)
{
  // Entre vous, por favor
}
else
{
  // Verbotten
}

what is strcmp? (store & compare)?

if(strcmp(keys, “1457”) == 0)

If (keys 1457 are pressed and equal to 0)
{then execute code}
else
{execute other code}

???

strcmp is string compare.

If (keys 1457 are pressed and equal to 0)

This is not the right interpretation of the statement. The correct interpretation: If the string of keys pressed, compared to "1457", is the same (not alphabetically higher or lower)...

Hello
I have a problem with my keypad in my program.
I took over the program you mentioned but then I had my pins 6 to 9 who were short-circuited, thus I saw the terminal number 1 and 2 appear.
http://www.arduino.cc/playground/Code/Keypad
I this program true?

So I changed my program with it I have no short-circuit but when I tapper a key on the keypad nothing appears, I would like to have your advice.

#include <Keypad.h>

const byte ROWS =4;
const byte COLS =4;
char keys[ROWS][COLS] = {
  {'1','2','3','F'},
  {'4','5','6','E'},
  {'7','8','9','D'},
  {'A','0','B','C'}
};
byte rowPins[ROWS] = {13,12,11,10};
byte colPins[COLS] = {9,8,7,6}; 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  char keyPressed = keypad.getKey();
  if(key != NO_KEY)
  {
    Serial.println(key); 
    delay(250);   
  }
}

sorry for my bad english.
Thank you in advance.

Good night.

http://arduino.cc/playground/Main/KeypadTutorial

Did you read that?

Yes of course

At first I didn't put pull-up resistors and then I put 4 of 10 K but nothing has changed