User6996
User6996

Reputation: 3003

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 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.

Upvotes: 0

Views: 1698

Answers (2)

bta
bta

Reputation: 45057

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).

Upvotes: 1

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

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;

Upvotes: 1

Related Questions