Hello people how are you? I hope you are well and healthy, I would like to share something with you to listen to your opinions; First of all I would like to clarify that I am a novice in the use of Arduino and its programming, in fact as many of me am learning to program simple codes, and for the most complex things I take some example from the internet and modify it to my interest.. Well, that's the reason for this post. I'd like to know if they'd help me learn how to modify this code,
to be able to use it better in my project;this code has this array to create menu and by using switch each "menu" has its case... What I would look for is to know if it is possible to add e.g. 2 more options, Because when I tried to add another menu ["X"] the program did not work anymore, my knowledge does not allow me to know why, in this case we have 12, is it possible to have, for example, 14 or 15?
I hope you can help me, thank you!
const int op_cnt = 13;
int current_id;
bool operation;
bool onoff;
struct menu_item {
int id; //id of menu item (might be same as number in array
int parent; //id of upper level menu item
int sub; //id of sub menu item, "0" means operation
int next; //id of next menu item at same level
int npp; //not used
String text; //menu item text
int operation; //operation code
int ang1;
} ;
menu_item menus[op_cnt];
// initialize the library with the numbers of the interface pins
String lcd_line1;
String lcd_line2;
int show_data;
unsigned long times;
int button_flag;
};
void setup() {
times = 0;
current_id = 0;
operation = false;
menus[0].id = 0;
menus[0].parent = 0;
menus[0].npp = 0;
menus[0].next = 0;
menus[0].sub = 1;
menus[0].text = "hi";
menus[0].operation = 0;
menus[1].id = 1; menus[1].parent = 0; menus[1].npp = 1; menus[1].next = 2; menus[1].sub = 3; menus[1].text = " Manual"; menus[1].operation = 0;
menus[2].id = 2; menus[2].parent = 0; menus[2].npp = 2; menus[2].next = 1; menus[2].sub = 11; menus[2].text = " AUTOMATIC"; menus[2].operation = 0;
menus[3].id = 3; menus[3].parent = 1; menus[3].npp = 1; menus[3].next = 4; menus[3].sub = 0; menus[3].text = " opt1"; menus[3].operation = 110;
menus[4].id = 4; menus[4].parent = 1; menus[4].npp = 2; menus[4].next = 3; menus[4].sub = 0; menus[4].text = " option2"; menus[4].operation = 120;
menus[11].id = 11; menus[11].parent = 2; menus[11].npp = 1; menus[11].next = 12; menus[11].sub = 0; menus[11].text = " 70% 30% "; menus[11].operation = 210;
menus[12].id = 12; menus[12].parent = 2; menus[12].npp = 2; menus[12].next = 11; menus[12].sub = 0; menus[12].text = " 85% 15% "; menus[12].operation = 220;
button_flag = 0;
lcd.begin(16, 2);
lcd.createChar(8, arrow);
}
void loop() {
if (val > 1000 && button_flag == 0) {
button_up();
button_flag = 1;
};
if (val > 280 && val < 360 && button_flag == 0) {
button_down();
button_flag = 1;
};
if (val > 200 && val < 260 && button_flag == 0) {
button_back();
button_flag = 1;
};
if (val > 110 && val < 180 && button_flag == 0) {
button_ok();
button_flag = 1;
};
if (val < 100 && button_flag == 1) { //reset button flag
button_flag = 0;
}
}
void button_up() {
if (operation == false) {
//get previous id and set menu
for (int i = 0; i < op_cnt; i++) {
if (menus[i].next == current_id) {
current_id = i;
break;
};
};
draw_menu(current_id);
};
}
void button_down() {
//get next id and set menu
if (operation == false) {
//get previous id and set menu
current_id = menus[current_id].next;
draw_menu(current_id);
};
}
void button_back() {
//get parent and set menu
if (menus[current_id].parent == current_id) {
//turn off screen
onoff = false;
}
else {
if (operation == true) {
draw_menu(current_id);
operation = false;
} else {
if (operation == false) {
//get previous id and set menu
current_id = menus[current_id].parent;
draw_menu(current_id);
};
};
};
}
void button_ok() {
if (onoff == false) {
onoff = true;
lcd.display();
} else {
//get sub or operation and set menu
if (operation == false && menus[current_id].sub != 0) {
current_id = menus[current_id].sub;
draw_menu(current_id);
} else {
if (operation == false && menus[current_id].sub == 0) {
//handle operations
show_function(menus[current_id].operation);
operation = true;
} else {
if (operation == true) {
trigger_operation(menus[current_id].operation);
};
};
};
};
}
void draw_menu(int id) {
lcd.clear();
lcd_line1 = menus[id].text;
if (menus[id].next != id) {
lcd_line2 = menus[menus[id].next].text;
} else lcd_line2 = "";
lcd.setCursor(0, 0);
lcd.print(lcd_line1);
lcd.write(8);
lcd.setCursor(0, 1);
lcd.print(lcd_line2);
lcd.setCursor(0, 0);
}
void show_function(int function) {
lcd.clear();
lcd.setCursor(0, 0);
switch (function) {
case 110:
//do something
break;
case 120:
//do something
break;
case 210:
//do something