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");
}
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
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
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.
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.