matrix keypad/How it reads a key/06. One adapter, two keymaps
How it reads a key · 06 of 11

One adapter, two keymaps

The library's key number says which two pins a key joined. A keymap string turns it into the printed character, and the soft and heavy-duty keypads need different strings because they bring their rows and columns out in opposite order.

The keymap

A keymap is a string of eighteen characters. Position 0 holds the character for key number 0, position 1 for key number 1, up to 15. Position 16 is what getChar() returns for no key, and position 17 for FAIL — by convention N and F.

char keymap[19] = "DCBA#9630852*741NF";      // soft keypad (5-pack, TinkerBlock)
char keymap[19] = "D#0*C987B654A321NF";      // heavy-duty keypad (2-pack)

Nineteen, not eighteen, because a C string ends with an invisible zero.

Two keypads, two strings

Key, number, character
wrong map
Press a key
Keypad on the desk
Keymap in the sketch
Pressed
2
Key number
14
Printed
4
Nothing is broken: the sketch has the other keypad’s map. The heavy-duty keypad brings its rows out where the soft one brings its columns, so the same key lands on a different number. Load the heavy-duty keypad map and 2 prints 2.

The soft keypad's ribbon carries its four rows first and its four columns second. On the adapter that puts the rows on P7 to P4 and the columns on P3 to P0. The heavy-duty keypad brings its columns out first, so its columns land on P7 to P4 and its rows on P3 to P0.

The library does not know about rows and columns. It only knows the top half of the chip and the bottom half. So on the soft keypad a key's row decides the first answer, and on the heavy-duty keypad its column does. The same key ends up with a different number, and each keypad needs its own string.

What the wrong map does

With the wrong string loaded, every key prints the key reflected across the diagonal from 1 to D: 2 prints 4, 3 prints 7, A prints *. The four keys on the diagonal — 1, 5, 9 and D — print correctly either way, which is why a quick test of 1 and 5 can pass with the wrong map.

Nothing is broken when this happens. Change one line of the sketch.

When it does not work

Every key prints a different key

The sketch has the other keypad's map. Pressing 2 and seeing 4, or A and seeing *, is the giveaway: the wrong map reflects every key across the diagonal. Swap to the other string.

1, 5, 9 and D are right and nothing else is

Same cause. Those four keys sit on the diagonal, where both maps agree, so they print correctly with either one. Load the map for the keypad in your hand.

Some keys print strange characters or nothing at all

The map string is short. It must be exactly eighteen characters — sixteen keys, then N and F — in a char array of 19. getChar() does not check, and reads past the end of a short string.

My keypad is from somewhere else

Then neither string is safe to assume. Run the keymap finder in the last article: press every key once in printed order and it prints the right string for that keypad.

Where this goes next

Why the library answers F for two keys, and why that is better than the alternative.

Two keys at once

Edit this page — content/books/matrix-keypad/one-adapter-two-keymaps.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum