[FIXED] 3-digit numbers entry via keypad Problem

Hello,

I am working on a Project, where I need to rechieve a 3-digit number. I want the final number to be displayed (via servial monitor) if I press "#" and reset the number back to = if "*" is pressed.

I am using this Keypad: EXP-R05-080

This is what I have sofar:

 #include <Keypad.h>

int n = 0;
int h;
int z;
int e;
int m;

const byte ROWS = 4;  //four rows
const byte COLS = 3; //three columns

char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {5, 6, 7, 0};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4};  //connect to the column pinouts of the keypad

Keypad
keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

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

}

 void loop() {
    char key = keypad.getKey();
if (key != NO_KEY){
getNumbers;

}

 }

void getNumbers(){
  n = 1;
  char key = keypad.getKey();

  if (key != NO_KEY){
  while ( n <= 3){
        switch (n){
          case 1:
            h = key; n++;
            break;
          case 2:
            z = key; n++;
            break;
          case 3:
            e = key; n++;
            break;
          
            
        }
  }
  }
      
        
        
        //multiplying each digit so as to form a 3 digit number hundreds + tenths + ones
        h = h * 100;
        z = z * 10;
        
        // finding the final 3 digit number
        m = h + z + e;

        
        Serial.println(m);
      
        
           
}

If you happen to have a different code prewriten for this use, feel free to show me!

Thanks:)

And the behavior is?

Do you understand why?

Have you try to go step by step through the program manually to see what it does?

The serial Monitor doesn't show anything, nomatter what I enter on the Keypad and I don't know why...
I know that the Programm works fine, if I just print key.

void getNumbers(){
  n = 1;
  char key = keypad.getKey();

  if (key != NO_KEY){
  while ( n <= 3){
        switch (n){
          case 1:
            h = key; n++;
            break;
          case 2:
            z = key; n++;
            break;
          case 3:
            e = key; n++;
            break;
          
            
        }
  }
  }
      
        
        
        //multiplying each digit so as to form a 3 digit number hundreds + tenths + ones
        h = h * 100;
        z = z * 10;
        
        // finding the final 3 digit number
        m = h + z + e;

        
        Serial.println(m);
      
        
           
}

This part is then supposed to add 1 to n every button push and then switch case to declare h, z, e after eachother to then add the digits together and recieve a final 3-digit number. At the end it's supposed to print m (the final number).

Where do you read your second and third key? Should that be in the loop?

my second and third key are read in getNumbers.

first key = h
second key = z
third key = e

What is called from the main loop?
How is n set if by chance the function was called?

in the main loop I check if a button got pressed. If so I execute getNumber();

I think that it'll be best if I write a completly new pragramm using a different principle.
I'll update you, once I have some new Infos:)

Where do you call the getNumbers() function?

LUCASB276:
in the main loop I check if a button got pressed. If so I execute getNumber();

Actually you don't call it from the main loop as you don't have parenthesis next to the function name

That's part of the problem you check key pressed there, it might have changed when you check it again in the function, there is no debounce but the critical piece you need to look at is how you handle the variable n which drives your state management (tells you if you are getting char 1,2 or 3)

At this Stage n always starts at 1 and you while will just go through your 3 stages without even reading a new key press...

If you try to walk that code manually you'll see its fundamentally broken

I wrote a completely new Program, that works now! :slight_smile:
Thanks for all the help everybody and here's the code, incase you're interested :smiley:

int h;
int z;
int e;
int m;
int a;
#include "Keypad.h"

// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
 {{'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}};
 
byte rowPins[ROWS] = {5, 6, 7, 0};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4};  //connect to the column pinouts of the keypad
 
int count=1;
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    int a = key - '0';                             //char key in int a umwandeln
    Serial.println(count);
    Serial.println("a=" + String(a));
    switch (count){
         case 1:
           if (key != NO_KEY){
           h = a * 100;
            Serial.println("h=" + String(h));}
           break;
         case 2:
           if (key != NO_KEY){
           z = a * 10;
            Serial.println("z=" + String(z));}
           break;
         case 3:
           if (key != NO_KEY){
           e = a;
            Serial.println("e=" + String(e));}
           break;  
       }
       m = h + z + e;
        Serial.println("m=" + String(m));
    count++;                                          //adds 1 to count
    if (count==4)                                     //checks for count=4
    {
      Serial.println();
      count=1;                                        //resetes count
    }
  }



 
}

good job

a few comments


         case 1:
           if (key != NO_KEY){

when you do that for each case it's useless as you are already within the execution of an englobing if (key != NO_KEY) and you have not modified the key value. so you are sure this is always true. you can get rid of that test in each case statement.

it's usually good practice to capture a default case for input error treatment. for example that's what would be called when case = 4 so you don't need to do your cheek if (count==4) after the switch case.


    if (count==4)                                     //checks for count=4
    {
      Serial.println();
      count=1;                                        //resetes count
    }

you should also reset m,h,z,e to zero.

side question: does your keypad only has number? don't you have the # or * keys? what happens if you press those?