Keypad scanning

dave_scfc

New Member
Hi im struggling to get my code to scan correctly , it seems to but stuck on scanning row 4 , heres my code for decoding each key press

symbol key = b0


main:
debug

high 0
let key = 0
gosub check
low 0

high 1
let key = 3
gosub check
low 1

high 2
let key = 6
gosub check
low 2

high 3
let key = 9
gosub check
low 3
goto main

check:
if pin0 = 1 then gosub add1
if pin1 = 1 then gosub add2
if pin2 = 1 then gosub add3
return

add3: let key = key + 1
add2: let key = key + 1
add1: let key = key + 1


goto main
 

Rickharris

Senior Member
Your using gosub Add1,2,3 but not return you jump direct to main again leaving the gosub address on the stack. you should return from all gosubs to clear the stack.
 

dave_scfc

New Member
like this ?

symbol key = b0


main:
debug

high 0
let key = 0
gosub check
low 0

high 1
let key = 3
gosub check
low 1

high 2
let key = 6
gosub check
low 2

high 3
let key = 9
gosub check
low 3
goto main

check:
if pin0 = 1 then gosub add1
if pin1 = 1 then gosub add2
if pin2 = 1 then gosub add3
return

add3: let key = key + 1
add2: let key = key + 1
add1: let key = key + 1
return

goto main
 

BeanieBots

Moderator
Well, the subs and returns now match up. Does it do what you want?

Edit:
The last "Goto main" is not required, program flow will never get there.
 
Last edited:

BeanieBots

Moderator
I'd remove "debug" and place sertxd in a few strategic positions to determine values at key points throughout the code.
That should reveal if it's inputs not working or the code not flowing as you expect.
 

BeanieBots

Moderator
Manual 2, page 175.
It's a method of sending specific data back down the programming cable.
You can use it (for example) to send a variable value back at a specific point in your code. By adding in some text it is possible to identify where the line is which has sent the value.

Alternatively, run your code in the simulator with some breakpoints or single step to check the flow.
 

hippy

Technical Support
Staff member
The bug is in your 'key' variable handling. Note that you have a series of 'let key=' statements which will disregard whatever 'key' was and set it to a new value.
 

dave_scfc

New Member
I know but my scan code , once its scanned the final row where the keypress = 9 it should then restart and go back through , = 0 , 3 , 6 etc , for some reason it stops on 9
 

Chavaquiah

Senior Member
It does not stop at 9. It just goes very fast through 0, 3, 6 and finally 9. By the time the program loops back to main and the debug line, the last of your let key = x (last being 9) statements has been executed. Just like hippy warned.

To see what's going on, try BeanieBots suggestion and run the code through the simulator.
 

dave_scfc

New Member
ive the code the simulator and it works fine , runnning through each line and then when reaching 9 go's to check comes back and returns to main
 

dave_scfc

New Member
here is the full code


scan_1:
debug
if code = 7 then goto Unlock

let Key_press = 0
let pins = %00000001
gosub check
'pause 15
let Key_press = 3
let pins = %00000010
gosub check
'pause 15
let Key_press = 6
let pins = %00000100
gosub check
'pause 15
let Key_press = 9
let pins = %00001000
gosub check
'pause 15
goto scan_1

check:
if pin0 = 1 then add1
if pin1 = 1 then add2
if pin2 = 1 then add3
return

add3: let Key_press = Key_press + 1
add2: let Key_press = Key_press + 1
add1: let Key_press = Key_press + 1

let code = code + 1
if code = 1 then goto test1
if code = 4 then goto test2
if code = 5 then goto test3
if code = 6 then goto test4

test1:
if Key_press = 2 then let code = 1+2
goto scan_1
else
goto ClearR
endif

test2:
if Key_press = 2 then let code = 2+2
goto scan_1
else
goto ClearR
endif

test3:
if Key_press = 2 then let code = 2+3
goto scan_1
else
goto ClearR
endif

test4:
if Key_press = 2 then let code = 2+5
goto scan_1
else
goto ClearR
endif


Unlock:

Let pins = %10000000
wait 5
goto ClearR

ClearR:

let code = 0
let Key_press = 0
goto scan_1
 

hippy

Technical Support
Staff member
Yes, the code returns to 'main' but what is held in the 'key_press' variable at that time ?

It's always 9 as you note. Now looking back as you progress through the code in another loop in simulation, why is that 'key_press' variable becoming 9 ?

It is because you are setting it always to 9.
 

dave_scfc

New Member
but when it gets to 9 and comes back from the gosub it goes back to the top of the program where it should be set to 0
 

hippy

Technical Support
Staff member
Ignore what I wrote if you read it.

The code has now significantly changed since the original, and there are further bugs within it such as 'goto scan_1' within subroutines.

I would recommend drawing a flowchart, at least on paper, of what you are attempting to achieve and ensure that the code you produce reflects that.
 
Last edited:

Chavaquiah

Senior Member
I wrote a little code before realizing you are trying to decode a key sequence. So, this will not serve as a complete solution, but still...

Code:
symbol Row1 = 0
symbol Row2 = 1
symbol Row3 = 2
symbol Row4 = 3

symbol Col1 = %001
symbol Col2 = %010
symbol Col3 = %100
symbol ColMask = %00000111

symbol key = b0
symbol row = b1
symbol col = b2

main:
   gosub CheckKey
   debug
   pause 100
   goto main

CheckKey:
   'On return key is:
   '    0 = No key pressed or invalid
   '    1..9 = Keys 1 to 9
   '    10 = *
   '    11 = key 0
   '    12 = #
   key = 0
   for row = Row1 to Row4
      high row
      col = pins & ColMask
      low row
      if col > 0 then
         key = 3 * row
         if col = Col1 then
            key = key + 1
         elseif col = Col2 then
            key = key + 2
         elseif col = Col3 then
            key = key + 3
         else 'More than one key pressed: ignore
            key = 0
         endif
         exit 'for
      endif
   next row
   return
I'm away from any Picaxe or the Programming Editor so I didn't test. Maybe there are some horrific bugs. Hope not.
 

hippy

Technical Support
Staff member
I would suggest following Chavaquiah's lead and write the key press detection routine separately from the code sequence checking. Get the key press detection working before moving on to the rest.

Your original code was pretty close to what you need, or you can do it how Chavaquiah suggests. The important thing is to have a routine you can Gosub to which returns a value in 'key_press' which indicates no key was pressed or which was.
 
Top