4x4 Keypad

hi i have 4x4 Keypad that is connected to digital pin 0 to 7 of an arduino uno and LCD 20x4 that is connected to digital pin 8-13.

i want to declare the ( * ) button to accept an order and ( # ) button to cancel the order

i have the code


#include <Wire.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

const byte rows = 4;
const byte cols = 4;

char keys[rows][cols] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[rows] = {0,1,2,3};
byte colPins[cols] = {4,5,6,7};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

LiquidCrystal lcd(8,9,10,11,12,13);

void setup()
{
lcd.begin(20,4);
lcd.setCursor(3,0);
lcd.print(“Enter liquor “);
lcd.setCursor(3,1);
lcd.print(“choice :”);
lcd.setCursor(0,3);
lcd.print(”(*)ACCEPT”);
lcd.setCursor(11,3);
lcd.print("(#)CANCEL");
}

void loop()
{
char key = keypad.getKey();
if(key)
{
switch (key)
{
case ‘1’:
lcd.setCursor(0,2);
lcd.print(" “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 1”);
break;
case ‘2’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 2”);
break;
case ‘3’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 3”);
break;
case ‘4’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 4”);
break;
case ‘5’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 5”);
break;
case ‘6’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 6”);
break;
case ‘7’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 7”);
break;
case ‘8’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 8”);
break;
case ‘9’:
lcd.setCursor(0,2);
lcd.print(” “);
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 9”);
break;
case ‘0’:
lcd.setCursor(0,2);
lcd.print(” ");
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 10”);
break;
}
}
}


the LCD PRINT like this: if the user choose (1)
Enter liquor
choice: 1
Liquor Mix 1
(*)ACCEPT (#)CANCEL

then if the user wants that mix he/she will press the ( * ) to accept the order an
then the lcd will print ex. “Mixing” then 2 or 3 DC Motor will pump depends on
our selection. ( # ) to cancel/clear the text in lcd.setCursor(12,1) and lcd.setCursor(5,2).

i want to declare the ( * ) button to accept an order and ( # ) button to cancel the order

What is stopping you ? What have you tried ?

You can obviously read the keypad, and the code posted does things based on which key is pressed so you are well on the way to your goal.

i tried to create another function

void option() {
char key1 = keypad.getKey();
if(key1)
{
switch (key1)
{
case ‘*’:
lcd.clear();
lcd.print(“Mixing”);
break;
case ‘#’:
lcd.setCursor(12,1);
lcd.print(" “);
lcd.setCursor(5,2);
lcd.print(” ");
break;
}
}
}

then i put option() here

case ‘1’:
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print(key);
lcd.setCursor(5,2);
lcd.print(“Liquor Mix 1”);
option();
break;

but nothing happen.

The keypad.getKey function is non blocking, ie it does not wait for a key to be pressed before returning so in your option() function if no key is pressed when the function starts it will immediately exit. As an experiment try the waitForKey() function instead in the option() function but be aware that it will stop the program while it waits for a key.

At some point you may want to wait for a key and exit if one is not pressed within a period, in which case waitForKey() would not be appropriate.

the LCD PRINT like this: if the user choose (1)
Enter liquor
choice: 1
Liquor Mix 1
(*)ACCEPT (#)CANCEL

then if the user wants that mix he/she will press the ( * ) to accept the order an
then the lcd will print ex. "Mixing" then 2 or 3 DC Motor will pump depends on
our selection. ( # ) to cancel/clear the text in lcd.setCursor(12,1) and lcd.setCursor(5,2).

I'm not sure what use the CANCEL function will be. Just pressing a different key changes the order. Nothing happens until the ACCEPT key is pressed, so I don't see what the CANCEL key will do.

I also don't see why ACCEPT warrants special treatment. Something happens when the 1 is pressed. Something happens when the 2 is pressed. Something (is to) happen(s) when the * key is pressed. Why wouldn't that just be another case?

Much of what you do in each case is common, and doesn't belong in the case.