keypad

Hello that such, I have a small problem on having wanted to store the keys pressed in a variable, alone this works if only I press an alone key, if I press in the keyboard 123 does not guard it in the variable, already try a bit of everything and I do not achieve it.

Can they help or orientate me that it is what podria to do me?

#include <Keypad.h>

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, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}
  
void loop(){
  char key = keypad.getKey();
   
  int n = tecla - 48;

  if (key != NO_KEY ){
    
   if(n == 123)
   {
        digitalWrite(13, HIGH);
        Serial.println(n);
   }

  }
}

I go some days with this code and cannot make it work hereby.

What you want to do is:
First, read the keypad, which you have done.

Second, see if a button is pressed, which again you have already done with if (key != NO_KEY )

Third, you want to convert the chars into an actual value. This is done simply with, n = n * 10 + (key - '0');. Where "(key - '0')" makes the char into a value by subtracting the char by 48 or '0'. Then adding it to "n * 10", where n must be set to zero before you add the converted value. Now once that is done, every value afterwards will concatenate into n, UNTIL, n is once again set back to zero.

Example using the serial monitor. Enter 123. or 25.

int num = 0;

void setup()
{
  Serial.begin(115200);
}

void loop() 
{
  if(Serial.available() > 0)
  {
    char c = Serial.read();
    //if(c != NO_KEY) // For keypad library
    //{
      if(c == '.') // if char == '.' then show the value. NOTE this can be changed to if(c == '*') or if(c == '#')
      {
        Serial.println(num);
        num = 0; // once shown reset num back to 0
      }
      else // char was not a period, so store the chars and convert them into num.
       num = num * 10 + (c - '0');
    //}
  }
}

Hi, I already made the code as you mentioned, but instead when I do the 4x4 keypad only thing I get when pressing ‘*’ I only printed on the serial monitor zero.

#include <Keypad.h>

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, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //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)
   {
     if(key == '*')
    {
      Serial.print(num);
      num = 0;
    }
    else{
      num = num * 10 + (key - '0');
    }
   }
  
    
}

already made the code as you mentioned, but instead when I do the 4x4 keypad only thing I get when pressing '*' I only printed on the serial monitor zero.

That code doesn't even compile.

AWOL excuse me much, really I ask an apology for forgetting to copy the integer variable num declaring

Logic_One:
AWOL excuse me much, really I ask an apology for forgetting to copy the integer variable num declaring

Please post your entire code. We will want to know where, exactly, you deeclare num.

You have the '#' and '*' reversed. If you press numbers, then '#', it should work.

One thing... when you are having problems, and you can't figure things out, plut some Serial.print() statements in, so you can be sure you're getting to where you think it should. Try changing your loop() to this...

void loop()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    Serial.println(key);
    if(key == '*') {
      Serial.println(key);
      Serial.print(num);
      num = 0;
      Serial.write(num);
    }
    else{
      Serial.print(key);
      Serial.print(" ");
      num = (num * 10) + (key - '0');
      Serial.println(num);
    }
  }
}

Did you remember to change the number in Serial.begin() to the number that is found in the lower right corner of your serial monitor?