Lcd Menu Callback function not working

Hi every one
thanks for help :hibiscus:

i have a problem with my arduino code

i am using ..
Esp32 wroom board
lcd 2004
rotary encoder

all i want to do is to make them works together correctly .

my code :point_down:

#pragma once
#include <Arduino.h>
#include <LcdMenu.h>
#include <MenuItem.h>
#include <ItemList.h>
#include <ItemInput.h>
#include <ItemToggle.h>
#include <ItemSubMenu.h>
#include <ItemCommand.h>
#include <ItemProgress.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <OneButton.h>
#include <ESP32Encoder.h>


#define LCD_ROWS 4
#define LCD_COLS 20

// Rotary Encoder Inputs
#define CLK 34
#define DT 39
#define SW 36

void inputCallback(char* value);
// For ItemToggle
void toggleBacklight(uint8_t isOn);
// For ItemCommand
void myCallbackFunction();

extern MenuItem* settingsMenu[];
extern MenuItem* mmMenu[];


/*
#define CHARSET_SIZE 10
// Create your charset
char charset[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
// Active index of the charset
uint8_t charsetPosition;
*/

MAIN_MENU(
  ITEM_BASIC("Start service"),
  ITEM_BASIC("Connect to WiFi"),
  ITEM_SUBMENU("Settings        >", settingsMenu),
  ITEM_INPUT("Connect", inputCallback)
  //ITEM_COMMAND("front", myCallbackFunction)
  //ITEM_TOGGLE("Backlight", myCallbackFunction)
  );


SUB_MENU(settingsMenu, mainMenu,
         ITEM_TOGGLE("Backlight", "ON", "OFF", toggleBacklight),
         ITEM_BASIC("Contrast"),
         ITEM_BASIC("Contrast2"),
         ITEM_SUBMENU("Contrast3", mmMenu),
         ITEM_BASIC("Contrast4"),
         ITEM_BASIC("Contrast5"),
         ITEM_BASIC("Contrast6")
         );

SUB_MENU(mmMenu, settingsMenu,
         ITEM_BASIC("lilo"),
         ITEM_BASIC("toff"),
         ITEM_BASIC("all")
         );



LcdMenu menu(LCD_ROWS, LCD_COLS);
LiquidCrystal_I2C lcd(0x3F, 20, 4);

ESP32Encoder encoder;
OneButton button(SW, true);


unsigned long pressStartTime;
int lastcount;
static unsigned long lastButtonPress = 0;
static unsigned long lastMenuTime = 0;
const long interval = 10000;

int Tab = 0;

//*************************************

void checkTicks() {
  button.tick();
}

void singleClick() {
  Serial.println("singleClick() detected.");
  menu.enter();
  if (menu.isInEditMode()){
    Serial.println("It is Edit Mode");
  }
  //menu.backspace();
  //menu.type(charset[charsetPosition]);
  lastMenuTime = millis();
}

void doubleClick() {
  Serial.println("doubleClick() detected.");
  menu.back();
  lastMenuTime = millis();
}

void multiClick() {
  int n = button.getNumberClicks();
  if (n == 3) {
    menu.right();
    Serial.println("tripleClick detected.");
    lastMenuTime = millis();
  } else if (n == 4) {
    menu.left();
    Serial.println("quadrupleClick detected.");
    lastMenuTime = millis();
  } else {
    Serial.print("multiClick(");
    Serial.print(n);
    Serial.println(") detected.");
    lastMenuTime = millis();
  }
}

void pressStart() {
  Serial.println("pressStart()");
  menu.setupLcdWithMenu(0x3F, mainMenu);
  pressStartTime = millis() - 1000;
  lastMenuTime = millis();
}

void pressStop() {
  Serial.print("pressStop(");
  Serial.print(millis() - pressStartTime);
  Serial.println(") detected.");
  lastMenuTime = millis();
}

//*************************************

void setup() {

  Serial.begin(115200);
  
  //Encoder & Button
  pinMode(SW, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SW), checkTicks, CHANGE);
  button.attachClick(singleClick);
  button.attachDoubleClick(doubleClick);
  button.attachMultiClick(multiClick);
  button.setPressMs(1000);
  button.attachLongPressStart(pressStart);
  button.attachLongPressStop(pressStop);

  encoder.attachHalfQuad(CLK, DT);
  lastcount = encoder.getCount();

  //Menu
  //charsetPosition = 0;

  //LCD Screen
  lcd.begin();
  lcd.backlight();
}

void loop() {

  button.tick();

  
  if (encoder.getCount() != lastcount) {
    if (encoder.getCount() > lastcount) {
      menu.down();
      lastMenuTime = millis();
      lastcount = encoder.getCount();
      /*
              if (menu.isInEditMode())  // Update the position only in edit mode
              charsetPosition = constrain(charsetPosition - 1, 0, CHARSET_SIZE);
              menu.drawChar(charset[charsetPosition]); 
              */
    } else {
      menu.up();
      lastMenuTime = millis();
      lastcount = encoder.getCount();
      /*
              if (menu.isInEditMode())  // Update the position only in edit mode
              charsetPosition = (charsetPosition + 1) % CHARSET_SIZE;
              menu.drawChar(charset[charsetPosition]);
              */
    }
  }
  
  int btnState = digitalRead(SW);

  if (btnState == HIGH && millis() - lastMenuTime >= interval) {
    lcd.clear();
  }
}

void inputCallback(char* value) {
  Serial.print(F("# "));
  Serial.println(value);
}

void toggleBacklight(uint8_t isOn) {
  menu.setBacklight(isOn);
}

i am using ( Thomas Forntoh Lcd menu + Esp32Encoder + Onebutton libraries ).
most of things working well , but input or command or toggle with lcd menu callback function not working at all .

Hello abdo_yasser20

Welcome to the worldbest Arduino forum ever.

I assume that you have written the programme yourself, then it is quite easy to find the error:
Use a logic analyzer to see what happens.
Insert Serial.println()ยดs at points of interrest and analyze the test results.

Have a nice day and enjoy coding in C++.

1 Like

Interesting but I have a problem with your question. After reading it I have no clue as to what you have. Post an annotated schematic showing how it was wired showing all connections, power, and ground. I do not work with frizzies or pictures. A readable picture of a hand drawn schematic is OK. Also post links to the hardware items that give the technical details. The reason there are many different devices and incompatible devices I can find with your description. After all "all i want to do is to make them works together correctly" and all we want to do is help you make it do that.

1 Like

I don't know the libraries but you may have problems issuing I2C commands (if that is what menu.xxxx() does even indirectly) within a call back routine. Try setting a flag in the call back routine and execute the menu command in the loop(). Also Serial.print() may behave incorrectly in a call back routine. Here I mean call back routines which are asynchronous with the loop, say initiated by an interrupt.

1 Like

what does that mean?

you are in the MAIN_MENU and single press the button and don't see the message "singleClick() detected." at all?

you must describe

  • what you are doing
  • what you expect that the sketch should do - on LCD and Serial!
  • and what the sketch is currently doing in reality
1 Like

Thanks paul .. you too :hibiscus:

Hi gilshultz .. thanks for your help .
I hope you are good :sunflower:

I have arrived to make lcd menu screen and his sub menu works well with encoder switch and his rotation by (esp32encoder + onebutton libraries ) .. but i can't to make " Input function " in lcd menu works .

there is my embeded hardware componants photos ..



My used modules links :point_down:

and another componantes like (4Ch relay modules , SD Card Module .. ). but it not used until now .


I am not perfect with arduino c/++ code , and all i was making is collecting some functions and worked exampels .

I am actually added " Serial.println() " to callback declaration .

Hi noiasca
yes i am receiveing masseges from serial with clickes (single , double .. )

I'm doing a program for my project , all i want to handle switches and relaies from functional user interface menu .
but until now i'm just arrived to make encoder works with main menu well .

3. Once you have created your menu, initialize LcdMenu with the menu items in the setup()

menu.setupLcdWithMenu(0x27, mainMenu); //I2C // or menu.setupLcdWithMenu(rs, en, d0, d1, d2, d3, mainMenu); // Standard

1 Like

Hi osval
I hope you are fine :sunflower:

void setup() {

  Serial.begin(115200);
  
  //Encoder & Button
  pinMode(SW, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SW), checkTicks, CHANGE);
  button.attachClick(singleClick);
  button.attachDoubleClick(doubleClick);
  button.attachMultiClick(multiClick);
  button.setPressMs(1000);
  button.attachLongPressStart(pressStart);
  button.attachLongPressStop(pressStop);

  encoder.attachHalfQuad(CLK, DT);
  lastcount = encoder.getCount();

  //Menu
  //charsetPosition = 0;
  //*********************** here it is *************
  menu.setupLcdWithMenu(0x3F, mainMenu);

  //LCD Screen
  lcd.begin();
  lcd.backlight();
}

actually i have tried what you say , but aslo not input function not working :disappointed_relieved:

1 Like

and a simple basic LCD with 1 menu never worked then ??
you should consider this library is perhaps not compatible with this chip ???? At least I had this trouble some time ago changing from AVR to ESP8266...

1 Like

with or without lcd menu library ?

input function actually workes before adding other libraries , but i don't remember how :joy:

here is the github lcd menu documentation

I'm really don't know if can interrupt function affect on other some libraries functions or not .

I think encoder uses interrupt, onebutton uses as well, and lcdmenu ?
if you say it was working before putting all these 3 features together you need to separate them.
remove interrupt from button, then if you can test without encoder library (written in idf )
I cannot go into the bits of your project. I believe you have the expertize to test implementing very raw code for button and perhaps encoder.
I firmly believe you have a problem welding those 3 features , something is messing somewhere... perhaps timers or perhaps interrupt.

1 Like

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