Help me to program keypad

Hello guys i have a project from my college, but im stuck at the program.
I dont understand how to input ex:1000 from keypad. this is my code.

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

const byte numRows= 4; //baris pada keypad
const byte numCols= 4; //Kolom pada keypad

/keymap mendefinisikan tombol ditekan sesuai
dengan baris dan kolom seperti muncul pada keypad
/
char keymap[numRows][numCols]=
{
{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{’*’, ‘0’, ‘#’, ‘D’}
};

byte rowPins[numRows]= {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
char input[10];
int i=0;
int result;
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

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

lcd.begin(16, 2);
lcd.setCursor(1, 0);
lcd.print(“Keypad dan LCD”);

lcd.setCursor(2, 1);
lcd.print(“COBA KEYPAD”);
delay(5000);
lcd.clear();
}

void loop()
{
lcd.setCursor(1, 0);
lcd.print(“Enter your number”);

char customKey = myKeypad.getKey();// inisialisasi keyped sebagai tampilan character
lcd.setCursor(0,1);
input*=customKey;*

if (customKey){

  • i++;*

  • lcd.print(customKey);*

}
}[/quote]
It still just showed 1 besides i have an increment code in if. please help me.

Don’t you want to test to see if you’ve had a key pressed?
Does the keypad have a begin method?

Please remember to use code tags when posting code.

If you want to input a 4 digit number, then you should follow this code I have given here.

char thenumber[] = {0, 0, 0, 0};
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


const byte numRows= 4; //baris pada keypad
const byte numCols= 4; //Kolom pada keypad

/*keymap mendefinisikan tombol ditekan sesuai
dengan baris dan kolom seperti muncul pada keypad*/
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};


byte rowPins[numRows]= {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
char input[10];
int i=0;
int result;
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

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

  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print("Keypad dan LCD");
 
  lcd.setCursor(2, 1);
  lcd.print("COBA KEYPAD");
  delay(5000);
  lcd.clear();
}


void loop()
{
  lcd.setCursor(1, 0);
  lcd.print("Enter your number");
  thenumber[0] = keypad.waitForKey();
 thenumber[1] = keypad.waitForKey();
 thenumber[2] = keypad.waitForKey();
 thenumber[3] = keypad.waitForKey();
lcd.print(thenumber);
}

…Arnav

@ArnavPawarAA - getKey (note spelling) is non-blocking.

See the problem?

Please stop time-wasting; it’s getting tedious and embarrassing.

I dont understand how to input ex:1000 from keypad.

Take a look at Keypad data entry

Please, carry out the following steps/tasks; as a result, the 4-digits (0 - 9) that you will be entering from the Keypad will appear on the Serial Monitor.
1. Check that you have connected the Keypad as per following diagram (Fig-1).
keyPadPic.png
Figure-1:

(2) Upload the following sketch:

#include<Keypad.h>     //needed to use ready-made routines
char myDigits[5] = "";//array to hold the ASCII codes of the digits to be entered from Keypad
int i = 0;
//-------------------------------------------------------------------------
const byte numRows = 4; //number of rows in the Keypad
const byte numCols = 4; //number of columns in the Keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[numRows] = {9, 8, 7, 6}; //R1R2R3R4
byte colPins[numRows] = {5, 4, 3, 2}; //C1C2C3C4

Keypad myKeypad =
  Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
//----------------------------------------------------------------------------------------------------

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

void loop()
{
  char keyCode = myKeypad.getKey(); //getting ASCII code of a pressed down key
  if (keyCode >= '0' && keyCode <= '9') //pressed down key found; deal with only 0 -9
  {
    Serial.print(keyCode);   //show the image of the pressed down key on Serial Monitor
    myDigits[i] = keyCode;   //save the ASCII code of the presed down key in an array
    i++;
    if (i == 4)     //four keys have been pressed
    {
      //take action as needed
      Serial.println();
      Serial.println(myDigits);     //show the content fo myDigots[] on Serial Monitor
      i = 0; //reset array index
      memset(myDigits, 0, 5);     //reset array myDigits[];
    }
  }
}

3. Press 1 0 0 0 from Keypad and check that they appear on the Serial Monitor as they are entered. At the end of 4-digit entry, 1000 again appears on the Serial Monitor on the next line (Fig-2).

smkey.png
Figure-2:

Note: Now connect I2C based LCD with the UNO; initialize the LCD; declare all variables as needed and use lcd.print() command to show the digits of the Keypad on the LCD.

keyPadPic.png

smkey.png

UKHeliBob:
Take a look at Keypad data entry

In the referred post, the mappings/connections of the rows and columns of the Keypad are different from those of OP’s sketch. Hope that OP will be able to notice the difference.

GolamMostafa:
In the referred post, the mappings/connections of the rows and columns of the Keypad are different from those of OP’s sketch.

This isn’t a homework-completion service.

TheMemberFormerlyKnownAsAWOL:
This isn't a homework-completion service.

However, they (the learners) deserve to receive some positive guidance.

They won’t learn anything if you put the code on a plate, with garnish.

They’ll learn even less from posts like that from @ArnavPawarAA.

Many of them come with total ignorance both in Arduino hardware and programming. In that case, should we help them with some kind of examples that contain check list -- do this and you get this and then you do that to get that?

Or we could encourage them to engage in dialogue.

This would be a great job; but, it requires skills.

And willingness

And patience as well.

TheMemberFormerlyKnownAsAWOL:
@ArnavPawarAA - getKey (note spelling) is non-blocking.

See the problem?

Please stop time-wasting; it's getting tedious and embarrassing.

Take a look at @UKHeliBob's thread. He is using the same structure, but using a slightly different way to save the numbers.

Stop complaining about others unless your knowledge is clear and you have any other idea about the problem. This can get your comment reported to by the people on the forum.

Wait until the OP says anything, and don't ruin the OP's imagination and perspective towards Arduino. He is a beginner. This type of approach isn't proper towards such a person.

..Arnav

Do you understand what “non-blocking” means?
(You may wish to take a closer look at UKHeliBob’s code - you may learn something)

Just please stop the knee-jerk “I’ve got this” posts

Also, go easy on the reporting threats - vexatious reporting is a good way of getting a forum timeout (someone I know well used to be a moderator here)

The function getKey() works like this-
It waits until any key is pressed, and then it ends.

It does no such thing. It reads the keypad and if a key is pressed it immediately returns its value otherwise it immediately returns -1

I think that you are confusing it with the waitForKey() function which does wait for a key to be pressed but in doing so blocks the program from doing anything else

UKHeliBob:
It does no such thing. It reads the keypad and if a key is pressed it immediately returns its value otherwise it immediately returns -1

I think that you are confusing it with the waitForKey() function which does wait for a key to be pressed but in doing so blocks the program from doing anything else

Yes, I apologise quickly now, I always used a slightly tweaked Keypad library. It had the waitforkey function merged with the read key.

..Arnav

It had the waitforkey function merged with the read key.

Why ?

Having both functions allows you to decide which to use. Blocking code is not always a bad thing but you have to choose when it is appropriate to use it