KeyPad 3x4 Picaxe 18m2

Alex3k

Member
Hey guys,

So im trying to make a keypad with say the combo lock of 1993.

I am seriously struggling to work out the code. Dont get me wrong i dont want people to tell me the code but can someone help with like what to do such as the pseudo code?

I just dont know where to start, ive looked over the forum and people are using things that i dont understand such as "b3=pinsB & %00001111"

Cheers
 

nick12ab

Senior Member
This code is a procedure for checking the keypad and putting a number in a variable depending on which key is pressed, which you could adapt for 4x3 keys. This code is for 3x3 keys for a tic-tac-toe game.
Code:
symbol keypad = b4
dirsB = %11000111
pullup  %00111000
prc_X:
	high B.2
	high B.1
	low B.0
	if pinB.3 = 0 then : keypad = 1 : return : end if
	if pinB.4 = 0 then : keypad = 4 : return : end if
	if pinB.5 = 0 then : keypad = 7 : return : end if
	high B.0
	low B.1
	if pinB.3 = 0 then : keypad = 2 : return : end if
	if pinB.4 = 0 then : keypad = 5 : return : end if
	if pinB.5 = 0 then : keypad = 8 : return : end if
	high B.1
	low B.2
	if pinB.3 = 0 then : keypad = 3 : return : end if
	if pinB.4 = 0 then : keypad = 6 : return : end if
	if pinB.5 = 0 then : keypad = 9 : return : end if
	let keypad = 0
	return
The code uses port B as it provides pull-up resistors so no external values. To use it in your application, add the extra lines for the extra row of keys, change that end line "let keypad = 0" to "let keypad = 15" so you can use 0 for the 0 key on the keypad. Simply use IF statements to check that the keypad variable matches the code AFTER you have executed "gosub prc_X" (in four steps so if keypad = 1, if keypad = 9 and so on).
 
Last edited:

nick12ab

Senior Member
dirsB = %11000111
pullup %00111000

What do the two above lines do?
DirsB is the data direction for port B and sets the required pins to inputs or outputs where 1 is an output and 0 is an input, with B.0 being the rightmost bit. Pullup enables the internal pullup resistors on the pins that require it where a 1 means enables and a 0 means disabled. Using a pullup eliminates the need for an external resistor.
 

Svejk

Senior Member
There are 2 methods to read the input from a 3x4 (or 4x4) keypad:

1. key scan powering the columns one by one and read the rows; or
2. use ADC, placing resistors across the terminals.

For both methods a "wait for release" routine must be implemented.

The first one is always acurate but takes more IO pins. It may be used with interrupts.

The second one is not always as acurate (this can be improved by carefully choosing the resistors) but uses only one pin.

See attached for hardware setup.
 

Attachments

Alex3k

Member
ok, so this is the code i have wrote this morning:
Code:
'OUTPUTS

'Row 1 - Blue Wire - Pin 10 (b.4)
'Row 2 - Green Wire - Pin 11 (b.5)
'Row 3 - Yellow Wire - Pin 12 (b.6)
'Row 4 - Brown Wire - Pin 13 (b.7)


'INPUTS

'Column 1 - Red Wire - Pin 17 (C.0)
'Column 2 - Orange Wire - 16 (C.7)
'Column 3 - White Wire - 15 (C.6)

'KeyPad

' 1	2	3
' 4	5	6
' 7	8	9
' *	0	#


symbol Row1 = b.4
symbol Row2 = b.5
symbol Row3 = b.6
symbol Row4 = b.7

symbol Column1 = c.0
symbol Column2 = c.7
symbol Column3 = c.6

symbol PinPressed1 = b0
symbol PinPressed2 = b4
symbol PinPressed3 = b5
symbol PinPressed4 = b6

Symbol Combination = b1

symbol Num1 = 1
symbol Num2 = 128
symbol Num3 = 64
symbol CurrentRow = b3

main:
debug

for CurrentRow = Row1 to Row4
High CurrentRow
High PinsC
b2 = PinsC

	if CurrentRow = Row1 then
		if b2 = Num1 then 
	      PinPressed1 = 1
		elseif b2 = Num2 then
		PinPressed1 = 2
		elseif b2 = Num3 then
		PinPressed1 = 3
		endif
	Endif
	
	If CurrentRow = Row2 then
		if b2 = Num1 then 
		PinPressed2 = 4
		elseif b2 = Num2 then 
		PinPressed2 = 5
		elseif b2 = Num3 then 
		PinPressed2 = 6
		endif
	Endif
	
	If CurrentRow = Row3 then
		if b2 = Num1 then 
		PinPressed3 = 7 
		elseif b2 = Num2 then
		PinPressed3 = 8
		elseif b2 = Num3 then 
		PinPressed3 = 9
		endif
	Endif
	
	If CurrentRow = Row4 then 
		if b2 = Num1 then 
		PinPressed4 = "*"
		elseif b2 = Num2 then 
		PinPressed4 = 0
		elseif b2 = Num3 then 
			PinPressed1 = 0
			PinPressed2 = 0
			PinPressed3 = 0
			PinPressed4 = 0 
		endif
	Endif
	


Low PinsC

Low CurrentRow

next



CurrentRow = 4

gosub Digit1

goto main


	

' PIN IS 25842

Digit1:
	if PinPressed1 = 2 then
		goto Digit2
	Endif

Digit2:
	if PinPressed2 = 5 then
		goto Digit3
	Endif

Digit3:
	if PinPressed3 = 8 then
		goto Digit4
	Endif

Digit4:
	if PinPressed4 = 42 then
	high B.3
	else
	low B.3
	Endif
	return
As you can see the pin is 25842 (the 42 is from the "*")
so the code i have used means you can only have one digit from each row for example if you input 3 in the first row PinPressed1 will change to 3 but then if you put 4 in the second row PinPressed2 = 4 however then for the 3rd digit if you input 1 the PinPressed1 variable changes from 3 to 1.

How can i do it like a concatenation? and then i check if the concatenated string is equal to 25842?

I hope i explained it well
Alex
 

Svejk

Senior Member
It may be better to break the things down:

1. write a subroutine that just check if any key is pressed
2. if a key is pressed then put the key value into a variable; debounce; wait for release.
3. once all user code is aquired compare those values with the unlock code

Code attached as starter (B.0 to B.3 are the rows, B.4 to B.6 the cols):

Code:
#picaxe 18M2

dirsB = $0F

#rem
b0..b3 rows
b4..b6 cols

   c1 c2 c3
r1 1  2  3
r2 4  5  6
r3 7  8  9
r4 *  0  #

keyPressed returns

   c1 c2 c3
r1 17 33 65
r2 18 34 66
r3 20 36 70
r4 24 40 72


#endrem

symbol row = b0
symbol col = b1
symbol keyPressed = b2
symbol digit1 = b3
symbol digit2 = b4
symbol digit3 = b5
symbol digit4 = b6


main:
  gosub check_Key
  digit1 = keyPressed
  gosub check_Key
  digit2 = keyPressed
  gosub check_Key
  digit3 = keyPressed
  gosub check_Key
  digit4 = keyPressed

goto main



check_Key:
' check_Key returns keypad button value
' INPUT: none
' OUTPUT: keyPressed


  ' wait for a key to be pressed
  row = 8
  do
    row = row * 2
    if row > 8 then
      row = 1
    endif
    pinsB = row
    col = pinsB & $70
  loop while col = 0
  
  ' key pressed
  keyPressed = row + col

  ' debounce
  pause 100 
  
  ' wait for release
  do 
    col = pinsB & $70
  loop until col = 0
return
 
Top