Adding values to array using function arguments (Arduino library)

I'm currently creating an Arduino library and there is a function where I need to take the provided arguments and add them to an array (that already has elements). However, no matter what I try, it doesn't seem to work. The "menu_items_settings" should have the items already in the array plus the items defined as arguments. The "menu_items_settings_bool" should have as many booleans as the "menu_items_settings" has elements.

You can find the complete code in the Wokwi

The function is:

void OpenMenuOS::drawSettingMenu(const char* items...) {                            //TODO: Be able to add elements in the setting menu
  if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked_settings == 0)) {  // up button clicked - jump to previous menu item
    item_selected_settings = item_selected_settings - 1;                            // select previous item
    button_up_clicked_settings = 1;                                                 // set button to clicked to only perform the action once
    if (item_selected_settings < 0) {                                               // if first item was selected, jump to last item
      item_selected_settings = NUM_SETTINGS_ITEMS - 1;
    }
  }

  if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked_settings == 1)) {  // unclick
    button_up_clicked_settings = 0;
  }

  uint16_t rect_x = (tftWidth - rect_width) / 2;                                   // Center the rectangle horizontally with a 4-pixel space on the right
  uint16_t rect_y = (tftHeight - rect_height) / 2;                                 // Center the rectangle vertically
  canvas.drawFastVLine(155, 29, 22, ST7735_WHITE);                                 // Display the Shadow
  canvas.drawFastHLine(3, 51, 150, ST7735_WHITE);                                  // Display the Shadow
  canvas.drawRoundRect(rect_x, rect_y, rect_width, rect_height, 4, ST7735_WHITE);  // Display the rectangle

  canvas.fillRoundRect(rect_x + 1, rect_y - 25, rect_width - 3, rect_height - 3, 4, ST7735_BLACK);
  canvas.fillRoundRect(rect_x + 1, rect_y + 27, rect_width - 3, rect_height - 3, 4, ST7735_BLACK);
  canvas.fillRoundRect(rect_x + 1, rect_y + 1, rect_width - 3, rect_height - 3, 4, ST7735_BLACK);


  // draw next item as icon + label
  canvas.setFont(&FreeMono9pt7b);
  canvas.setTextSize(1);
  canvas.setTextColor(ST7735_WHITE);
  canvas.setCursor(10, 17);
  canvas.println(menu_items_settings[item_selected_settings_previous]);
  if ((item_selected_settings - 1) >= 0) {
    if (menu_items_settings_bool[item_selected_settings - 1] == false) {
      canvas.drawRoundRect(114, 3, 40, 20, 11, ST7735_RED);
      canvas.fillRoundRect(116, 5, 16, 16, 11, ST7735_RED);
    } else {
      canvas.drawRoundRect(114, 3, 40, 20, 11, ST7735_GREEN);
      canvas.fillRoundRect(136, 5, 16, 16, 11, ST7735_GREEN);
    }
  }

  // draw selected item as icon + label in bold font
  canvas.setFont(&FreeMonoBold9pt7b);
  canvas.setTextSize(1);
  canvas.setTextColor(ST7735_WHITE);
  canvas.setCursor(10, 44);
  canvas.println(menu_items_settings[item_selected_settings]);
  if (item_selected_settings >= 0 && item_selected_settings < NUM_SETTINGS_ITEMS) {
    if (menu_items_settings_bool[item_selected_settings] == false) {
      canvas.drawRoundRect(114, 30, 40, 20, 11, ST7735_RED);
      canvas.fillRoundRect(116, 32, 16, 16, 11, ST7735_RED);
    } else {
      canvas.drawRoundRect(114, 30, 40, 20, 11, ST7735_GREEN);
      canvas.fillRoundRect(136, 32, 16, 16, 11, ST7735_GREEN);
    }
  }

  // draw previous item as icon + label
  canvas.setFont(&FreeMono9pt7b);
  canvas.setTextSize(1);
  canvas.setTextColor(ST7735_WHITE);
  canvas.setCursor(10, 70);
  canvas.println(menu_items_settings[item_selected_settings_next]);

  if ((item_selected_settings + 1) < NUM_SETTINGS_ITEMS) {
    if (menu_items_settings_bool[item_selected_settings + 1] == false) {
      canvas.drawRoundRect(114, 56, 40, 20, 11, ST7735_RED);
      canvas.fillRoundRect(116, 58, 16, 16, 11, ST7735_RED);
    } else {
      canvas.drawRoundRect(114, 56, 40, 20, 11, ST7735_GREEN);
      canvas.fillRoundRect(136, 58, 16, 16, 11, ST7735_GREEN);
    }
  }
  if (digitalRead(BUTTON_SELECT_PIN) == HIGH) {
    if (previousButtonState == LOW && !buttonPressProcessed) {
      if (item_selected_settings == 0) {
        if (menu_items_settings_bool[0] == true) {
          menu_items_settings_bool[0] = false;
          if (WiFi.status() == WL_CONNECTED) {
            WiFi.disconnect();
          }
        } else {
          menu_items_settings_bool[0] = true;
          if (WiFi.status() != WL_CONNECTED) {
            connectToWiFi();
          }
        }
      } else if (item_selected_settings == 1) {
        if (menu_items_settings_bool[1] == true) {
          menu_items_settings_bool[1] = false;
        } else {
          menu_items_settings_bool[1] = true;
        }
      } else if (item_selected_settings == 2) {
        if (menu_items_settings_bool[2] == true) {
          menu_items_settings_bool[2] = false;
        } else {
          menu_items_settings_bool[2] = true;
        }
      } else if (item_selected_settings == 3) {
        if (menu_items_settings_bool[3] == true) {
          menu_items_settings_bool[3] = false;
          digitalWrite(tft_bl, HIGH);
        } else {
          menu_items_settings_bool[3] = true;
          digitalWrite(tft_bl, LOW);
        }
      } else if (item_selected_settings == 4) {
        if (menu_items_settings_bool[4] == true) {
          menu_items_settings_bool[4] = false;
        } else {
          menu_items_settings_bool[4] = true;
          ArduinoOTA.begin();
          ArduinoOTA.handle();
        }
      }
      buttonPressProcessed = true;
      saveToEEPROM();
    }
    previousButtonState = HIGH;
  } else {
    previousButtonState = LOW;
    buttonPressProcessed = false;  // Reinitialiser la variable de controle lorsque le bouton est relache
  }
}

And the arrays in which elements needs to be added are :

char menu_items_settings[][MAX_ITEM_LENGTH] = {
  // array with item names
  { "Wifi" },
  { "Open wifi" },
  { "Bluetooth" },
  { "Backlight" },
  { "OTA" },
};
const int NUM_SETTINGS_ITEMS = sizeof(menu_items_settings) / sizeof(menu_items_settings[0]);

bool OpenMenuOS::menu_items_settings_bool[NUM_SETTINGS_ITEMS] = {
  true,   // Open wifi
  false,  // Wifi
  true,   // Bluetooth
  false,  // Backlight
  false,  // OTA Updates
};

Does anyone know how to do this? Also, if you notice any other issues, let me know!

Ok, thanks for letting me know. But do you know how I could do it so that it puts values in the array (the ones always there) and after that it adds the values from the arguments? I'm only 14 y/o so I still have a lot to learn.

Ok, finally, what you said earlier : "In C++ you can't add to an array. The array needs to be created with as many slots as you'll need.." Really helped me in undertanding the problem and I think I'm going to be able to make this work.

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