Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: wodm on Sep 06, 2015, 06:59 pm

Title: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 06:59 pm
Hi All

I have searched but cant find the answer so thought i would ask.

I am new to arduino and coding so would love some help and / or advice regarding membrane button pads.

I have a 4x3 pad and have it connected to my arduino micro (have also had it on my uno and teensy 3.1)
I can see my button press registering in the serial monitor but cant figure out how to make them register in say.. notepad for example.

Is it possible or am i missing a setting in the IDE? i have the kepad library etc but i am lost now.

Ultimately, i want to have simple control over a couple of in game commands and would rather use the membrane than build a button panel.

Thanks in advance for any help.

Code: [Select]
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>

const byte ROWS = 3; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'3','6','9','#'},
  {'2','5','8','0'},
  {'1','4','7','*'}
};
byte rowPins[ROWS] = {6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10, 9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char key = keypad.getKey();
 
  if (key){
    Serial.println(key);
  }
}
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 08:14 pm
Quote
Is it possible
Not unless you can figure out how to install Notepad on the Arduino.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 08:18 pm
ok, joking aside, (i assume you were making a joke) i want to press a button and it read as a keystroke on my pc. It doesnt have to be notepad.
The button press is registering in serial monitor because i can see it but is there a way for it to be read as a keystroke on the pc itself?

As i said, i am a complete newbie to this and was after some help.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 08:28 pm
Which Arduino do you have? Only the discontinued Leonardo and the Micro have the hardware to send keystrokes. The others can not.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 08:32 pm
Which Arduino do you have? Only the discontinued Leonardo and the Micro have the hardware to send keystrokes. The others can not.
I have 2 officail arduino. 1 uno r3 and 1 micro. i also have a teensy 3.1
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 08:34 pm
The teensy or the Micro will work. So, what is the problem?
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 08:40 pm
The teensy or the Micro will work. So, what is the problem?
im not sure, i have posted the code i am using. when i open the serial monitor, it works but i cant figure out why it does nothing in the pc itself. I may have an incorrect setting orsomething but i just dont know.

Am i right in thinking that if i am in any text editor, then the button press should show as a number in the editor?

board is set to micro
programmer is USBtinyISP
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 10:06 pm
Quote
but i cant figure out why it does nothing in the pc itself.
Because you are not sending keystrokes. THAT is done with the Keyboard library.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 10:42 pm
so is it doable? feel free to speak to me like a child, i really do know almost nothing.

if it is not feasible with the membrane button pad then i will build a board

Thanks
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 10:48 pm
Quote
if it is not feasible with the membrane button pad then i will build a board
It IS feasible to get data from the button pad. But, what, EXACTLY, do you want to do with that data? Have you looked at the Keyboard library examples?
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 10:56 pm
i have yes and to be honest, i though i was doing it correctly but it appears i wasnt.

The purpose of it for me, is to be able to map the numbers on the pad to in game commands on a racing game so i can turn my lights on/off winscreen wipers on/off etc.
I could use my keybord but if i can get this working, it will be a much better solution (space wise at least)

http://www.elecfreaks.com/estore/sealed-membrane-3-4-button-pad-with-sticker.html

Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 06, 2015, 11:23 pm
You did not answer any of my questions. What are the labels on the keys? If the match what is in the code, what, EXACTLY, should happen when the '3' key is pressed?

If you want to execute a Keyboard.press('3'); when that happens, why don't you?
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 06, 2015, 11:36 pm
i can map in game controls to any key on my keyboard
an example would be keypad 3 turns my lights on / off
i would like to press number 3 on the button pad (the one in the earlier link) and have it register as the keyboard number 3
in my mind it sounds simple but i am not a coder so it would sound simple to me ;)

i can adjust my seating position in game using wasd keys, i could remap them to 2,4,6,8 on number pad.

As i sit in a racing seat, i cant really reach my keyboard so this stick on pad would be an excellent replacement.

I have installed the keypad library and all works well within the arduino IDE but i am having trouble once i try to do anything outside of this.

I appreciate your patience and thank you for it
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 01:39 am
Quote
I have installed the keypad library and all works well within the arduino IDE but i am having trouble once i try to do anything outside of this.
Why are you trying to do anything outside of the IDE?
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 07, 2015, 02:03 pm
Why are you trying to do anything outside of the IDE?
im not, i am trying the code inside and the serial monitor shows my keystrokes. I want these keystrokes to show as actual keystrokes on the pc (ie. if i have my curser in the google search bar and i press 1 on my numberpad, i want it to show in the serch bar)

Not sure i have explained correctly. i want to control a game with it or at least an option within the game. i just want to press a button and it register as a keystroke.
i know this can be done with buttons and wiring and breadboards etc, i have seen videos but i can not get my button pad to register as a keystroke and see it as such anywhere outside of the serial monitor.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 02:27 pm
Quote
and the serial monitor shows my keystrokes
No, it does not. It shows some text sent by the Arduino. That is NOT the same as a keystroke.

The Serial pot is useful to get the code to the point where it can properly read from the keypad. Since you have that working, you now need to stop using Serial, so that the Keyboard class can talk to the PC, in a manner that does not use Serial.

Instead of using Serial.print(key), you will use Keyboard.press(key), a small delay, and Keyboard.release(key);. Look at thhe Keyboard examples, as I have said repeatedly.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 07, 2015, 02:29 pm
Thank you again
i shall give this a quick try in a minute then sadly, its of to work i go.
I will report back and let you know how much of a start you have been
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 02:41 pm
Quote
I can't read the music format.
That's the Disney theme song. " I owe, I  owe, so off to work i go...".
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 07, 2015, 03:06 pm
i think i owe you a coffee :)

123456789*0£

all typed with the number pad. only problem is the £ is actually a # on the pad but that easily fixed (keyboard setting maybe?)

Anyway, i removed  Serial.print(key) and replaced it with
Code: [Select]
Keyboard.press(key);
    delay; 500,
    Keyboard.release(key)


Thank you for your help (and patience)
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 03:21 pm
Quote
Anyway, i removed  Serial.print(key) and replaced it with
I hope you mean "something like", because there are problems with that snippet.

Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 07, 2015, 03:39 pm
I hope you mean "something like", because there are problems with that snippet.


what have i done wrong?
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 05:07 pm
The snippet should be:
Code: [Select]
    Keyboard.press(key);
    delay(500);
    Keyboard.release(key);


The last line was missing a ;. The middle one was not calling delay(). The 500, bit was just weird.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: PaulS on Sep 07, 2015, 05:09 pm
By the way, holding the key pressed for half a second might be way too long. I'd be using 10, not 500, as the key down time.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 07, 2015, 05:53 pm
I see, learning all the time. In work now but will alter my sketch once home.
It's starting to make sense now.
Title: Re: 4x3 membrane keypad as input [help please]
Post by: wodm on Sep 08, 2015, 01:24 am
home from work, tested and working.
Just tried in game to assign controls and it works without issue.

Thanks for your help and thank you for not just giving me the answer either ;)