HCMatrixkeypad - Matrix keypad library

Useful guides, libraries, and example sketches to support our Arduino based products.
andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Fri Aug 19, 2022 8:20 am

That would be a bit difficult to do because as this is a generic keypad library (not just for our keypads), it would somehow have to know the key mapping of the actual keypad you're using. The most memory efficient way to do this would be in your sketch as it then can be tailored to your keypad.

Here is an example (I don't have a keypad to hand right now so this is untested) for the 4x4 membrane keypad (HCPROJ0001) we sell for how to convert the row/col value returned by the read() function to the actual value printed on its keys:

Note, the key2val() function may look a bit long winded but as you're only mapping 10 keys it's actually more memory efficient to do it this way then have a lookup table and a loop function...

  1.  
  2. void setup()
  3. {
  4.     Serial.begin(9600);
  5. }
  6.  
  7. /* Main program */
  8. void loop()
  9. {
  10.   Keypad.Scan();
  11.  
  12.   if(Keypad.New_Key())
  13.   {
  14.     byte keyVal = key2val(Keypad.Read());
  15.  
  16.     if(keyVal != 255)
  17.       Serial.println(keyVal);
  18.   }
  19. }
  20.  
  21.  
  22. // Convert row/col index to key value
  23. byte key2val(byte key)
  24. {
  25.   byte val = 255;
  26.  
  27.   if(key == 11)
  28.     return 1;
  29.   if(key == 12)
  30.     return 2;
  31.   if(key == 13)
  32.     return 3;
  33.   if(key == 21)
  34.     return 4;
  35.   if(key == 22)
  36.     return 5;
  37.   if(key == 23)
  38.     return 6;
  39.   if(key == 31)
  40.     return 7;
  41.   if(key == 32)
  42.     return 8;
  43.   if(key == 33)
  44.     return 9;
  45.   if(key == 42)
  46.     return 0;
  47.   return val;
  48. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Fri Aug 19, 2022 3:03 pm

Good Afternoon Andrew,

Thanks for the reply. In fact I had already done a similar thing but could not 'test' as I was awaiting the keypad to arrive. [I think I had mentioned this before, but an 'Age Issue' means I now tend to ask before thinking, so my apologies. ]

I worked using you '.cpp' code that 3 x 4 KP was ( did not need any more ):-

R 1 [ 11 : 12 : 13 ]
R2 [ 21 : 22 : 23 ]
R3 [ 31 : 32 : 33 ]
R4 [ 41 : 42 : 43 ]

Anyway, keypads arrived, code tested, working well. Simply Store the 1st key, wait for the 2nd ( or the '#' ). I'm sure you know how my MATHS is working here. Using '#' as ENTER ( ret = 43 ) - means I know if 0 - 9[1 digit] or 10 - 99[2 digit], and '*' ( ret = 41 ) as SAVE ( Relay settings, Power-Up reload last saved setting ).

Working well.

Thanks a lot, Cheers, S

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

HCPROJ0002 - Row & Column Pinout

Post by RetroBoy » Tue Sep 13, 2022 6:36 pm

Hi,

I have the Keypad attached as per the Pinout you have described ( 2,3,4,5 - 12,11,10,9 ) working brilliantly on an UNO.
The problem I have is when I Upload to a NANO, it's a NO GO! Half the keys are not recognized, and it is so slow.
I have tried 3 different NANOs just to be sure I didn't have a dudun', but always the same results.

Does the NANO need a different PINOUT, or is there something else I am missing?

Any help on this appreciated.

Regards S.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Tue Sep 13, 2022 9:19 pm

Hi Andrew,

First ... Wiring problem with NANO solved. One of the Dupont Pins was loose in the connector ( I have said before about 'Age' Issues :oops: ).

However ... speed is still an issue. Don't know why it should be so slow running on an NANO when the UNO is there as soon as I enter any value(s).

Regards S.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Wed Sep 14, 2022 7:38 am

Does the NANO need a different PINOUT, or is there something else I am missing?
Nope, both the Nano and the Uno use ATMega328p's running at 16MHz and the keypad library doesn't care what it's running on. I.e. it doesn't alter the way it works based on the board you're using. In fact, a Nano can just be treated as a cut-down Uno in a more breadboard friendly DIP format.


First ... Wiring problem with NANO solved. One of the Dupont Pins was loose in the connector ( I have said before about 'Age' Issues :oops: ).

However ... speed is still an issue. Don't know why it should be so slow running on an NANO when the UNO is there as soon as I enter any value(s).
Although unlikely, I'd first try running the blink sketch (File->examples->01.Basics->Blink) just to sanity check your Nano. Check to see that the LED blinks on for 1 second and off for 1 second and not for example on and off for 2 seconds or more.

By slow, do you mean you press *and* release a key and then there's a delay before you see the value, or do you mean you have to hold a key down for a while before it registers the key press?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Wed Sep 14, 2022 11:06 am

Good Morning Andrew,

Thanks for the reply.

The Speed Issue is a General Issue. Everything on the NANO works as it should, Keypad, Display, Relays, PCF8574s; everything Works --- SLOWER when compared to the UNO.

If I switch back to either a UNO or MEGA the Speed Issue disappears. I know the UNO and NANO are 16mHz, I have 4 NANOs and the Speed is the same using any of them. It's not so much the 'Wait for it to change' , more 'Come on, what are you waiting for?'.
I think if I hadn't been checking things with the UNO, I wouldn't have noticed the difference, and would think the 'Speed' was OK.

At the end of the day, it's only switching relays so I can live with it.

Regards S.

( NB: Watch out for a PM, if you get a chance to lookover the code, maybe you will spot something I missed. )

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Thu Oct 13, 2022 7:07 pm

Hi Andrew / All,

General question as I am experiencing a little issue with the 4x4 Keypad.

All the keys work fine, all RET Values are fine - Except for the VERY FIRST returned Value. The only way I can describe it is for you to PICK any number between 0 & 255 - that is what I get. After that, I get every RET CODE as I should.

Any Ideas what the issue(s) may be - [ Andrew, you already have the Relay Code ]?

Regards S

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Fri Oct 14, 2022 10:00 am

Does it do the same thing if you run the libraries example sketch (HCMatrixKeypad_Example1)?

This would at least show if its an issue with the library or your sketch.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Fri Oct 14, 2022 1:28 pm

Hi Andrew,

Sorry to say it does the same, 1st RET CODE is always 'Pick a Number', after that Perfect.

All I can say is Very Strange.

However, could it also be a pointer to Win XP OS ?

Regards S.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Fri Oct 14, 2022 1:41 pm

Hi Andrew,

Sorry to be a nuisance, but I have just run a couple of silly Tests.

As you know I run Win XP-Pro sp3.

T1: Compile using IDE v1.0.6 - RET CODE error does NOT occur; repeat does NOT occur.

T2: Compile using IDE v 1.8.9 - RET CODE error DOES occur; repeat DOES occur.

To me that points to something with IDE v1.8.9 and WIN XP.

If you remember I was having similar stupid errors when attempting to compile code for the LGT8 Board - which I still have not found an answer to.

Regards S

Post Reply

Return to “Arduino”