Ho scritto la base e scopiazzato qualcosa per creare un menu e sottomenu con <Keypad.h> e <LiquidCrystal.h> per intenderci quello a 2 righe.
La parte display non è inclusa. Uso la seriale per vedere i risultati.
Il listato lo ritenete corretto?
È semplificative in qualche modo secondo voi?
È solo una bozza, ma mi piacerebbe renderlo più semplificato, anche se questo è di facile lettura.
Soprattutto vorrei che non sia avido di memoria.
Il menu è la pressione del tasto 0 zero
I sottomenu vanno da 1 a 9
La parte che rimanda al sottomenu deve rimanere nel ciclo fino a che premo il tasto 0 zero.
Abbiate pietà sono alle primissime armi con il C++ e purtroppo non ho molto tempo per approfondire il linguaggio.
Per età e acciacchi vari.
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char Keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
/* Tabella conversione Hex a Dec per i tasti premuti
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
A 65
B 66
C 67
D 68
# 35
* 42
*/
int MenuOption = 48;
byte rowPins[ROWS] = {36, 38, 40, 42}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {44, 46, 48, 50}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
}
void loop()
{
ImpostazioniMenu();
}
void ImpostazioniMenu()
{
char customKey = customKeypad.getKey();
int numero = customKey;
if (customKey)
{
Serial.print(customKey);
Serial.print (" ");
Serial.println(numero);
if ((customKey) == 49) // tasto 1
{
Menu1();
}
if ((customKey) == 50)
{
Menu2();
}
if ((customKey) == 51)
{
Menu3();
}
if ((customKey) == 52)
{
Menu4();
}
if ((customKey) == 53)
{
Menu5();
}
if ((customKey) == 54)
{
Menu6();
}
if ((customKey) == 55)
{
Menu7();
}
if ((customKey) == 56)
{
Menu8();
}
if ((customKey) == 57)
{
Menu9();
}
if ((customKey) == 48) // tasto 0
{
return;
}
}
}
void Menu1()
{
Serial.println("Menu 1");
for (int i = 0;i <= 99;i++){
Serial.println(i);//il blocco di istruzioni verrà eseguito 100 volte
char customKey = customKeypad.getKey();
int numero = customKey;
if (customKey)
{
Serial.print(customKey);
Serial.print (" ");
Serial.println(numero);
}
if ((customKey) == 48)
{
i = 99;
Serial.println ("Esco dal ciclo e vado a Menu 0");
Serial.println(i);
return;
}
delay (250);}
}
void Menu2()
{
Serial.println("Menu 2");
}
void Menu3()
{
Serial.println("Menu 3");
}
void Menu4()
{
Serial.println("Menu 4");
}
void Menu5()
{
Serial.println("Menu 5");
}
void Menu6()
{
Serial.println("Menu 6");
}
void Menu7()
{
Serial.println("Menu 7");
}
void Menu8()
{
Serial.println("Menu 8");
}
void Menu9()
{
Serial.println("Menu 9");
}
