help with "saving" and printing keypad analog

Hello ,
I have connected my keypad using 1 analog input
everything is working as it should be
when I press “1” - the serial.print show me “1”
all good 0-9 number

but how can I show “*” and “#” ?

I want to see this chars and not the ASCII code of it (42 and 35)

what I need to change in my code? (in the code I have only number 0-3 and * # , just want to see it works)

int keyboardPin = 0;    // Analog input pin that the keypad is attached to
int keyboardValue = 0;   // value read from the keyboard
int keypressed = 0;
void setup(){


  Serial.begin(9600);  //hardware serial to PC
  delay (600);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}

void loop()
{

  keyboardValue=analogRead(keyboardPin);
  if (keyboardValue<25)
  {
    delay (50);
  }
  else
  {
    Serial.println(shownumber());
  }

}


int shownumber()
{
  keyboardValue=analogRead(keyboardPin);
  Serial.print("you press - ");
  Serial.println(keyboardValue);
  if (keyboardValue<25)
  {
    keypressed = 0;
  }
  if ((keyboardValue>25)&&(keyboardValue<67))
  {
    keypressed = 1;
  }
  if ((keyboardValue>67)&&(keyboardValue<108))
  {
    keypressed = 2;
  }
  if ((keyboardValue>108)&&(keyboardValue<162))
  {
    keypressed = 3;
  }
  if ((keyboardValue>819)&&(keyboardValue<889))
  {
    keypressed = '*';
  }
  if (keyboardValue>938)
  {
    keypressed = '#';
  }

  while (keyboardValue > 25) {
    delay (100);
    keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
  }//wait until key no longer being pressed before continuing
  delay (600);
  return keypressed;

}

Thanks ,

What keypad are you using? Can you post a link to its specifications?

...R

I have used this explain http://www.instructables.com/id/Arduino-3-wire-Matrix-Keypad/step2/Wiring-up-the-resistors/

and it's working as it said there my only problem is reading the "*" and "#"

what I want to do (in the end) is when I press a button I will enter a number and the "#" will be like enter(finish and return to the main code ) and "*" will erase the last digit. I'm sure it's not difficult - just need to know how to read and process * and #

Thanks ,

There are some issues with your code. The showNumber() function's name does not imply that it returns a value. It implies that it takes a value to show. Neither implication is true, so the name is crap.

The while loop at the end of showNumber() seems pointless. The differing return values (1, 2, 3, '', etc.) make using the value somewhere a real challenge. Return '1', '2', '', etc. as a char.

o.k
this is the original code I have found online ,it’s working.
I don’t mind working with this code - just need to return the value and be able to get the char

int keypressed = 0;
int keyboardPin = 0;    // Analog input pin that the keypad is attached to
int keyboardValue = 0;   // value read from the keyboard

void setup(){


  Serial.begin(9600);  //hardware serial to PC
  delay (600);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}


void loop(){


  keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
  while (keyboardValue < 25){
    //do nothing until a key is pressed
    keyboardValue = analogRead(keyboardPin); 
    delay(50);
  }//end of do nothing till a key is pressed


  readkeyboard(); //get the value of key being pressed "keypressed" i.e. 0-9

}



//read the keyboard routine
void readkeyboard(){
  keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
  if (keyboardValue <25){
    keypressed = 0;
  }
  if ((keyboardValue >25) && (keyboardValue < 67)){
    keypressed = 1;
  }
  if ((keyboardValue >67) && (keyboardValue < 108)){
    keypressed = 2;
  }
  if ((keyboardValue >108) && (keyboardValue < 162)){
    keypressed = 3;
  }
  if ((keyboardValue >162) && (keyboardValue < 253)){
    keypressed = 4;
  }
  if ((keyboardValue >253) && (keyboardValue < 361)){
    keypressed = 5;
  }
  if ((keyboardValue >361) && (keyboardValue < 479)){
    keypressed = 6;
  }
  if ((keyboardValue >479) && (keyboardValue < 619)){
    keypressed = 7;
  }
  if ((keyboardValue >619) && (keyboardValue < 765)){
    keypressed = 8;
  }
  if ((keyboardValue >765) && (keyboardValue < 819)){
    keypressed = 9;
  }
  if ((keyboardValue >819) && (keyboardValue < 889)){
    keypressed = '*';
  }//press *
  if ((keyboardValue >889) && (keyboardValue < 938)){
    keypressed = 0;
  }
  if (keyboardValue >938){
    keypressed = '#';
  }//press #
  //NOTE: the values used above are all halfway between the value obtained with each keypress in previous test sketch 

  while (keyboardValue > 25) {
    delay (100);
    keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
  }//wait until key no longer being pressed before continuing



  Serial.println(keypressed);      // print the value back to the Serial view window on your PC
  delay(1000);                     // wait 1000 milliseconds before the next loop
}
//end of read the keyboard routine

this is the original code I have found online

It, unfortunately, is still crap. readKeyboard() implies that it will return a value, It does not. Overuse of globals is NOT a good idea.

What are you going to do with the value you get? Why are you using 1, 2, 3, '', '#', etc. Make the return value consistent - '1', '2', '3', '', '#', etc.

so to what are you suggestion to change the name to? getpress() ?I don't see what is the different - is just a name. I don't understand what you want me to do... I have wrote all numbers as '1' , '2'... and now I don't see nothing - just the ASCII of it ,and no so good (it have mistakes) if you can explain to me and not just to say "it's still no good " it will be nice

this is want I my code to do this : 1.write a number of 3 digits using the keypad 000--999, when * will be use to erase the last digit ( let say you wrote 12 and you want to write 13 . so if you press * , you will you will see 1) and # will be "finish" and "save" the number in a new int for future use. 2. show the numbers on a serial 7 segments - https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Serial-7-Segment-Display-Datasheet. 3. to be able to use the number for comparing another number I get from a sensor . for example the sensor give me 653 and I wrote 600 so it will light a red led

is this information help you to understand what I want to do?

Thanks ,

To address the specific problem of printing ASCII codes instead of the character, try changing your Serial.print(keypressed) to Serial.write(keypressed).

Your previous code returned the numeric value 0, 1, 2, etc for the numerals, but the ASCII codes for '*' and '#'. PaulS's recommendation was to make the returned value [u]consistently[/u] the ASCII value of the key pressed. And then you need to change how you display it.

I don't see what is the different - is just a name

Having names that accurately reflect what a variable represent, or what a function does and what it returns, helps other people (and indeed the person who wrote the code) understand it and spot bugs, or make changes in the future. It's about improving the maintainability.

Regards

Ray

O.K
so I have start from the beginning
this is the full code only on loop()
now the numbers are correct , and when I press 1 - I can see in the serial print “1” and also # and *.
thank you .
this is the code:

#include <Wire.h>
int keypressed = 0;
int keyboardPin = 0;    // Analog input pin that the keypad is attached to

void setup(){


  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}


void loop()
{
  keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
  keyboardValue=map(keyboardValue,0,1023,0,255);
  if (keyboardValue < 8)
  { //do nothing until a key is pressed
    delay(50);
    //end of do nothing till a key is pressed
  }
  else
  {
    Serial.print("this is what you have press - ");
    Serial.println(keyboardValue);
    delay(500);
    if ((keyboardValue >8) && (keyboardValue < 12)){
      Serial.println(keyboardValue);
      keypressed = '1';
    }
    if ((keyboardValue >12) && (keyboardValue < 30)){
      Serial.println(keyboardValue);
      keypressed = '2';
    }
    if ((keyboardValue >30) && (keyboardValue < 48)){
      Serial.println(keyboardValue);
      keypressed = '3';
    }
    if ((keyboardValue >49) && (keyboardValue < 67)){
      Serial.println(keyboardValue);
      keypressed = '4';
    }
    if ((keyboardValue >68) && (keyboardValue < 102)){
      Serial.println(keyboardValue);
      keypressed = '5';
    }
    if ((keyboardValue >103) && (keyboardValue < 137)){
      Serial.println(keyboardValue);
      keypressed = '6';
    }
    if ((keyboardValue >138) && (keyboardValue < 158)){
      Serial.println(keyboardValue);
      keypressed = '7';
    }
    if ((keyboardValue >158) && (keyboardValue < 195)){
      Serial.println(keyboardValue);
      keypressed = '8';
    }
    if ((keyboardValue >195) && (keyboardValue < 208)){
      Serial.println(keyboardValue);
      keypressed = '9';
    }
    if ((keyboardValue >208) && (keyboardValue < 210)){
      Serial.println(keyboardValue);
      keypressed = '*';
    }//press *
    if ((keyboardValue >210) && (keyboardValue <220)){
      Serial.println(keyboardValue);
      keypressed = '0';
    }
    if (keyboardValue >220){
      Serial.println(keyboardValue);
      keypressed = '#';
    }//press #
    //NOTE: the values used above are all halfway between the value obtained with each keypress in previous test sketch 
    Serial.println("   ");
    Serial.write("     ");
    Serial.write(keypressed);      // print the value back to the Serial view window on your PC
    Serial.println("  ");
    delay(500);                    
  }
}

now my question is -
how do I make this a sub program ?
if I declare it as

char whatIPress()

I get an error

do I understand what I’m asking?

so what should I do?

Hi David

Can you please post the code where you try to turn it into a function. And also please copy and paste the error message.

Thanks

Ray

I have found my mistake
but now I’m having another problem
I don’t mange to create a 3 digits number ,
now that I can read every digit(number) by it self I want to create a 3 digit number and then to compare it to a known value.
so I have created a char array of 4 (3 digits and end signal - “#”) , when it see the ‘#’ he will know this is the final number.
example - when I will press 1,5,9,# the FullNumber will show my 159
and I will be able to compare it to the value .

#include <Wire.h>
int keypressed = 0;
int keyboardPin = 0;    // Analog input pin that the keypad is attached to
char temp_digit=0;
int keyboardValue=0;
char FullNumber [4];
int i=0;

void setup(){
  delay(1000);
  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}


void loop()
{//start of loop

    keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
  keyboardValue=map(keyboardValue,0,1023,0,255);
  if (keyboardValue < 9)
  { //do nothing until a key is pressed
    delay(50);
    //end of do nothing till a key is pressed
  }
  else
    temp_digit=readingKeypad(); 
  Serial.print("the button that was press is - ");
  Serial.println(temp_digit);
    if (temp_digit='*')//erase last digit
  {
    FullNumber[i]='0';
    i--;
  }
  if (temp_digit='#')//finish the number
  {
    FullNumber[3]='#';
  }
  else
  {//entering the number 
    FullNumber[i]=temp_digit;
    i++;
  }

  Serial.print("The final number is - ");
  Serial.println(FullNumber);

if (FullNumber > 300)
{
  Serial.println("GREEN LED IS ON");
}
else if ((FullNumber<600)&&(FUllNumber>350))
{
  Serial.println("RED LED IS ON");
}

  delay(500);

}//end of loop

char readingKeypad()
{//start of sub code
  Serial.print("this is what you have press - ");
  Serial.println(keyboardValue);
  delay(500);
  if ((keyboardValue >9) && (keyboardValue < 12)){
    Serial.println(keyboardValue);
    keypressed = '1';
  }
  if ((keyboardValue >12) && (keyboardValue < 30)){
    Serial.println(keyboardValue);
    keypressed = '2';
  }
  if ((keyboardValue >30) && (keyboardValue < 48)){
    Serial.println(keyboardValue);
    keypressed = '3';
  }
  if ((keyboardValue >49) && (keyboardValue < 67)){
    Serial.println(keyboardValue);
    keypressed = '4';
  }
  if ((keyboardValue >68) && (keyboardValue < 102)){
    Serial.println(keyboardValue);
    keypressed = '5';
  }
  if ((keyboardValue >103) && (keyboardValue < 137)){
    Serial.println(keyboardValue);
    keypressed = '6';
  }
  if ((keyboardValue >138) && (keyboardValue < 158)){
    Serial.println(keyboardValue);
    keypressed = '7';
  }
  if ((keyboardValue >158) && (keyboardValue < 195)){
    Serial.println(keyboardValue);
    keypressed = '8';
  }
  if ((keyboardValue >195) && (keyboardValue < 208)){
    Serial.println(keyboardValue);
    keypressed = '9';
  }
  if ((keyboardValue >208) && (keyboardValue < 210)){
    Serial.println(keyboardValue);
    keypressed = '*';
  }//press *
  if ((keyboardValue >210) && (keyboardValue <220)){
    Serial.println(keyboardValue);
    keypressed = '0';
  }
  if (keyboardValue >220){
    Serial.println(keyboardValue);
    keypressed = '#';
  }//press #
  delay(1000);    
  // wait 1000 milliseconds before the next loop

  return keypressed;

}//end of sub_code

this is the error I’m getting

keypad:55: error: ISO C++ forbids comparison between pointer and integer

Thanks ,

You are forgetting that FullNumber is a char array and not a number. You need to convert it to a number before you can compare it with 300 (for example).

I think the way to do that isint myNumber = atoi(FullNumber);and then you can do if (myNumber > 300);

"atoi()" is ascii to integer

...R

I have use the command - thank you (forgot about it)
but the problems continue :slight_smile:

  1. now I’m cheeking if the final digit is “#” - if so to show the number , it’s doesn’t work
    I take only 3 out of the 4 (only the number I have read) , but I don’t get the number-so I can’t compare it and work with him. why?
  2. when I press * - I want it to delete the last digit - but he don’t do it , I can see I press * - and the code knowledge it - but it won’t do what it suppose to do , why ?

Thanks .

#include <Wire.h>
int keypressed = 0;
int keyboardPin = 0;    // Analog input pin that the keypad is attached to
char temp_digit=0;
int keyboardValue=0;
char TempNumber [4];
int i=0;

void setup(){
  delay(1000);
  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}


void loop()
{//start of loop

    keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
  keyboardValue=map(keyboardValue,0,1023,0,255);
  if (keyboardValue < 9)
  { //do nothing until a key is pressed
    delay(50);
        //end of do nothing till a key is pressed
  }
  else
  {//what to do if analog is bigger then 9 - means that button was press 
    temp_digit=readingKeypad(); 
    Serial.print("the button that was press is - ");
    Serial.println(temp_digit);
     if (temp_digit=='*')
    {//if press * - remove last digit from final number
      TempNumber[i]=NULL;
      i--;
    }
    else 
    {
      TempNumber[i]=temp_digit;
    i++;
    }
Serial.write(TempNumber);
  }//end  of entering the number 

if (TempNumber[3]=='#')//wait until # is in the end of the number do start of processing the number
{ 
  i=0;
    char realnumber [2];// only 3 digit 
  for (int t=2;t<3;t++)
  {
     realnumber[t]=TempNumber[t];
    t++;
      }
      Serial.write(realnumber);
 TempNumber[4]=NULL;
  int Final_Number=atoi(realnumber);//convert from ASCII to integer
 Serial.print("The final number is - ");
 Serial.println(Final_Number);
  if (Final_Number > 300)
  {
    Serial.println("GREEN LED IS ON");
  }
  
}//end of processing the number 
    
else 
 
  delay(500);

}//end of loop

char readingKeypad()
{//start of sub code
  Serial.print("this is what you have press - ");
  Serial.println(keyboardValue);
  delay(500);
  if ((keyboardValue >9) && (keyboardValue < 12)){
    Serial.println(keyboardValue);
    keypressed = '1';
  }
  if ((keyboardValue >12) && (keyboardValue < 30)){
    Serial.println(keyboardValue);
    keypressed = '2';
  }
  if ((keyboardValue >30) && (keyboardValue < 48)){
    Serial.println(keyboardValue);
    keypressed = '3';
  }
  if ((keyboardValue >49) && (keyboardValue < 67)){
    Serial.println(keyboardValue);
    keypressed = '4';
  }
  if ((keyboardValue >68) && (keyboardValue < 102)){
    Serial.println(keyboardValue);
    keypressed = '5';
  }
  if ((keyboardValue >103) && (keyboardValue < 137)){
    Serial.println(keyboardValue);
    keypressed = '6';
  }
  if ((keyboardValue >138) && (keyboardValue < 158)){
    Serial.println(keyboardValue);
    keypressed = '7';
  }
  if ((keyboardValue >158) && (keyboardValue < 195)){
    Serial.println(keyboardValue);
    keypressed = '8';
  }
  if ((keyboardValue >195) && (keyboardValue < 208)){
    Serial.println(keyboardValue);
    keypressed = '9';
  }
  if ((keyboardValue >208) && (keyboardValue < 210)){
    Serial.println(keyboardValue);
    keypressed = '*';
  }//press *
  if ((keyboardValue >210) && (keyboardValue <220)){
    Serial.println(keyboardValue);
    keypressed = '0';
  }
  if (keyboardValue >220){
    Serial.println(keyboardValue);
    keypressed = '#';
  }//press #
  delay(1000);    
  // wait 1000 milliseconds before the next loop

  return keypressed;

}//end of sub_code
  keyboardValue=map(keyboardValue,0,1023,0,255);

That’s an expensive way to divide by 4.

{//start of loop

Stop it! That crap obscures the {. The comment is useless. We can all SEE that this is the start of loop.

  if (keyboardValue < 9)
  { //do nothing until a key is pressed
    delay(50);
        //end of do nothing till a key is pressed
  }

The code does NOT match the comment. Do NOTHING means DO NOTHING. It does NOT mean “stick your thumb up your *** and wait a while”.

    temp_digit=readingKeypad();

Isyourspacekeybroken. Therearestepsyoucantaketomakeyourcodereadablethatcostnothing.

Serial.write(TempNumber);

This function expects a NULL terminated array of chars. Under some circumstances, TempNumber is a NULL terminated array. Under other circumstances, it is not. When it isn’t, you should not call this function.

    char realnumber [2];// only 3 digit

Another useless AND incorrect comment.

  for (int t=2;t<3;t++)
  {

Quick. How many integral values are equal to two and less than three?

    t++;

WTF? Why are you diddling with the loop index inside the loop?

I quit reading at this point.

@david_david, Stuffing all the code into loop() is fine when there are only 10 or so lines of code and it only does one thing. Your code has now become unmanageable and I strongly suspect that is making it difficult for you to make progress, as well as for us to help.

You need to divide up your code into several short functions each of which is limited to a single activity. Then the only code in loop() will be calls to these functions.

If something is not working you (and we) can then focus attention on a small piece of code.

...R

  1. I’m writing the comments for me - so I will know where do I begin and where do I stop
    it’s for me only to avoid mistakes , to understand what belong to what. if there is a way to publish the code without the comments - let me know and I will do this. again this is just for me.
  2. I though that delay(50) is like doing nothing - but maybe I mistaken , so how should I write this ? how to use the “while” in a smart way ?
    3.for now on I will make all my variables in this form “Temp_Something” - I will change it also ,so it will be nicer to read. no problem.
  3. I will remove this - Serial.write(TempNumber);. I thought this way I will see what I have inside the char ,but I guess I made a mistake - no problem.
    5.why this is char realnumber [2];// only 3 digit incorrect? doesn’t it create a new char variable with size of 3? 0,1,2?
    explain why this is wrong.
    6.yes yes yes , I see my mistake , didn’t notice it meant to write
    for (int t=0;t<3;t++) - sorry , this one is a very stupid mistake .
    7.again my mistake - I started to write this part in another way , then I decided to make it a loop - and didn’t notice

this is the code I have now

#include <Wire.h>
int Key_Press= 0;
int Keyboard_Pin = 0;    // Analog input pin that the keypad is attached to
char Temp_Digit=0;
int Key_Value=0;
char Temp_Number [4];
int i=0;

void setup(){
  delay(1000);
  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<6;i++)
  {
    Serial.print("*-");
    delay(1000);
  }
  Serial.println("Done!");
}


void loop()
{//start ofTemp_Number loop

    Key_Value = analogRead(Keyboard_Pin); // read the keyboard value (0 - 1023)
  Key_Value=map(Key_Value,0,1023,0,255);
  while  (Key_Value < 9)
  { //do nothing until a key is pressed
   break;
        //end of do nothing till a key is pressed
  }

  //what to do if analog is bigger then 9 - means that button was press 
    Temp_Digit=Read_Keypad(); 
    Serial.print("the button that was press is - ");
    Serial.println(Temp_Digit);
     if (Temp_Digit=='*')
    {//if press * - remove last digit from final number
      Temp_Number[i]=NULL;
      i--;
    }
    else 
    {
      Temp_Number[i]=Temp_Digit;
    i++;
    }
Serial.write(Temp_Number);
  //end  of entering the number 

if (Temp_Number[3]=='#') //wait until # is in the end of the number do start of processing the number
{ 
  i=0;
  char Real_Number [2];// only 3 digit 
  for (int t=0;t<3;t++)
  {
     Real_Number[t]=Temp_Number[t];
    
      }
    Temp_Number[3]=NULL;//will clean the # 
  int Final_Number=atoi(Real_Number); //convert from ASCII to integer
 Serial.print("The final number is - ");
 Serial.println(Final_Number);
  if (Final_Number > 300)
  {
    Serial.println("GREEN LED IS ON");
  }
  
}//end of processing the number 
     
  delay(500);

}//end of loop

char Read_Keypad()
{//start of sub code
  Serial.print("this is what you have press - ");
  Serial.println(Key_Value);
  delay(500);
  if ((Key_Value >9) && (Key_Value < 12)){
    Serial.println(Key_Value);
    Key_Press = '1';
  }
  if ((Key_Value >12) && (Key_Value < 30)){
    Serial.println(Key_Value);
    Key_Press = '2';
  }
  if ((Key_Value >30) && (Key_Value < 48)){
    Serial.println(Key_Value);
    Key_Press = '3';
  }
  if ((Key_Value >49) && (Key_Value < 67)){
    Serial.println(Key_Value);
    Key_Press = '4';
  }
  if ((Key_Value >68) && (Key_Value < 102)){
    Serial.println(Key_Value);
    Key_Press = '5';
  }
  if ((Key_Value >103) && (Key_Value < 137)){
    Serial.println(Key_Value);
    Key_Press = '6';
  }
  if ((Key_Value >138) && (Key_Value < 158)){
    Serial.println(Key_Value);
    Key_Press = '7';
  }
  if ((Key_Value >158) && (Key_Value < 195)){
    Serial.println(Key_Value);
    Key_Press = '8';
  }
  if ((Key_Value >195) && (Key_Value < 208)){
    Serial.println(Key_Value);
    Key_Press = '9';
  }
  if ((Key_Value >208) && (Key_Value < 210)){
    Serial.println(Key_Value);
    Key_Press = '*';
  }//press *
  if ((Key_Value >210) && (Key_Value <220)){
    Serial.println(Key_Value);
    Key_Press = '0';
  }
  if (Key_Value >220){
    Serial.println(Key_Value);
    Key_Press = '#';
  }//press #
  delay(1000);    
  // wait 1000 milliseconds before the next loop

  return Key_Press;

}//end of sub_code

Hi ,I have manage to make it work
when I press “*” it delete the last digit
when I press “#” in the last digit it make compering and I can use the number as I want .
also when I connect PIN 7 to GND - all the data get erase, like reset button.
now if you can please take a look at the code and explain and show me if I can make it better?
less lines? better function to use? less chance to get stuck ?
I want to learn how to make it better ( all the serial.print\comments\blinking of the LED are just for me to debug it , - so please feel free to ignore then…-this is just for me to understanding )

Thanks ,

#include <Wire.h>
int Key_Press= 0;
int Keyboard_Pin = 0;    // Analog input pin that the keypad is attached to
char Temp_Digit=0;
int Key_Value=0;
char Temp_Number [4];
int i=0;
int Reset_Reading=7;
int Final_Number=0;

void setup()
{
  pinMode(Reset_Reading,INPUT_PULLUP);
  pinMode(13,OUTPUT);

  delay(500);
  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<4;i++)
  {
    digitalWrite(13, HIGH);
    Serial.print("*-");
    delay(1000);
    digitalWrite(13, LOW);
    delay(500);
  }
  Serial.println("Done!");
  digitalWrite(13, LOW);
}

void loop()
{//start of the main-loop
  if (digitalRead(Reset_Reading)==HIGH)
  {//reset all the numbers
  digitalWrite(13, HIGH);
    Serial.println("wait until reset all numbers ");
    Final_Number=0;
    i=0;
    Temp_Number[4]=NULL;
    delay(700);
    digitalWrite(13, LOW);
    Serial.println("Done");
    goto end_of_loop;
  }
  else 
  {//start working on keypad
    Key_Value = analogRead(Keyboard_Pin); // read the keyboard value (0 - 1023)
    Key_Value=map(Key_Value,0,1023,0,255);
    if (Key_Value<9)
    {//go to the end of the code- nothing is press
      goto end_of_loop;
    }
    else 
    {//somthing is press

      Temp_Digit=Read_Keypad(); 
      Serial.print("the button that was press is - ");
      Serial.println(Temp_Digit); // getting the value and the showing the button that was press
      if (Temp_Digit=='*')
      {
        Serial.println("Remove last digit!!!");
        Temp_Number[i]=NULL;
        Temp_Number[i-1]=NULL;
        i--;
      }
      else 
      {
        Temp_Number[i]=Temp_Digit;
        i++;
        if (i==4) //bring back to last digit 
        {
          i=3;
        }
      }
           
      Serial.print("the number so far is - ");
      Serial.println(Temp_Number); 
    }    //end of press

    if (Temp_Number[3]=='#')
    {//the number is ready to work 
      i=0;
      char Real_Number[2];//new char for final number 3 digit 
      for (int t=0;t<3;t++)
      {
        Real_Number[t]=Temp_Number[t];
      }
      Final_Number=atoi(Real_Number);
      Serial.print("the final number is - ");
      Serial.println(Final_Number);
      Temp_Number[4]=NULL;
    }//the number is ready to work
    else 
    {//what to do if the last char isn't # 
      goto end_of_loop;
    }
  }//end of working on keypad

  if (Final_Number<500)
  {
    Serial.println("B-L-I-N-K !!!!!");
    for (int y=0;y<4;y++)
    {   
      digitalWrite(13, HIGH);
      delay (300);
      digitalWrite(13,LOW);
      delay(300);
    }//blink 

  }



end_of_loop :
  delay(50);
}//end of the main-loop


char Read_Keypad()
{//start of sub code
  Serial.print("this is what you have press - ");
  Serial.println(Key_Value);
  delay(500);
  if ((Key_Value >9) && (Key_Value < 12)){
    Serial.println(Key_Value);
    Key_Press = '1';
  }
  if ((Key_Value >12) && (Key_Value < 30)){
    Serial.println(Key_Value);
    Key_Press = '2';
  }
  if ((Key_Value >30) && (Key_Value < 48)){
    Serial.println(Key_Value);
    Key_Press = '3';
  }
  if ((Key_Value >49) && (Key_Value < 67)){
    Serial.println(Key_Value);
    Key_Press = '4';
  }
  if ((Key_Value >68) && (Key_Value < 102)){
    Serial.println(Key_Value);
    Key_Press = '5';
  }
  if ((Key_Value >103) && (Key_Value < 137)){
    Serial.println(Key_Value);
    Key_Press = '6';
  }
  if ((Key_Value >138) && (Key_Value < 158)){
    Serial.println(Key_Value);
    Key_Press = '7';
  }
  if ((Key_Value >158) && (Key_Value < 195)){
    Serial.println(Key_Value);
    Key_Press = '8';
  }
  if ((Key_Value >195) && (Key_Value < 208)){
    Serial.println(Key_Value);
    Key_Press = '9';
  }
  if ((Key_Value >208) && (Key_Value < 210)){
    Serial.println(Key_Value);
    Key_Press = '*';
  }//press *
  if ((Key_Value >210) && (Key_Value <220)){
    Serial.println(Key_Value);
    Key_Press = '0';
  }
  if (Key_Value >220){
    Serial.println(Key_Value);
    Key_Press = '#';
  }//press #
  delay(1000);    
  // wait 1000 milliseconds before the next loop

  return Key_Press;

}//end of sub_code

Thanks ,

    Temp_Number[4]=NULL;

Temp_Number is an array with 4 elements, numbered 0 to 3. You just wrote beyond the end of your array.

    goto end_of_loop;

No! Get rid of this crap. You are not programming in Basic any more.

    Key_Value=map(Key_Value,0,1023,0,255);

That’s a pretty expensive way to accomplish:

    Key_Value /= 4;
    if (Key_Value<9)
    {//go to the end of the code- nothing is press
      goto end_of_loop;
    }

No! Deal with the useful case of Key_Value >= 9. Get rid of the goto.

    if (Temp_Number[3]=='#')
    {//the number is ready to work

So, you are dealing with 3 digit numbers?

      char Real_Number[2];//new char for final number 3 digit

How are you going to store three characters AND a terminating NULL in a 2 element array?

      for (int t=0;t<3;t++)
      {
        Real_Number[t]=Temp_Number[t];
      }

Why are you then copying 4 characters into the two element array?

      Final_Number=atoi(Real_Number);

Since the 4th character in the array is a ‘#’, there is no NULL. So, Real_Number is NOT a string. Do not pass non-strings to functions that expect strings.

      Temp_Number[4]=NULL;

Again, writing beyond the end of the array is not a good idea. Ever.

  Serial.print("this is what you have press - ");
  Serial.println(Key_Value);

Really? Why is that at the top of the function to get the key that was pressed?

  delay(1000);    
  // wait 1000 milliseconds before the next loop

Why?
There
is
no
earthly
reason
for
this
crap!

Other than that, great job.

first of all - thank you for informative answer :slight_smile:
now , I don’t understand a few things :

  1. Temp_Number is an array with 4 elements -so how do I clean it ?how do I make it reset? if I write
    Temp_Number[4]=0; - is it good?
  2. I will get rid of the goto command, so if I will change the if to
    if (Key_Value=>9) only , will it be O.K ? and just ignore the situation where (Key_Value<9)? will this be good?
    3.char Real_Number[2];//new char for final number 3 digit - my mistake , should be written [3].
  3. so I don’t need to use this ? Final_Number=atoi(Real_Number);, but in earlier post you\Robin2 told me to use this function to change from char to int. or I misunderstood what need to be done?
    just need to understand this issues…
    this is the code now "
#include <Wire.h>
int Key_Press= 0;
int Keyboard_Pin = 0;    // Analog input pin that the keypad is attached to
char Temp_Digit=0;
int Key_Value=0;
char Temp_Number [4];
int i=0;
int Reset_Reading=7;
int Final_Number=0;

void setup()
{
  pinMode(Reset_Reading,INPUT_PULLUP);
  pinMode(13,OUTPUT);

  delay(500);
  Serial.begin(9600);  //hardware serial to PC
  delay (1000);
  Serial.println("Please wait.. ");
  for (int i=0;i<4;i++)
  {
    digitalWrite(13, HIGH);
    Serial.print("*-");
    delay(1000);
    digitalWrite(13, LOW);
    delay(500);
  }
  Serial.println("Done!");
  digitalWrite(13, LOW);
}

void loop()
{//start of the main-loop
  if (digitalRead(Reset_Reading)==HIGH)
  {
    Reset_Number();
  }
  else 
  {//start working on keypad
    Key_Value = analogRead(Keyboard_Pin); // read the keyboard value (0 - 1023)
    Key_Value=map(Key_Value,0,1023,0,255);
    if (Key_Value>=9)

    {//somthing is press

      Temp_Digit=Read_Keypad(); 
      Serial.print("the button that was press is - ");
      Serial.println(Temp_Digit); // getting the value and the showing the button that was press
      if (Temp_Digit=='*')
      {
        Serial.println("Remove last digit!!!");
        Temp_Number[i]=NULL;
        Temp_Number[i-1]=NULL;
        i--;
      }
      else 
      {
        Temp_Number[i]=Temp_Digit;
        i++;
        if (i==4) //bring back to last digit 
        {
          i=3;
        }
      }

      Serial.print("the number so far is - ");
      Serial.println(Temp_Number); 
    }    //end of press

    if (Temp_Number[3]=='#')
    {//the number is ready to work 
      i=0;
      char Real_Number[3];//new char for final number 3 digit 
      for (int t=0;t<3;t++)
      {
        Real_Number[t]=Temp_Number[t];
      }
      Final_Number=atoi(Real_Number);
      Serial.print("the final number is - ");
      Serial.println(Final_Number);
      Temp_Number[4]=0;
    }//the number is ready to work
    //    else 
    //    {//what to do if the last char isn't # 
    //      goto end_of_loop;
    //    }
  }//end of working on keypad

  if (Final_Number<500)
  {
    Serial.println("B-L-I-N-K !!!!!");
    for (int y=0;y<4;y++)
    {   
      digitalWrite(13, HIGH);
      delay (300);
      digitalWrite(13,LOW);
      delay(300);
    }//blink 

  }

  delay(50);
}//end of the main-loop


char Read_Keypad()
{//start of sub code
  Serial.print("this is what you have press - ");
  Serial.println(Key_Value);
  delay(500);
  if ((Key_Value >9) && (Key_Value < 12)){
    Serial.println(Key_Value);
    Key_Press = '1';
  }
  if ((Key_Value >12) && (Key_Value < 30)){
    Serial.println(Key_Value);
    Key_Press = '2';
  }
  if ((Key_Value >30) && (Key_Value < 48)){
    Serial.println(Key_Value);
    Key_Press = '3';
  }
  if ((Key_Value >49) && (Key_Value < 67)){
    Serial.println(Key_Value);
    Key_Press = '4';
  }
  if ((Key_Value >68) && (Key_Value < 102)){
    Serial.println(Key_Value);
    Key_Press = '5';
  }
  if ((Key_Value >103) && (Key_Value < 137)){
    Serial.println(Key_Value);
    Key_Press = '6';
  }
  if ((Key_Value >138) && (Key_Value < 158)){
    Serial.println(Key_Value);
    Key_Press = '7';
  }
  if ((Key_Value >158) && (Key_Value < 195)){
    Serial.println(Key_Value);
    Key_Press = '8';
  }
  if ((Key_Value >195) && (Key_Value < 208)){
    Serial.println(Key_Value);
    Key_Press = '9';
  }
  if ((Key_Value >208) && (Key_Value < 210)){
    Serial.println(Key_Value);
    Key_Press = '*';
  }//press *
  if ((Key_Value >210) && (Key_Value <220)){
    Serial.println(Key_Value);
    Key_Press = '0';
  }
  if (Key_Value >220){
    Serial.println(Key_Value);
    Key_Press = '#';
  }//press #

  return Key_Press;

}//end of sub_code

void Reset_Number ()
{
  digitalWrite(13, HIGH);
  Serial.println("wait until reset all numbers ");
  Final_Number=0;
  i=0;
  Temp_Number[4]=0;
  delay(700);
  digitalWrite(13, LOW);
  Serial.println("Done");
}

Thanks ,

so how do I clean it ?how do I make it reset? if I write...is it good?

Do you know what NULL terminated char array means? The NULL is a stop sign to functions that expect NULL terminated arrays of chars. Can you imagine where to place the stop sign in the array to tell a function that expects a string to see the string as empty? Does "position 0" suggest itself? It should. No, [4] is not good.

so if I will change the if to...only , will it be O.K ?

Probably. But, you need to review the code and decide for yourself.

so I don't need to use this ?

You do need to call that function, but you must call it with a string, not a char array.