Hi! This Menu code was meant to run in Serial but I'm trying to transfer it on LCD. I added the library and change all Serial code to lcd codes but it displays nothing. I also tried to code them at the same time to display them both but both of them wont display anything. Until I narrow down the problem on code lcd.print() after adding this code the problem starts. Also I tried adding this code to different parts of my code and it prints if the code was place before "Menu definition" on setup. placing it after that wont display. Any ideas? TIA
P.S. I tried printing a basic code on the LCD alone and with the Serial and it prints fine. I also tried changing the lcd Library
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <ButtonDebounce.h> //Library for Debouncing Buttons
#include "Menu.h"
Menu MyMenu;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
//The DOWN button
ButtonDebounce down(9, 75);
byte BtnDownState = 0;
byte BtnDownLastState = 0;
//The RIGHT button
ButtonDebounce up(8, 75);
byte BtnRightState = 0;
byte BtnRightLastState = 0;
//The ENTER button
ButtonDebounce select(10, 75);
byte BtnEnterState = 0;
byte BtnEnterLastState = 0;
void setup()
{
Serial.begin(9600);
lcd.init(); //start LCD
lcd.backlight(); //start LCD backlight
//Menu definition (Title,Father,Down,Next)
(*(MyMenu.GetMenuCell(0))).Set("WInSSEMBLY", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(8),MyMenu.GetMenuCell(1));
(*(MyMenu.GetMenuCell(1))).Set("MCF", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(2),MyMenu.GetMenuCell(1));
(*(MyMenu.GetMenuCell(2))).Set("MCR", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(3),MyMenu.GetMenuCell(2));
(*(MyMenu.GetMenuCell(3))).Set("MCU", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(4),MyMenu.GetMenuCell(3));
(*(MyMenu.GetMenuCell(4))).Set("ME", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(5),MyMenu.GetMenuCell(4));
(*(MyMenu.GetMenuCell(5))).Set("MS", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(6),MyMenu.GetMenuCell(5));
(*(MyMenu.GetMenuCell(6))).Set("MZ", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(7),MyMenu.GetMenuCell(6));
(*(MyMenu.GetMenuCell(7))).Set("back", MyMenu.GetMenuCell(0), MyMenu.GetMenuCell(1),MyMenu.GetMenuCell(7));
(*(MyMenu.GetMenuCell(8))).Set("CASSEMBLY", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(0),MyMenu.GetMenuCell(9));
(*(MyMenu.GetMenuCell(9))).Set("MCF", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(10),MyMenu.GetMenuCell(9));
(*(MyMenu.GetMenuCell(10))).Set("MCR", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(11),MyMenu.GetMenuCell(10));
(*(MyMenu.GetMenuCell(11))).Set("MCU", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(12),MyMenu.GetMenuCell(11));
(*(MyMenu.GetMenuCell(12))).Set("ME", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(13),MyMenu.GetMenuCell(12));
(*(MyMenu.GetMenuCell(13))).Set("MS", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(14),MyMenu.GetMenuCell(13));
(*(MyMenu.GetMenuCell(14))).Set("MZ", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(15),MyMenu.GetMenuCell(14));
(*(MyMenu.GetMenuCell(15))).Set("back", MyMenu.GetMenuCell(8), MyMenu.GetMenuCell(9),MyMenu.GetMenuCell(15));
Serial.print(">");
Serial.println( (*(MyMenu.GetSelectedMenuCell())).GetTitle() );
(*(MyMenu.GetSelectedMenuCell())).SetIsSelected(true);
lcd.setCursor(0,0);
lcd.print("Hello");
lcd.setCursor(0,1);
lcd.print("World!");
}
void loop() // The main loop (runs over and over again)
{
up.update(); // read UP button
down.update(); // read DOWN button
select.update(); // read SELECT button
BtnDownState = down.state();
BtnRightState = up.state();
BtnEnterState = select.state();
//The DOWN button
if (BtnDownState != BtnDownLastState) {
if (BtnDownState == HIGH) {
Serial.print(">");
Serial.println( (*(MyMenu.GetNextDownMenuCell(MyMenu.GetSelectedMenuCell()))).GetTitle() );
}
}
BtnDownLastState = BtnDownState;
//The RIGHT button
if (BtnRightState != BtnRightLastState) {
if (BtnRightState == HIGH) {
Serial.print(">");
Serial.println( (*(MyMenu.GetNextRightMenuCell(MyMenu.GetSelectedMenuCell()))).GetTitle() );
}
}
BtnRightLastState = BtnRightState;
//The ENTER button
if (BtnEnterState != BtnEnterLastState) {
if (BtnEnterState == LOW) {
if ( (*(MyMenu.GetMenuCell(0))).GetIsSelected()==true ) {
Serial.println("This is the WIND info page");
} else if ( (*(MyMenu.GetMenuCell(1))).GetIsSelected()==true ) {
Serial.println("This is the MCF info page");
} else if ( (*(MyMenu.GetMenuCell(2))).GetIsSelected()==true ) {
Serial.println("This is the MCR info page");
} else if ( (*(MyMenu.GetMenuCell(3))).GetIsSelected()==true ) {
Serial.println("This is the MCU info page");
} else if ( (*(MyMenu.GetMenuCell(4))).GetIsSelected()==true ) {
Serial.println("This is the ME info page");
} else if ( (*(MyMenu.GetMenuCell(5))).GetIsSelected()==true ) { //UP ONE DIR
Serial.println("This is the MS info page");
} else if ( (*(MyMenu.GetMenuCell(6))).GetIsSelected()==true ) {
Serial.println("This is the MZ info page");
} else if ( (*(MyMenu.GetMenuCell(7))).GetIsSelected()==true ) {
Serial.print(">");
Serial.println( (*(MyMenu.GetFatherMenuCell(MyMenu.GetSelectedMenuCell()))).GetTitle() );
} else if ( (*(MyMenu.GetMenuCell(8))).GetIsSelected()==true ) {
Serial.println("This is the COIL info page");
} else if ( (*(MyMenu.GetMenuCell(9))).GetIsSelected()==true ) {
Serial.println("This is the MCF info page");
} else if ( (*(MyMenu.GetMenuCell(10))).GetIsSelected()==true ) {
Serial.println("This is the MCR info page");
} else if ( (*(MyMenu.GetMenuCell(11))).GetIsSelected()==true ) { //UP ONE DIR
Serial.println("This is the MCU info page");
} else if ( (*(MyMenu.GetMenuCell(12))).GetIsSelected()==true ) {
Serial.println("This is the ME info page");
} else if ( (*(MyMenu.GetMenuCell(13))).GetIsSelected()==true ) { //UP ONE DIR
Serial.println("This is the MS info page");
} else if ( (*(MyMenu.GetMenuCell(14))).GetIsSelected()==true ) {
Serial.println("This is the MZ info page");
} else if ( (*(MyMenu.GetMenuCell(15))).GetIsSelected()==true ) {
Serial.print(">");
Serial.println( (*(MyMenu.GetFatherMenuCell(MyMenu.GetSelectedMenuCell()))).GetTitle() );
}
}
}
BtnEnterLastState = BtnEnterState;
}
Here are the hearders and cpp:
#ifndef MenuCell_h
#define MenuCell_h
#include "Arduino.h"
class MenuCell
{
private:
char* Title; // string
MenuCell* Father; // not always needed if it was reference i need it every time !!!
MenuCell* NextDown;
MenuCell* NextRight;
bool IsSelected;
public:
MenuCell();
char* GetTitle(); //string
MenuCell* GetFather();
MenuCell* GetNextDown();
MenuCell* GetNextRight();
bool GetIsSelected();
void SetIsSelected(bool _IsSelected);
void Set(char* _Title, MenuCell* _Father, MenuCell* _NextDown, MenuCell* _NextRight);
};
#endif
#include "Arduino.h"
#include "MenuCell.h"
MenuCell::MenuCell()
{
IsSelected=false;
}
char* MenuCell::GetTitle()
{
return Title;
}
MenuCell* MenuCell::GetFather()
{
return Father;
}
MenuCell* MenuCell::GetNextDown()
{
return NextDown;
}
MenuCell* MenuCell::GetNextRight()
{
return NextRight;
}
bool MenuCell::GetIsSelected()
{
return IsSelected;
}
void MenuCell::SetIsSelected(bool _IsSelected)
{
IsSelected=_IsSelected;
}
void MenuCell::Set(char* _Title, MenuCell* _Father, MenuCell* _NextDown, MenuCell* _NextRight)
{
Title = _Title;
Father = _Father;
NextDown = _NextDown;
NextRight = _NextRight;
}
#ifndef Menu_h
#define Menu_h
#define byte uint8_t
#define MENUSIZE 15 //26 ELEMENTS
#include "Arduino.h"
#include "MenuCell.h"
class Menu
{
private:
MenuCell ArrayMenuCell[MENUSIZE]; // An array of MenuCell objects using default constructor
// 0 ... MENUSIZE-1
public:
Menu();
MenuCell* GetMenuCell(byte index);
MenuCell* GetNextDownMenuCell(MenuCell* current);
MenuCell* GetNextRightMenuCell(MenuCell* current);
MenuCell* GetFatherMenuCell(MenuCell* current);
MenuCell* GetSelectedMenuCell();
byte GetSize();
};
#endif
#include "Arduino.h"
#include "Menu.h"
Menu::Menu()
{
}
MenuCell* Menu::GetMenuCell(byte index)
{
return &ArrayMenuCell[index];
}
MenuCell* Menu::GetNextDownMenuCell(MenuCell* current)
{
(*current).SetIsSelected(false);
(*((*current).GetNextDown())).SetIsSelected(true);
return (*current).GetNextDown();
}
MenuCell* Menu::GetNextRightMenuCell(MenuCell* current)
{
(*current).SetIsSelected(false);
(*((*current).GetNextRight())).SetIsSelected(true);
return (*current).GetNextRight();
}
MenuCell* Menu::GetFatherMenuCell(MenuCell* current)
{
(*current).SetIsSelected(false);
(*((*current).GetFather())).SetIsSelected(true);
return (*current).GetFather();
}
MenuCell* Menu::GetSelectedMenuCell()
{
int i;
for (i = 0; i < MENUSIZE; i = i + 1) {
if (ArrayMenuCell[i].GetIsSelected()==true) {
return &ArrayMenuCell[i];
}
}
return &ArrayMenuCell[0]; //We assume that at position 0 is the root
}
byte Menu::GetSize()
{
return MENUSIZE;
}