Transforming code into a library

So, i made a menu program and now i want to turn it into a library so i can use it in other sketches, but i tried and tried and just kept getting errors.
here is my original code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//button arrows are wired: 9, left, 8, down, 7, right, 10, up, and select to 6. And also that is the order in which they are assigned on the bool buttonPressed[]
int buttonArrows[] = {9, 8, 7, 10};
int selectButton = 6;

bool buttonPressed[5];

bool inMenu = true;

void Options();
void Restart();

struct menuItemStruct
{
  String name; //name of the item
  void (*action)(); //custom action
};

struct menuStruct
{
  String name; //name of the menu
  int currentItemIndex; //current selected item
  menuItemStruct* items; // pointer to menu array
  int numberOfItems; // length of the menu
};

//menus
menuItemStruct mainMenuItems[] = 
{
  {"Options", Options},
  {"Reset", Restart}
};
menuStruct mainMenu = 
{
  "Main menu",
  0,
  mainMenuItems,
  sizeof(mainMenuItems) / sizeof(mainMenuItems[0])
};

//current menu pointer
menuStruct* currentMenu = &mainMenu;  // start in mainMenu

void setup() 
{
  lcd.begin(16, 2);
  lcd.clear();

  //set pinmodes
  for (int i = 0; i < 4; i++)
  {
    pinMode(buttonArrows[i], INPUT_PULLUP);
  }
  pinMode(selectButton, INPUT_PULLUP);

  DisplayMenu(currentMenu);
}

void loop() 
{
  if (inMenu == true)
  {
    //button logic
    if (digitalRead(buttonArrows[0]) == LOW && buttonPressed[0] == false)
    {
      //runs only once
      buttonPressed[0] = true;
    }
    else if (digitalRead(buttonArrows[0]) == HIGH)
      buttonPressed[0] = false;

    if (digitalRead(buttonArrows[1]) == LOW && buttonPressed[1] == false)
    {
      //runs only once
      buttonPressed[1] = true;
      (*currentMenu).currentItemIndex++;
      DisplayMenu(currentMenu);
    }
    else if (digitalRead(buttonArrows[1]) == HIGH)
      buttonPressed[1] = false;

    if (digitalRead(buttonArrows[2]) == LOW && buttonPressed[2] == false)
    {
      //runs only once
      buttonPressed[2] = true;
    }
    else if (digitalRead(buttonArrows[2]) == HIGH)
      buttonPressed[2] = false;

    if (digitalRead(buttonArrows[3]) == LOW && buttonPressed[3] == false)
    {
      //runs only once
      buttonPressed[3] = true;
    }
    else if (digitalRead(buttonArrows[3]) == HIGH)
      buttonPressed[3] = false;

    if (digitalRead(selectButton) == LOW && buttonPressed[4] == false)
    {
      //runs only once
      buttonPressed[4] = true;
      (*currentMenu).items[(*currentMenu).currentItemIndex].action();
      inMenu = false;
    }
    else if (digitalRead(selectButton) == HIGH)
      buttonPressed[4] = false;

    //stop menus overflowing
    if ((*currentMenu).currentItemIndex >= (*currentMenu).numberOfItems)
    {
      (*currentMenu).currentItemIndex = 0;
      DisplayMenu(currentMenu);
    }
  }
}

void PrintToScreen(String line1, String line2)
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(line1);
  lcd.setCursor(0,1);
  lcd.print(line2);
}

void DisplayMenu(menuStruct* menuPointer)
{
  if ((*menuPointer).currentItemIndex > (*menuPointer).numberOfItems)
  {
    PrintToScreen((*menuPointer).items[(*menuPointer).currentItemIndex].name, "");
  }
  else
    PrintToScreen((*menuPointer).items[(*menuPointer).currentItemIndex].name, (*menuPointer).items[(*menuPointer).currentItemIndex + 1].name);
}

void Options()
{
  PrintToScreen("under work", "press reset");
}

void Restart()
{
  PrintToScreen("under work", "press reset");
}

Welcome to the forum

It would be easier to help if you posted your attempt to turn this into a library

Please post the .h, .cpp and .ino test sketch with details of the errors that you get when compiling, using code tags when you post the error messdages

this is my .h file (one of them, my latest atempt)

#ifndef arduino_computer_h
#define arduino_computer_h

#include <LiquidCrystal.h>
#include <Arduino.h>

// Forward declaration so structs can reference the class
class arduino_computer;

struct menuItemStruct
{
    String name; //name of the item
    void (*action)(arduino_computer*); //custom action
};

struct menuStruct
{
    String name; //name of the menu
    int currentItemIndex; //current selected item
    menuItemStruct* items; // pointer to menu array
    int numberOfItems; // length of the menu
};

class arduino_computer
{
    public:
        arduino_computer(LiquidCrystal& lcd, int* buttonArrows, int selectButton);

        void setupOS();
        void loopOS();

        static void Options(arduino_computer* comp);
        static void Restart(arduino_computer* comp);

        void PrintToScreen(const char* line1, const char* line2);
        void DisplayMenu(menuStruct* menu);

        void setMenu(menuStruct* menu);

    private:
        LiquidCrystal& _lcd;

        int* _buttonArrows;
        int _selectButton;

        bool _buttonPressed[5];
        bool _inMenu;

        menuStruct* _currentMenu;
};

#endif

and this is my cpp file (latest atempt)


#include <LiquidCrystal.h>
#include <Arduino.h>
#include <arduino_computer.h>

//constructor, i think
arduino_computer::arduino_computer(LiquidCrystal& lcdRef, int* buttonArrows, int numberOfItems) {
    _lcd = &lcdRef;
    _buttonArrows = buttonArrows;
    _numberOfItems = numberOfItems;

    // initialize buttonPressed array
    for (int i = 0; i < numberOfItems; i++) {
        buttonPressed[i] = false;
    }
}

bool buttonPressed[5] = { false, false, false, false, false };;

bool inMenu = true;

void arduino_computer::Options(arduino_computer* comp);
void arduino_computer::Restart(arduino_computer* comp);

//menus
menuItemStruct mainMenuItems[] =
{
  {"Options", arduino_computer::Options},
  {"Reset", arduino_computer::Restart}
};
menuStruct mainMenu =
{
  "Main menu",
  0,
  mainMenuItems,
  sizeof(mainMenuItems) / sizeof(mainMenuItems[0])
};

//current menu pointer
menuStruct* currentMenu = &mainMenu;  // start in mainMenu

void arduino_computer::PrintToScreen(const char* line1, const char* line2)
{
    _lcd.clear();
    _lcd.setCursor(0, 0);
    _lcd.print(line1);
    _lcd.setCursor(0, 1);
    _lcd.print(line2);
}

void arduino_computer::DisplayMenu(menuStruct* menuPointer)
{
    if ((*menuPointer).currentItemIndex > (*menuPointer).numberOfItems)
    {
        PrintToScreen((*menuPointer).items[(*menuPointer).currentItemIndex].name.c_str(), "");
    }
    else
        PrintToScreen((*menuPointer).items[(*menuPointer).currentItemIndex].name.c_str(), (*menuPointer).items[(*menuPointer).currentItemIndex + 1].name.c_str());
}

void arduino_computer::Options(arduino_computer* comp) 
{
    comp->PrintToScreen("under work", "press reset");
}

void arduino_computer::Restart(arduino_computer* comp) 
{
    comp->PrintToScreen("under work", "press reset");
}

void arduino_computer::arduino_computer::setupOS()
{
    _lcd.begin(16, 2);
    _lcd.clear();

    //set pinmodes
    for (int i = 0; i < 4; i++)
    {
        pinMode(_buttonArrows[i], INPUT_PULLUP);
    }
    pinMode(_selectButton, INPUT_PULLUP);

    DisplayMenu(currentMenu);
}

void arduino_computer::arduino_computer::loopOS()
{
    if (inMenu == true)
    {
        //button logic
        if (digitalRead(_buttonArrows[0]) == LOW && buttonPressed[0] == false)
        {
            //runs only once
            buttonPressed[0] = true;
        }
        else if (digitalRead(_buttonArrows[0]) == HIGH)
            buttonPressed[0] = false;

        if (digitalRead(_buttonArrows[1]) == LOW && buttonPressed[1] == false)
        {
            //runs only once
            buttonPressed[1] = true;
            (*currentMenu).currentItemIndex++;
            DisplayMenu(currentMenu);
        }
        else if (digitalRead(_buttonArrows[1]) == HIGH)
            buttonPressed[1] = false;

        if (digitalRead(_buttonArrows[2]) == LOW && buttonPressed[2] == false)
        {
            //runs only once
            buttonPressed[2] = true;
        }
        else if (digitalRead(_buttonArrows[2]) == HIGH)
            buttonPressed[2] = false;

        if (digitalRead(_buttonArrows[3]) == LOW && buttonPressed[3] == false)
        {
            //runs only once
            buttonPressed[3] = true;
        }
        else if (digitalRead(_buttonArrows[3]) == HIGH)
            buttonPressed[3] = false;

        if (digitalRead(_selectButton) == LOW && buttonPressed[4] == false)
        {
            //runs only once
            buttonPressed[4] = true;
            (*currentMenu).items[(*currentMenu).currentItemIndex].action(this);
            inMenu = false;
        }
        else if (digitalRead(_selectButton) == HIGH)
            buttonPressed[4] = false;

        //stop menus overflowing
        if ((*currentMenu).currentItemIndex >= (*currentMenu).numberOfItems)
        {
            (*currentMenu).currentItemIndex = 0;
            DisplayMenu(currentMenu);
        }
    }
}

I don't see the error log and current ino test case.

well basically i need advice to make a new one because the last one got more errors after fixing one error and then i just quit that one

A new what ?

Please post a sketch that uses the library whether or not it causes errors

Compiling with an empty sketch, just the two library files.

The first error that I get

C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp: In constructor 'arduino_computer::arduino_computer(LiquidCrystal&, int*, int)':
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp:6:1: warning: uninitialized reference member in 'class LiquidCrystal&' [-fpermissive]
 arduino_computer::arduino_computer(LiquidCrystal& lcdRef, int* buttonArrows, int numberOfItems)
 ^~~~~~~~~~~~~~~~
In file included from C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp:3:0:
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.h:41:24: note: 'LiquidCrystal& arduino_computer::_lcd' should be initialized
         LiquidCrystal& _lcd;
                        ^~~~
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp:8:11: error: no match for 'operator=' (operand types are 'LiquidCrystal' and 'LiquidCrystal*')
   _lcd = &lcdRef;
           ^~~~~~

I'm not an expert C++ programmer but as far as I know you need an initialiser list with the arduino_computer constructor. In the cpp file

//constructor, i think
arduino_computer::arduino_computer(LiquidCrystal& lcdRef, int* buttonArrows, int numberOfItems) : _lcd(lcdRef)
{
  //_lcd = &lcdRef;
  _buttonArrows = buttonArrows;
  _numberOfItems = numberOfItems;

Next error

C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp: In constructor 'arduino_computer::arduino_computer(LiquidCrystal&, int*, int)':
C:\Users\bugge\OneDrive\Documents\Arduino\1_forum.arduino.cc\1423345_library\1423345_post03\arduino_computer.cpp:10:3: error: '_numberOfItems' was not declared in this scope
   _numberOfItems = numberOfItems;
   ^~~~~~~~~~~~~~

Your arduino_computer class does not have a variable called _numberOfItems. Your menuStruct does have one.

There is more that the compiler does not like.

ok, but how can i turn the original into a library

Start by correcting the errors that @sterretje flagged and getting a clean compilation.

The other approach is to build the class incrementally. Start with a very basic .ino file which defines the lcd, pins etc. then calls the class constructor, then calls the class setup and loop functions. Initially, strip out everything from the class loop so it is empty also strip out all the other functions from the .cpp file . Try to get a clean compilation, commenting out code which gives errors that you can't solve. Once you get to that stage then add some code to the class setup function just to print out say the pin numbers and add just enough code in the class loop to print "Hello World" to the lcd. Work from there.

Just simply handing the task to an AI chatbot when you don't fully understand what it is doing, especially with a moderately complex application, is unlikely to succeed.

i will try thx, and i will post my results

here is the final fixed library, i will make a readme soon

Well, that is a remarkable transformation. I haven't tested it but the code looks very clean and compact.

EDIT
It more usual to have the Serial.begin() statement in the .ino file instead of a library file.

No need to assign _buttonArrows and _selectButton in the constructor's body, you already did that in the initializer list.

arduino_computer::arduino_computer(LiquidCrystal& lcdRef, int* buttonArrows, int selectButton)
    : _lcd(lcdRef), _buttonArrows(buttonArrows), _selectButton(selectButton)
{
    _buttonArrows = buttonArrows;
    _selectButton = selectButton;
}