KEYPAD WITH ARDUINO

when using for loop with keypad to countdown what i pressed it types 53 instead of typing 9 and so on ?
If you could give me a tip .thanks in advance

#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] =
{
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’#’,‘0’,’*’}
};
byte rowPins[ROWS] = {31,33,35,37};
byte colPins[COLS] = {39,41,43};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
Serial.begin(9600);

}

void loop() {

char key = keypad.getKey();

if (key != NO_KEY){

for(int i=key;i>-1;i–){
Serial.println(i);
delay(1000);

}

}}

ard.JPG

First thing is first, we are not here to do the work for you. We are here to teach you and help you solve your own problem.

Secondly, I’m pretty sure you mean 57 when you press 9.

Why would it do this? Well think about it, if you pressed ‘a’ how is that letter sent. Anything and everything you type as “text” will have each and every character converted into a binary number. Luckily, there is a well known standard. Try searching “ASCII” in google.

Lastly, put your code in code tags Like this, select your code and press the </> button

Why this:
for(int i=key;i>-1;i--){
Serial.println(i);
delay(1000);

instead of just:
Serial.print(key);

CrossRoads as i need to countdown what i have pressed

Ps991:
First thing is first, we are not here to do the work for you. We are here to teach you and help you solve your own problem.

Secondly, I’m pretty sure you mean 57 when you press 9.

Why would it do this? Well think about it, if you pressed ‘a’ how is that letter sent. Anything and everything you type as “text” will have each and every character converted into a binary number. Luckily, there is a well known standard. Try searching “ASCII” in google.

Lastly, put your code in code tags Like this, select your code and press the </> button

yes you are right i searched for ascii but how to make it not reading the binary codes i have tried a lot but nothing if you could get me a tip

It is actually a lot easier than you think.

byte number = 0;
if(key >= '0' && key <= '9') //make sure key is actually a number
  number = key - '0';

BOOM, done…

This works because 9 and ‘9’ are not the same. 9 is an integer. ‘9’ is a character in ASCII, which translates to the integer 57. So if you pressed 9 and get 57, then subtracting ‘0’ (or 48) gives you 57 - 48 = 9 (as an integer).

Ps991:
It is actually a lot easier than you think.

byte number = 0;

if(key >= ‘0’ && key <= ‘9’) //make sure key is actually a number
  number = key - ‘0’;



BOOM, done...

This works because 9 and '9' are not the same. 9 is an integer. '9' is a character in ASCII, which translates to the integer 57. So if you pressed 9 and get 57, then subtracting '0' (or 48) gives you 57 - 48 = 9 (as an integer).

man you are so great you teached me alot of things really thx very much :smiley: