how to have 0 value from ASCII conversion

Hi everybody. I'm writing a program that, using a Keypad, reads the keypressed, store the value into an array num[4], than calculate the number NUM with 4 digits associated.
The problem: mapping the keypad with '1', '2', '3' etc. Arduino stores into num[] the ASCII value, that is 49, 50, 51 etc.
So I mapped the keypad with 1,2,3. WHAT ABOUT ZERO??? Mapping with 0, the keypad doesn't work, mapping with NULL I obtain nothing, what a mess!
Can someone help me?

Here is my code:

#include <Keypad.h>

int i;
int k;
int NUM;
int TIME;
int Time;
int Num;
int Num1;
int Time1;
int shut = 10; // pin scatto
const byte myRows = 4; // number of rows
const byte myCols = 3; //number of columns

char keys[myRows][myCols] = {

{1,2,3},
{4,5,6},
{7,8,9},
{'a',0,'c'} ////HERE IS THE PROBLEM

};

byte rowPins[myRows] = {7, 2, 3, 6 }; //array to map keypad to MCU pins
byte colPins[myCols] = {5, 8, 4 }; //array to map keypad to MCU pins

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols ); //keypad library map function

int num[4];
int time[4];

void setup(){
pinMode(shut,OUTPUT);
digitalWrite(shut,LOW);
num[4]=0;
time[4]=0;

Serial.begin(9600);

for (int i=0; i<=3;){
//int num[4];
char key= keypad.getKey();
num *= key; *

  • if (key!=NO_KEY){*
  • if (key=='a' || key=='c'){*
  • i=4; *
  • }*
  • if(key=='0'){ /// IS IT RIGHT?*
  • key=atoi(0);*
  • }*
  • Serial.print(i);*
    _ Serial.print(num*);_
    _
    i++; _
    _
    };*_

* if(key=='a' || key=='b' || i==4){*
* for(int k=0; k<=3;){*
* int kei= keypad.getKey();*
* //int time[4];*
* time[k]=kei;*
* if (kei!=NO_KEY){
_
Serial.print(k);_
_
k++;_
_
};_
_
if (time[k]==48){_
_
time[k]=0; _
_
}_
_
}_
_
i=4;_
_
}_
_
}*_

NUM = num[3]+num[2]*10+num[1]*100+num[0]*1000;
TIME = time[3]+time[2]*10+time[1]*100+time[0]*1000;
}
void loop(){
* for(int i=0; i<=NUM; i++){*
* digitalWrite(shut, HIGH);*
* delay(200);*
* digitalWrite(shut,LOW);*
* delay(TIME); *
* }*
}

The char '0' has the ascii code 30 hex or 48 decimal, it comes right before '1' which is 31 hex or 49 decimal.

If you have a text character which you know is in the range '0' to '9', you can get the corresponding actual number by

char a = '7'   ;             //  or any other char in the range '0' to '9'

int value = a - '0' ;     //  value will be the binary number between 0 and 9

You need to figure out what your getKey() function is actually returning to you.

Get rid of all of that complicated garbage in the second half of your sketch, until you have figured it out.

getKey() returns the ASCII code. So, mapping the keypad, I don't write '1' but 1, so getKey() returns me the number 1.
With zero I'm trying to do the same thing, that is I want the ASCII code = 0, not the ASCII code for 0...

If you are getting ASCII back, then subtract 0x30, or 48 decimal, to convert the numbers to 0 to 9.
See www.asciitable.com

 if (key!=NO_KEY){
      if (key=='a' || key=='c'){
      i=4;  
      }
     else{
     num = key - 48; // convert to 0 to 9
     }
     key=atoi(0);

is nonsense. atoi converts a string to an int, and you give it a null pointer. It might
actually return 0 as it happens, not sure, but really...

According to Harbison and Steele, atoi() has undefined results if a conversion cannot be performed. It is safer to use strtol(), which is guaranteed to output a zero if a conversion cannot be performed.

CrossRoads:
If you are getting ASCII back, then subtract 0x30, or 48 decimal, to convert the numbers to 0 to 9.
See www.asciitable.com

 if (key!=NO_KEY){

if (key=='a' || key=='c'){
     i=4;  
     }
    else{
    num = key - 48; // convert to 0 to 9
    }

Thankyou all for your answers.
@ CrossRoads, I've tried your code:

  for (int i=0; i<=3;){      
    //int num[4];
    int key= keypad.getKey(); // legge il tasto premuto
    num[i] = key;    
    if (key!=NO_KEY){
      if (key=='#' || key=='*'){
      i=4;  
      }
    Serial.println(i);  
    Serial.write(num[i]); // per leggere il valore vero e non l'ASCII
    i++;       
    };
  
    if(key=='#' || key=='*' || i==4){ 
      for(int k=0; k<=3;){
        char kei= keypad.getKey();
        //int time[4];
        time[k]=kei; 
        if (kei!=NO_KEY){
          Serial.print(k);
          k++; 
        };
    //Serial.println(kei);
    }
    i=4;
    }
  }
  
// conversione ASCII-->valore numerico reale

int a=num[3]-48;
int b=num[2]-48;
int c=num[1]-48;
int d=num[0]-48;

int e=time[3]-48;
int f=time[2]-48;
int g=time[1]-48;
int h=time[0]-48;
 
int Num=(a+10*b+100*c+1000*d); // vettore-->numero con migliaia, centinaia etc
int Time=(e+10*f+100*g+1000*h);

Serial.println(Time);
Serial.println(Num);

}

but the results Num and Time don't seem to be integer:

the following part doesn't work:

void loop(){ 

  for(int i=0; i<=Num; i++){
    digitalWrite(shut, HIGH);
    delay(200);
    digitalWrite(shut,LOW);
    delay(Time*1000);   /// devo convertirlo dall'ASCII!!! come faccio?
  }

}
Serial.write(num[i]); // per leggere il valore vero e non l'ASCII

You need

Serial.print(num[i]);

if you want to see this on serial monitor.
Serial.write() is for sending the data to another device when you don't want ASCII versions of the data.
If you look at asciitable.com, the values 0-9 are all non-printing characters.

digitalWrite(shut,LOW);
    delay(Time*1000);   /// devo convertirlo dall'ASCII!!! come faccio?

Add a Serial.print(Time); here and see how long the delay should be.

Ok, thanks.
Doing this:

int a=num[3]-48;
int b=num[2]-48;
int c=num[1]-48;
int d=num[0]-48;

int Num=(a+10*b+100*c+1000*d); // vettore-->numero con migliaia, centinaia etc

does Num become an integer? example: num[0]=50 ----> d=2 etc..
than Num=3+410+7100+2*1000 is it equal to 2734? (I mean 2 thousand, 7 hundred and thirtyfour)
If it is so, why doen't it work in the void loop()??? damn

Are you missing something here?
for(int k=0; k<=3;){

k=k+1, or k++?
for(int k=0; k<=3; k=k+1){

Otherwise, k just stays 0, yes?

No, I put it in the if loop, so k=k+1 only if I press a key

for(int k=0; k<=3;){
        char kei= keypad.getKey();
        time[k]=kei; 
        if (kei!=NO_KEY){
          Serial.print(k);
          k++;     ////  <---HERE
        };
}

SOLVEDDD!!!!

The error was in char kei= keypad.getKey(); ]:slight_smile:

I have to define kei as an int, NOW IT WORKS!

Thank you VERY MUCH for replying my question XD

Bye