Change some lines of the code

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



I think you need to change

op_cnt = 13 

to

op_cnt = 15 // or whatever number you want

If you want more menus.
Because in line 18 it says

menu_item menus[op_cnt];

Hi.
Something is missing at the end of the sketch.

RV mineirin

yeah I noticed. He didn't post the full code.

right, don't put the whole code (I apologize if necessary)I believed that with the appointment in this specific part you could come to understand correctly. Thank you for your answers, in fact, by modifying the value as they said, you already allow me to add another menu, now I'm trying to leave it functional. I'll tell you how it all turned out later, thank you very much!!!

You are welcome! Do tell us how it works out. And can you PM me? I am interested in your code because I was working on a menu and I like the way that one is structured.
I would like to use it. Yes I would it change a bunch but I wanted to know if this is some project where you for some reason don't want to give everyone your code. Some people are like that which is why I am asking you.

no problem, in a few moments I send you mp, then I update this post, thank you!

Ok thanks!

if it's possible,send me you a mp because I don't find the way to

Ok!

It might be easier to read the initialization if it is done at compile time and not as long line that go off the side of the window:

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;

} menus[op_cnt] =
{
  {0, 0, 0, 0, 1, "hi", 0, 0},
  {1, 0, 1, 2, 3, " Manual", 0, 0},
  {2, 0, 2, 1, 11, " AUTOMATIC", 0, 0},
  {3, 1, 1, 4, 0, "  opt1", 110, 0},
  {4, 1, 2, 3, 0, "  option2", 120, 0},
  {6, 0, 0, 0, 0, "", 0, 0}, // UNUSED
  {7, 0, 0, 0, 0, "", 0, 0}, // UNUSED
  {8, 0, 0, 0, 0, "", 0, 0}, // UNUSED
  {9, 0, 0, 0, 0, "", 0, 0}, // UNUSED
  {10, 0, 0, 0, 0, "", 0, 0}, // UNUSED
  {11, 2, 1, 12, 0, " 70% 30% ", 210, 0},
  {12, 2, 2, 11, 0, " 85% 15% ", 220, 0},
};

Hey, that looks much better, I'm going to investigate a little more that way of writing the code, thanks!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.