how do i Read keypad using C + AVR controller
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开发者_Go百科 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.
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;
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.
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.
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).
精彩评论