numeric keypad

sghioto

Senior Member
I have programed a numeric keypad to store a two digit number, 00 to 99 . The first digit (units) is stored in B1 and the second digit (tens) is stored in B2. Lets say I press the number "1" and then the number "2". What's the best way to combine these two variables to get the value 12 ? My program works but seems like an awful "long way around" approach. Is there a simple math command that will do this? Have searched this forum and Google but couldn't find anything related to what I'm trying to do.

Thanks,
Steve Ghioto
 

free1000

New Member
I have programed a numeric keypad to store a two digit number, 00 to 99 . The first digit (units) is stored in B1 and the second digit (tens) is stored in B2. Lets say I press the number "1" and then the number "2". What's the best way to combine these two variables to get the value 12 ? My program works but seems like an awful "long way around" approach. Is there a simple math command that will do this? Have searched this forum and Google but couldn't find anything related to what I'm trying to do.

Thanks,
Steve Ghioto
I'm just adding the ones digit to the 10s digit x 10... You can also do it with binary operations... but its a long time since I was at college... so I've also done it the long way round.

'
'set the relevant EEPROM entry with the current digits
'
' delayLocation, number denoting EEPROM location
' delay, time to delay output firing
'
updateDelay:
w6 = digit1 * 1000 'thousands
delay = w6
w6 = digit2 * 100 'hundreds
delay = delay + w6
w6 = digit3 * 10 'tens
delay = delay + w6
w6 = digit4 'ones
delay = delay + w6
write delayLocation, word delay
return
 

sghioto

Senior Member
Here's the lower half of the program as I have configured to store the two digit number from the keypad in variable b0. This number is then "infraout" in the xmit routine. This works as programed but again is there a simpler way, without all the "IF" and "THEN" commands? (tens is stored in b1 and units in b2.)

Thanks,
Steve Ghioto

tens_check:
if b1 = 0 then code0
if b1 = 1 then code1
if b1 = 2 then code2
if b1 = 3 then code3
if b1 = 4 then code4
if b1 = 5 then code5
if b1 = 6 then code6
if b1 = 7 then code7
if b1 = 8 then code8
if b1 = 9 then code9

code0: b0 = b2
goto xmit

code1: b0 = b1 + b2 + 9
goto xmit

code2: b0 = b1 + b2 + 18
goto xmit

code3: b0 = b1 + b2 + 27
goto xmit

code4: b0 = b1 + b2 + 36
goto xmit

code5: b0 = b1 + b2 + 45
goto xmit

code6: b0 = b1 + b2 + 54
goto xmit

code7: b0 = b1 + b2 + 63
goto xmit

code8: b0 = b1 + b2 + 72
goto xmit

code9: b0 = b1 + b2 + 81
goto xmit

xmit: pause 100
for b3 = 1 to 5
high 5
infraout 1,b0
pause 45
next b3
low 5
goto main
 
Last edited:

BCJKiwi

Senior Member
Presumably the first press is in b1 (tens) and the second in b2 (ones).
If so this might work - after the second press;

gosub FullNum

FullNum:
b3=b1*10+b2
Return

b3 would contain 12 in your original example
 

sghioto

Senior Member
Yes that works for numbers from 10 to 19, but you need a different formula and subroutine for each decade. I'm doing that now with the program as written. So is this the only way the units and tens can be converted and stored in a seperate variable?

Steve Ghioto
 

BCJKiwi

Senior Member
Don't follow;

You are looking for 00 to 99

If you press 7 then 3,
you will get 73 from that routine.

This works for 00 thru 99, try it in the simulator.

If that does not do what you want, then perhaps further explanation of your objectives would help.
 
Last edited:

hippy

Technical Support
Staff member
If you have five digits, 1, 2, 3 and 4 in variables b1 through b4, you can put them in one word variable using -

w0 = b1 * 10 + b2 * 10 + b3 * 10 + b4

PICAXE maths is strictly left to right so that's the same as -

w0 = (b1*1000) + (b2*100) + (b3*10) + b4

But you cannot use that form with the PICAXE.
 

sghioto

Senior Member
Sorry BCJKiwi . My mistake, thats the formula I'm looking for and thanks. And to you hippy, that would have been my next question.

Steve Ghioto
 

BCJKiwi

Senior Member
That's fine Steve - it's always tricky trying to understand another's explanations.

You will also need to think about whether you want the higher order digits entered first or last , and how to test if you received them all.

If you do high order first (more logical for the human brain?) then you would need to enter 0,0,7,3 to distinguish it from 7,3,nopress,nopress unless you prefill the variables with a non numeric value, test, and shift them over etc.

As oft jokingly misquoted;
"Confucius he say it impossible to make anything foolproof because fools so ingenious"
 
Top