KeyPad, accept/reject input

ZOR

Senior Member
Is there a preferred or best way to collect data from a KeyPad to compare with what is accepted?

Example, a correct number to gain entry could be 123456 (only an example)

I have this code as starters for a 4 digit number

Code:
For b3=1 to 4

readadc B.2,b0

if b0<50 then goto gg

Select Case b0
Case>240:B1=12' hash
Case>190:B1=11' zero
Case >150:B1=10'star 
Case >125:b1=9
Case >105:b1=8
Case >92:b1=7
Case >80:b1=6
Case >73:b1=5
Case >68:b1=4
Case >62:b1=3
Case >57:b1=2
Case >50:b1=1
End Select	

' ???????????????

Next
I was pleased to see the other thread describing Put and Get, which would presumeably be the best way to store numbers

Code:
b0 = 100
For b1 = 0 To 5
  Put b1, b0 ; Scratchpad[b1] := b0
  b0 = b0 + 1
Next

For b1 = 0 To 5
  Get b1, b0 ; b0 := Scratchpad[b1]
  SerTxd( "Scratchpad[", #b1, "] = ", #b0, CR, LF )
Next
EDIT: Just found Put and Get not supported in 08M2 or 14M2 according to my v5 simulator, duh!
 
Last edited:

cravenhaven

Senior Member
You can use poke and peek. Just remember not to use the low end addresses as these are reserved for the common registers (b1,b2......).
 

ZOR

Senior Member
Many thanks, will look that up for future. Will wait on any suggestions for the main task entering all digits and comparing.
 

hippy

Technical Support
Staff member
If you create a routine which only returns when a key pressed, and returns the key in b0, you can simply move b0 into other variables then check those later ...

Code:
GetCode:
  Gosub GetKeyPress : b1 = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : b2 = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : b3 = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : b4 = b0 : If b0 = KEY_CLEAR Then GetCode
  If b1 <> KEY_CODE_1ST Then WrongCode
  If b2 <> KEY_CODE_2ND Then WrongCode
  If b3 <> KEY_CODE_3RD Then WrongCode
  If b4 <> KEY_CODE_4TH Then WrongCode
  Goto CorrectCode
If you want to use scratchpad to save on using variables ...

Code:
GetCode:
  ptr = ?
  Gosub GetKeyPress : @ptrInc = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : @ptrInc = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : @ptrInc = b0 : If b0 = KEY_CLEAR Then GetCode
  Gosub GetKeyPress : @ptrInc = b0 : If b0 = KEY_CLEAR Then GetCode
  ptr = ?
  If @ptrInc <> KEY_CODE_1ST Then WrongCode
  If @ptrInc <> KEY_CODE_2ND Then WrongCode
  If @ptrInc <> KEY_CODE_3RD Then WrongCode
  If @ptrInc <> KEY_CODE_4TH Then WrongCode
  Goto CorrectCode
And use 'bptr' rather than 'ptr' to use RAM rather than scratchpad.
 

ZOR

Senior Member
Many thanks hippy. The first routine looks just what I want.
Am I correct, storing a 4 digit number would entail hard coding KEY_CODE_1ST as Symbol = b4 etc, and then having KEY_CODE_1ST = 2 etc
I found I cannot use scratchpad with M2 Picaxe, but not worried as data dies on re-powering.
 

hippy

Technical Support
Staff member
You don't have to hard code the the security code. You can compare the b1..b4 variables to other variables which have been loaded with the security code, or compare to what is held in Eeprom ...

Code:
Read ADR_CODE_1ST, b0 : If b1 <> b0 Then WrongCode
Read ADR_CODE_2ND, b0 : If b2 <> b0 Then WrongCode
Read ADR_CODE_3RD, b0 : If b3 <> b0 Then WrongCode
Read ADR_CODE_4TH, b0 : If b4 <> b0 Then WrongCode
Goto CorrectCode
You don't have to hard code Eeprom addresses either, which would probably help having multiple codes granting access and/or programming.
 

ZOR

Senior Member
Thanks hippy. I had never played with read/write as always thought it required seperate IC. However playing with this code does not work? What am I doing wrong. I thought I could fill b1 in a loop and then get back.

Code:
#TERMINAL 4800

b1=3

for b0 = 0 to 5 ; start a loop

write b0,b1 ; write value of b1 into b0

next b0

for b0 = 0 to 5 ; start a loop

read b0,b1 ; read value at b0 into b1

SerTxd ( "b1 ",#b1, CR, LF )
SerTxd ( "b0 ",#b0, CR, LF )

next b0
 

hippy

Technical Support
Staff member
However playing with this code does not work?
In what way doesn't it work ? Which PICAXE are you using, are you running it on a real chip or simulating ?

It works for me when I test it in simulation and on a real 08M2 chip.
 

ZOR

Senior Member
Thanks, I am using a 14M2, programming it with the code.

The terminal window shows

ó3
b0 5

I thought as the read loop ran I would see the value of b0,b1
 

ZOR

Senior Member
hippy, I tried it in the simulator and it worked, but went through quickly so I put a Pause 1000 statement after the read syntax which slowed it down. I put the code into the 14M2 and that worked. But if I remove the delay I just get rubbish out. So with delay all okay. Normal?
 

hippy

Technical Support
Staff member
After downloading a program to a PICAXE it will begin to execute immediately, and may complete or partially complete before the PICAXE Editor has opened its Terminal window. This may corrupt the output displayed or more likely miss some or all of the data to be displayed.

The solution is usually to put a "PAUSE 2000" at the start of such a program to delay its operation until the Terminal has been opened.

If the Terminal is open, butthe display has missing data, turning the PICAXE off then back on should cause it to restart and all the expected data should be shown.
 

MartinM57

Moderator
4 standard things, none of them mandatory, that are well worth putting at the top of every program...

Code:
#picaxeXXX            ; reminds you which PICAXE you're using
#terminal XXXX        ; set the terminal speed
pause 1000            ; allow electronics and the terminal to stabilise 
sertxd ("Started...") ; to catch expected/unexpected restarts
 

ZOR

Senior Member
Thanks hippy, Martin, that's good to know, I will make that a practice in future. I simply cut and pasted from thread, and ran prior to experimental changes. Good to know why it happened. Regards
 
Top