Arduino Forum

Using Arduino => Programming Questions => Topic started by: ferkheusa on Apr 17, 2012, 07:23 am

Title: Keypad
Post by: ferkheusa on Apr 17, 2012, 07:23 am
Getting one digit from the keypad is easy, but how do you get two or three digits from the user (through the keypad) and use it to do something useful.
Title: Re: Keypad
Post by: dxw00d on Apr 17, 2012, 08:44 am
You read the keypad as many times as you need. Either a fixed number of times, or until a terminating character is entered. As keys are read, you store them, or add them together, in whatever way is appropriate. When you have a complete entry, act on it.
Title: Re: Turn keypad input to an integer
Post by: blackm0k on Apr 18, 2012, 07:23 pm
You just need to keep track of the number you are creating from the input. Every time you get a new input, multiply this by ten and add the new input to it. It's hard to create a solid example without more specifics, but something like the following should work:

Code: [Select]
int number = 0;

while( <more_input_required> )
{
  number *= 10;
  number += get_input();
}


Of course, you'll need to replace <more_input_required> with your own condition (no. of digits expected etc.) and replace get_input() with whatever function you call to get the next input.
Title: Re: Keypad
Post by: PaulS on Apr 18, 2012, 07:30 pm
Quote
Can someone give me some feedback on what I am doing wrong.

Sure. It's pretty simple, really. You have a Serial.begin() statement, but no Serial.print() statements, to tell what is happening.

Code: [Select]
  keypad.getKey();
The getKey() method returns a value. If you are not going to use the value, it makes no sense to call the method.

Some things to care about. Is the keypadEvent() function ever called? In that function, the event is not the same as the key. An event is like keyPressed (a key was pressed) or keyReleased (the key is no longer pressed). The password class is concerned about characters, not events.

Title: Re: Keypad
Post by: AWOL on Apr 18, 2012, 07:34 pm
I thought these all looked fairly similar, so I merged them.
Title: Re: Keypad
Post by: liuzengqiang on Apr 18, 2012, 07:39 pm

I thought these all looked fairly similar, so I merged them.


I thought I was reading up this post and then ... :)

I think the event is not actually interrupt driven as I learned from a recent post. The event is called every time getKey is called so it's a callback function (right?).

Not sure about the password library at all. Why do people use it instead of strcpy is beyond me. I do strcpy strcmp. It's simple.
Title: Re: Keypad
Post by: PaulS on Apr 18, 2012, 08:06 pm
Quote
Not sure about the password library at all. Why do people use it instead of strcpy is beyond me. I do strcpy. It's simple.

strcpy()? Or strcmp()?

The password class hides the complexity (?) of defining and initializing the array, adding the new character to the end of the array, and comparing the contents of the array against a predefined string.