Hello friends, I need your help in Keypad Menu design...
Objective: To design menu with submenus using 4x4 matrix keypad
Apparatus: Mega 2560, 4x4 Keypad and a LCD
#include <Password.h>
#include <Keypad.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(43,41,39);
Password password = Password( "1234" );
Password opt1 = Password( "1" );
Password opt2 = Password( "2" );
Password opt3 = Password( "3" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {37,35,33,31};
byte colPins[COLS] = {29,27,25,23};
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
keypad.addEventListener(auth_pwd); //add an event listener for this keypad
authenticate();
}
void authenticate(){
u8g.setFont(u8g_font_tpss);
u8g.setColorIndex(1);
u8g.firstPage ();
do{
u8g.setPrintPos(16, 25);
u8g.print("Please enter the");
u8g.setPrintPos(37, 46);
u8g.print("PASSWORD");
}while( u8g.nextPage () );
}
void loop(){
keypad.getKey();
}
//take care of some special events
void auth_pwd(KeypadEvent aKey){
switch (keypad.getState()){
case PRESSED:
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
switch (aKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(aKey);
}
}
}
void option1(KeypadEvent bKey){
switch (keypad.getState()){
case PRESSED:
u8g.setFont(u8g_font_helvR08);
u8g.setPrintPos(5, 20);
u8g.firstPage();
do {
u8g.print("You choosed option: ");
u8g.print(bKey);
} while( u8g.nextPage() );
switch (bKey){
case '*': choose_menu_option1(); break;
case '#': opt1.reset(); break;
default: opt1.append(bKey);
}
}
}
void option2(KeypadEvent cKey){
switch (keypad.getState()){
case PRESSED:
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setPrintPos(0, 20);
u8g.print("You choosed option: ");
u8g.print(cKey);
} while( u8g.nextPage() );
switch (cKey){
case '*': choose_menu_option2(); break;
case '#': opt2.reset(); break;
default: opt2.append(cKey);
}
}
}
void option3(KeypadEvent dKey){
switch (keypad.getState()){
case PRESSED:
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setPrintPos(0, 20);
u8g.print("You choosed option: ");
u8g.print(dKey);
} while( u8g.nextPage() );
switch (dKey){
case '*': choose_menu_option3(); break;
case '#': opt3.reset(); break;
default: opt3.append(dKey);
}
}
}
void checkPassword(){
if (password.evaluate()){
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
u8g.firstPage();
do {
u8g.setFont(u8g_font_fub17);
u8g.setColorIndex(1);
u8g.drawBox(0,0,128,64);
u8g.setColorIndex(0);
u8g.setPrintPos(16, 25);
u8g.print("Menu design");
} while( u8g.nextPage() );
delay(1000);
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setColorIndex(1);
u8g.setPrintPos(2, 5);
u8g.print("Choose the options below");
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(16, 15);
u8g.print(" 1. option1");
u8g.setPrintPos(16, 30);
u8g.print(" 2. option2");
u8g.setPrintPos(16, 45);
u8g.print(" 3. option3");
} while( u8g.nextPage() );
keypad.addEventListener(option1); //add an event listener for this keypad
keypad.addEventListener(option2); //add an event listener for this keypad
keypad.addEventListener(option3); //add an event listener for this keypad
}else{
u8g.firstPage();
do {
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 20);
u8g.print("Wrong option!");
} while( u8g.nextPage() );
//add code to run if it did not work
}
}
void choose_menu_option1(){
if (opt1.evaluate()){
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setColorIndex(1);
u8g.setPrintPos(2, 15);
u8g.print("This is option 1");
} while( u8g.nextPage() );
}
}
void choose_menu_option2(){
if (opt2.evaluate()){
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setColorIndex(1);
u8g.setPrintPos(2, 15);
u8g.print("This is option 2");
} while( u8g.nextPage() );
}
}
void choose_menu_option3(){
if (opt3.evaluate()){
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvR08);
u8g.setColorIndex(1);
u8g.setPrintPos(2, 15);
u8g.print("This is option 3");
} while( u8g.nextPage() );
}
}
In the sample code i have attached , the first password validation goes fine. It asks for a password, i entered '1234' and it went inside. After that When i entered 1,2,3 for options, nothing happens. Only the characters i entered appear. Not going inside the option! What am i doing wrong here???
I have also attached the setup and the output images for your reference.
Can somebody give me a menu design program using Keypad library with multiple choice selection???
Please help! My entire project is stuck in here!