tags:

views:

180

answers:

2

hi,

I like to know how count the received values from. Im using an 4x4 keypad, and AVR studio as compiler

for example if I press button "1" I receive a "1" but if I press button "1" again it should be an "11" and not a "2",

int inputcounter;
if (button = 00x1) { // what should i do instead of inputcounter++ to get "11" and not "2" }

Thank you.

+1  A: 

Based on the comment instead of inputcounter++, it sounds like you are trying to use a numeric value.

So you need to do:

inputcounter = (inputcounter * 10) + newvalue;
R Samuel Klatchko
Note that a 16-button keypad is used here. Multiplying by ten will only work if input is restricted to the first ten buttons.
bta
i use 0123456789 keys to drive an dc motor the with a max speed of 999 and min 0. multiplying inputs is good enough for the case but i well undoubtedly try your ways. thank you bta
Power-Mosfet
+1  A: 

I'm assuming you are trying to read in a key sequence and compare it to a sequence stored in the microcontroller's memory (a secret code, for example). You have two easy ways of doing this.

  1. Use an array. Each time a new input arrives, place it in the next array slot until you have read in your max number of input button presses.

  2. Pack the keystrokes into a single number. Assuming your keypad returns 1 when 1 is pressed, 2 when 2 is pressed, etc, you can use an integer to track input. Initialize the variable to zero. Whenever an input comes in, multiply the variable's current value by 16 and add the incoming digit. Since you have a 4x4 keypad, you will have to treat incoming keystrokes as hexidecimal digits, not decimal digits (the other suggestions that multiply by 10 will limit you to only using 10 out of your 16 available buttons).

The number of keys you can track at a time will depend on how many long you declare your array (for option #1) or what size variable you use (for option #2).

bta