Looking for Spin example to look up a value in a list
Don M
Posts: 1,652
in Propeller 1
I've been messing around with Beau Schwabe's 4x4 keypad demo. It produces a word and depending on which key is pressed sets a bit in the word. The keypad I have has these keys in the order in which I see the bits set (*, 0, #, D, 7, 8, 9, C, 4, 5, 6, B, 1, 2, 3, A). For instance 0100000000000000 would be the "0" key pressed.
How do I iterate through the list to get the corresponding character that aligns with the bit set in the word?
Comments
I got this to work:
Is there any better or simpler way?
BTW how do you post code in the forum. I tried [/code] and [\code]...
Triple backticks, like this:.
```
Code goes here
```
Thanks. I changed it.
Here is the code!
https://github.com/parallaxinc/propeller/blob/master/libraries/community/p1/All/4x4 Keypad Reader DEMO/4x4 keypad Reader DEMO.spin
You can also use a CASE statement.
Not necessarily better, just easier to read.
And then list all possible states for the keypad
You can find the correct binary number when you see what the RAW keypad value is on your terminal output.
You can then place that entire binary number into the CASE statement like I did for two of the binary positions.
No need to iterate. The >| (encode) operator will tell you which bit is set (+1). This routine will turn your bit into an ASCII character.
This will return 0 if no key was pressed, or the ASCII char if one was.
Or:
Thanks for the help. Much appreciated.