converting keypad number to binary

Hello,

I am programming a numerical keypad, going well so far. I intend to limit it to 3 digit numbers, so 1 would be 001.

I am using variable b1 to store which number key is pressed.

the question is...

How could I convert the number in b1 to binary, and then use that to control all 8 outputs on a 20 M2?

ie, if the numbers 001 is pressed, I would like pinsB to output as %00000001 and so on. This can then be read at another 20M2 and interpreted as a number again.


I also want to use a serial output at some point, but Im pretty confident with that.

Why both you ask? As a method of teaching students about binary, but also having a dirt simple serial backup.

Hope someone can come up with some useful ideas.

Thanks
Jon
 

techElder

Well-known member
Jon, how are you getting a value to store in b1? Are you scanning rows and columns of the keypad?
 
yep, scanning rows and columns. works for a single digit so far, but now Im trying to work out how to put each keypress into a 3 digit number. I thought it would be easy... hey ho.

b1 is the key that has been pressed, so Im now going to use b2 to copy b1 and form a 3 digit number... not quite sure how yet though...
 
Nope - it is as easy as I thought - just use b2, b3 and b4 to generate a 3 digit number on b5.

b1 is the original key press
b2 = b1 * 100
b3 = b1 * 10
b4 = b1
b5 = b2+b3+b4 = 3 digit number!

it could be done for any number of digits too :)

so now the question is... how would I convert a 3 digit number in b5 to a binary output?
ie 001 = %00000001
002 = %00000010

and so on
 

techElder

Well-known member
Well, you don't really have "a 3 digit number" in b5, Jon. You just have a number in b5.

You can branch with this number in a SELECT CASE command to do other things or you could send this out serially by adding the "#" sign in front of it for ASCII output, or many other things.
 
Its a number with 3 digits - a 3 digit number. as opposed to a 2 or 4 digit number...

SELECT CASE seems like it will need an individual line for every number between 0 and 255 to produce binary.

is there a way to produce binary from the number (3 digit or whatever you want to call it) that I can then use with let pinsB = %00000000 ?
 
well that was ridiculously easy when I stopped trying to make it more complex than it was.

just use the 3 digit number entered as a variable with pinsB

Let dirsB = 255
Let pinsB = b5

not sure why I needed to worry about select case or anything else - perhaps Tex was making it too complicated as well.
 

techElder

Well-known member
Jon, I am a lot of things ... but an overseas mind reader is not one of them. You're on your own now.
 

techElder

Well-known member
“Confidence is ignorance. If you're feeling cocky, it's because there's something you don't know.”

― Eoin Colfer, Artemis Fowl​
 

AllyCat

Senior Member
Hi,

Not sure how it could be misunderstood.
Perhaps because you were asking a nonsensical question. b1 (or b5, or whatever) does not contain a "3-digit number", it (already) contains a binary number in the range 0 to 255. So, as you have now discovered, no "conversion" is required. But if you enter the 3-digit number "256" on the keyboard, does it do what you want?

Cheers, Alan.
 
I asked a question and expected some possibly useful answers.

I certainly did not expect people telling me that the question was nonsensical and being petty and pedantic.

If you have nothing useful to add, dont say anything. its that simple.

Alan, you know the answer to your own question - so why bother asking it.
 

westaust55

Moderator
@Jon,
As you have worked out, all values are stored in PICAXE registers/variables in binary format.
It is only for our benefit/readability that when displayed we see decimal numbers such as when using SEROUT with a # in front of the variable name - otherwise we see an other ASCII character on the LCD/OLED display.

From a binary number it is real actively easy to convert also to BCD and Hexadecimal as well as decimal output.

As highlighted/inferred by Alan, your keypad scanning and number build-up does also need to check that a valid result in the range 0 - 255 has been entered.
That may require the final variable to be a word variable to hold a value greater than 255.
Say you use variable w6 for which the constituent byte variables are b13:b12
Then if b13 > 0 you have overflow otherwise if b13 = 0 then use b12 passed to the port pins.
 

sghioto

Senior Member
let dirsB = %00000000
let pinsB = b1

I think this is all you need as far as the output on pinsB is required

Steve G
 
Top