# reading a 4x4 switch matrix

#### MrDEB

##### Well-Known Member
You guys crack me up.
I realize the diodes are not needed but it makes a better mouse trap .
I understand the module after I followed the code flow and the need for the diodes but why not include them. It does make a better key pad.
I admit I tend to jump the gun and assimilate bits and pieces then ask why.
That's why I assemble bits of code, test that snippet then AFTER all the snippets work as planned, I put the entire puzzle together into one package.
The keypad is the first snippet then get the LCD working then hopefully get both to play together nice.

#### be80be

##### Well-Known Member
You don't have the trap right tho

It's like this

#### Pommie

##### Well-Known Member
Burt, the switches and diodes should be in series. They should not be in the column lines.

Mike.

#### be80be

##### Well-Known Member
Mike that how they show it I tried it once maybe 8 years ago

It's there drawing not mine It's only 8 buttons I added a set It did work I still got the keypad I made.

#### be80be

##### Well-Known Member
Here you go

But I didn't make that one it's not complete.

#### JonSea

##### Well-Known Member
No. More, unnecessary parts do not make a "better mouse trap", particularly where it is likely that they won't be added correctly. Sorry MrDEB, your history here attests to that fact.

MrDEB is trying to make a calculator, to add up domino values. As such, there is no reason multiple keys will be pressed at the same time and therefore, no problem with ghosting.

Don't make it more difficult than it needs to be, especially when doing so enhances nothing.

#### be80be

##### Well-Known Member
Here is with the diodes

#### MrDEB

##### Well-Known Member
After reviewing all the suggestions and having no need to order smd diodes I have decided to forgo using them.
Might regret but??

#### JonSea

##### Well-Known Member
Mike, what do you think? Over 12 on this one?

#### be80be

##### Well-Known Member
Well if he had read the whole post that he linked the diodes don't make up for bad coding. I tried them out didn't really make anything better that was using the keypad bas

#### JonSea

##### Well-Known Member
They don't make for bad coding, if they are installed correctly. They add nothing for this app and increase the likelihood of errors substantially.

With about 10 minutes of effort, I came up with this program, which reads keys as expected.

Code:
Include "keypad16pullup.bas"
Include "usart.bas"
Include "convert.bas"

Dim Keypressed As Byte
Dim Keyvalue As Byte
SetBaudrate(br9600)

DelayMS(5000)

USART.Write ("Test program",13, 10, 10)

While 1 = 1
If Keypressed <> 0 Then
USART.Write ("Key pressed = ", HexToStr(Keypressed), 13, 10)
End If
Wend
The output looks like this. It prints out a value as long as a key is pressed.

Code:
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 1
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 2
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 3
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4
Key pressed = 4

Yes, a little logic is required to return a single value when a key is pressed. A trivial amount.

#### be80be

##### Well-Known Member
Jon I don't think you read what I said.

I said they don't make up for bad coding.

And how I showed them is the right way it straight from the page

I don't understand where mike thinks there wrong

#### JonSea

##### Well-Known Member
Burt, I haven't really looked at where the diodes should go. I suspect there are different methods. But since they aren't necessary for this application, they can't improve anything; they can only screw things up. So why have them?

Here's a picture of the keypad I used. The 4×4 matrix is completely isolated from the other switches and LEDs. Nothing but 16 switches comnected to rows and columns. And NO DIODES.

As far as telling when a new switch is pressed, look for a value of 0. That indicates no switch is pressed.

#### Pommie

##### Well-Known Member
Burt,
What you and MrDEB posted both had the diodes in the same place - in series with the push buttons. When you said they were wrong I mistook the green arrow in your diagram as a diode.

Jon, 17.

Mike.

#### JonSea

##### Well-Known Member
Mike, I think diodes are going to add 5 by themselves!

It's weird to me that someone who won't spend 27 cents to use a crystal and who is complaining how crowded his board is would be so reluctant to not include diodes where they aren't needed. Such are the mysteries of life.

#### be80be

##### Well-Known Member
The diodes are wrong there not going to fix the problem and your right i looked at the link he posted he has them drawn different.
But its the same but i was more thinking adding them. Not going to help lol

#### JonSea

##### Well-Known Member
Here's a slight addition to my code to read each keypress only once until the key has been released. Holding the key down does not result in another keypress being recorded. I am printing out the hex value of each key, pressed in order. For whatever reason, $10 comes out of order after$F. I'm not sure if this is a problem in the keypad module or in the keypad hardware. At any rate, this won't be a problem for MrDEB, as he needs to map the keypress value against what he wants for the value of each key.

This is still a tiny amount of code to read the keypad.

Code:
Include "keypad16pullup.bas"
Include "usart.bas"
Include "convert.bas"

Dim Keypressed As Byte
Dim Keyvalue As Byte

SetBaudrate(br9600)

DelayMS(5000)

USART.Write ("Test program",13, 10, 10)

keypressed = 0
While 1 = 1

while keypressed = 0            'print key and exit when key pressed
If Keypressed <> 0 Then
USART.Write ("Key pressed = ", HexToStr(Keypressed), 13, 10)
End If
wend

while keypressed <> 0                'loop until key not pressed
wend
wend
The output of pressing each key in turn looks like this. It doesn't matter if a key is pressed and held down. It will be recorded only once.

Code:
Test program
Key pressed = 1
Key pressed = 2
Key pressed = 3
Key pressed = 4
Key pressed = 5
Key pressed = 6
Key pressed = 7
Key pressed = 8
Key pressed = 9
Key pressed = A
Key pressed = B
Key pressed = C
Key pressed = D
Key pressed = E
Key pressed = F
Key pressed = 10
Key pressed = 1
Key pressed = 2
Key pressed = 3
Key pressed = 4
Key pressed = 5
Key pressed = 6
Key pressed = 7
Key pressed = 8
Key pressed = 9
Key pressed = A
Key pressed = B
Key pressed = C
Key pressed = D
Key pressed = E
Key pressed = F
Key pressed = 10

#### tumbleweed

##### Active Member
For whatever reason, $10 comes out of order after$F
That looks right to me... the function returns 1-16 for a keypress, so the last few would be 14, 15, 16 (or $0E,$0F, $10) #### JonSea ##### Well-Known Member That looks right to me... the function returns 1-16 for a keypress, so the last few would be 14, 15, 16 (or$0E, $0F,$10)
d'oh! You can see how much I work with hex! Of course A comes after 9. It was late, early, I was hungry....pick a good excuse