Can someone help me how can I make a sub menu?
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{ '1','2','3', 'A' },
{ '4','5','6', 'B' },
{ '7','8','9', 'C' },
{ '*','0','#', 'D' }
};
byte rowPins[ROWS] = { 53, 51, 49, 47 }; //connect to the row pinouts of the keypad {1, 2, 3, 4}
byte colPins[COLS] = { 52, 50, 48, 46 }; //connect to the column pinouts of the keypad {5, 6, 7, 8}
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//Keys definition
/////////////////////////////////////////////////////////////////////////////////////////////////////
const int upKey = '2';
const int downKey = '8';
const int selectKey = '5';
const int leftKey = '4';
const int rightKey = '6';
/////////////////////////////////////////////////////////////////////////////////////////////////////
//Menu definition
/////////////////////////////////////////////////////////////////////////////////////////////////////
int currentMenuItem = 0;
int lastState = 0;
const int maxMenu = 3;
const int minMenu = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
}
void loop()
{
mainMenu();
}
void mainMenu() {
int state = 0;
int x = char(keys);
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == upKey) {
state = 1;
}
else if (key == downKey) {
state = 2;
}
else if (key == selectKey) {
state = 3;
}
else if (key == leftKey) {
}
else if (key == rightKey) {
}
}
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem ++;
if (currentMenuItem > maxMenu) currentMenuItem = minMenu;
displayMenu(currentMenuItem);
//Serial.println(currentMenuItem);
}
else if (state == 2) {
//If Down
currentMenuItem--;
if(currentMenuItem < minMenu) currentMenuItem = maxMenu;
displayMenu(currentMenuItem);
//Serial.println(currentMenuItem);
}
else if (state == 3 && currentMenuItem == 2) {
//If Selected
selectMenuControl();
//Serial.println(currentMenuItem);
}
else if (state == 3 && currentMenuItem == 3) {
//If Selected
selectMenuSettings();
//Serial.println(currentMenuItem);
}
lastState = state;
}
delay(5);
}
void displayMenu(int x) {
switch (x) {
case 1:
Serial.println("-> Basic");
break;
case 2:
Serial.println("-> Control");
break;
case 3:
Serial.println("-> Settings");
break;
}
}
void selectMenuControl() {
Serial.println("Automatically Manually Back");
}
void selectMenuSettings() {
Serial.println("Settings options");
}