Dynamic arduino LCD menu

The following Dynamic Arduino LCD menu is easy to use, easy to edit, expand. You can add as many menu items as you want. Just enter the menu items, write the corresponding function and you’re done. All this at the beginning of the arduino sketch, you don’t have to search the code for where to paste the code snippets. It can be a good solution for both beginners and advanced users if you need to create an LCD menu quickly.

Proper external power is highly recommended, the USB port power is low to power the LCD display and the Arduino UNO. You can also use other boards, such as: Arduino Nano, Mini, Leonardo, Mega, or ESP 8266, ESP32, whichever suits you.

It is very easy to use. In the MenuItems [] String array, enter the names of the menu items, and then type the function for the menu item in an if () statement in the menuFunctions () function. the condition for if () must be the serial number of the menu item. That’s it, you can use the dynamic arduino menu.

amazon
Roku Express HD Streaming Media Player with High Speed HDMI Cable and Simple Remote
amazon
Roku Streaming Stick 4K | Streaming Device 4K/HDR/Dolby Vision with Roku Voice Remote and TV Controls

advertising – amazon.com

I have written a few examples in the arduino sketch, you can delete or rewrite them, I just intended them as guidelines for easier understanding.

For some reason, the lcd.clear () function did not work on one of my 4 x 20 character LCDs, using the lcd.noBacklight () function solved the problem. This lcd module has the PCF8574A expander IC with an I2C address of “0x3F”. The other 4 x 20 character LCD display is connected to the I2C bus via PCF8574 IC, address of “0x27”. This is the second worked perfectly.

The first example was for a 4 x 20 character LCD display.

/**************************************/
/*      https://myhomethings.eu       */
/*                                    */
/**************************************/

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4);   // I2C address: "0x3F" or "0x27"

const byte upButtonPin = 4;
const byte downButtonPin = 5;
const byte rightButtonPin = 6;
const byte leftButtonPin = 7;
const byte tweeterPin = 9;
const byte ledPin = 13;
const byte ledPin_fade = 10; 
int fade = 0;


String MenuItems[] = {  // Your menu items
  "LED Switch",
  "LED Brightness",
  "menuitem 3",
  "menuitem 4",
  "menuitem 5",
  "menuitem 6" 
  // and so on... 
};

void menuFunctions(int menu, byte right, byte left)  // Your menu functions
{
  if(menu == 1) // example function for 1. menu item
  {
    if(right == 1)
    {
      lcd.setCursor(0, 3);
      lcd.print("Off   ");
      lcd.setCursor(10, 3);
      lcd.print("On  <<");
      digitalWrite(ledPin, HIGH);
    }
    if(left == 1)
    {
      lcd.setCursor(0, 3);
      lcd.print("Off <<");
      lcd.setCursor(10, 3);
      lcd.print("On    ");
      digitalWrite(ledPin, LOW);
    }
  }
  if(menu == 2) // example function for 2. menu item
  {
    if(right == 1)
    {
      fade += 20;
      if(fade >= 255)
      {
        fade = 255;
      }
    }
    if(left == 1)
    {
      fade -= 20;
      if(fade <= 0)
      {
        fade = 0;
      }
    }
    lcd.setCursor(0, 3);
    lcd.print("Brightness:     ");
    lcd.setCursor(12, 3);
    lcd.print(fade);
    analogWrite(ledPin_fade, fade);
  }
  if(menu == 3)
  {
    lcd.setCursor(0,2);
    lcd.print("Hello Menu Item 3");
  }
  // and so on... 
}


/***  do not modify  ***********************************************/
template< typename T, size_t NumberOfSize > 
size_t MenuItemsSize(T (&) [NumberOfSize]){ return NumberOfSize; }
int numberOfMenuItems = MenuItemsSize(MenuItems) - 1;
int currentMenuItem = 0;
int previousMenuItem = 1;
byte button_flag = 0;
unsigned long previousMillis = millis();
const int note = 4699;
void beepsOnce()
{
  tone(tweeterPin, note, 125);
  delay(60);
  noTone(tweeterPin);
}
/*******************************************************************/

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin_fade, OUTPUT);

  /************************************/
  pinMode(upButtonPin, INPUT_PULLUP);
  pinMode(downButtonPin, INPUT_PULLUP);
  pinMode(rightButtonPin, INPUT_PULLUP);
  pinMode(leftButtonPin, INPUT_PULLUP);
  
  lcd.begin();
  //lcd.noBacklight();
  lcd.clear();
  lcd.setCursor(2, 1);
  lcd.print("myhomethings.eu");
  lcd.setCursor(0, 3);
  lcd.print("Simple Dynamic Menu");
  delay(5000);
  lcd.clear();
}

void loop() 
{
  if(digitalRead(rightButtonPin) == LOW && button_flag == 0)
  {
    menuFunctions(currentMenuItem + 1, 1, 0);
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(digitalRead(leftButtonPin) == LOW && button_flag == 0)
  {
    menuFunctions(currentMenuItem + 1, 0, 1);
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(digitalRead(upButtonPin) == LOW && button_flag == 0)
  {
    ++currentMenuItem;
    if(currentMenuItem > numberOfMenuItems )
    {
      currentMenuItem = numberOfMenuItems ;
    }
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  else if(digitalRead(downButtonPin) == LOW && button_flag == 0)
  {
    currentMenuItem--;
    if(currentMenuItem < 0)
    {
      currentMenuItem = 0;
    }
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(currentMenuItem != previousMenuItem)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Menu Item:");
    lcd.setCursor(0, 1);
    lcd.print(MenuItems [currentMenuItem]);
    menuFunctions(currentMenuItem + 1, 0, 0);
    previousMenuItem = currentMenuItem;
  }
  if(millis() - previousMillis >= 400) 
  {
    previousMillis = millis();
    button_flag = 0;
  }
}

advertising – Amazon.com


The following is an example of an arduino menu that can be used with a 2 x 16 character LCD display:

/**************************************/
/*      https://myhomethings.eu       */
/*                                    */
/**************************************/

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);   // I2C address: "0x3F" or "0x27"

const byte upButtonPin = 4;
const byte downButtonPin = 5;
const byte rightButtonPin = 6;
const byte leftButtonPin = 7;
const byte tweeterPin = 9;
const byte ledPin = 13;
const byte ledPin_fade = 10; 
int fade = 0;


String MenuItems[] = {  // Your menü items
  "LED Switch",
  "LED Brightness",
  "menuitem 3",
  "menuitem 4",
  "menuitem 5",
  "menuitem 6" 
  // and so on... 
};

void menuFunctions(int menu, byte right, byte left)  // Your menü functions
{
  if(menu == 1) // example function for 1. menü item
  {
    if(right == 1)
    {
      lcd.setCursor(0, 1);
      lcd.print("Off   ");
      lcd.setCursor(10, 1);
      lcd.print("On  <<");
      digitalWrite(ledPin, HIGH);
    }
    if(left == 1)
    {
      lcd.setCursor(0, 1);
      lcd.print("Off <<");
      lcd.setCursor(10, 1);
      lcd.print("On    ");
      digitalWrite(ledPin, LOW);
    }
  }
  if(menu == 2) // example function for 2. menü item
  {
    if(right == 1)
    {
      fade += 20;
      if(fade >= 255)
      {
        fade = 255;
      }
    }
    if(left == 1)
    {
      fade -= 20;
      if(fade <= 0)
      {
        fade = 0;
      }
    }
    lcd.setCursor(0, 1);
    lcd.print("Brightness:     ");
    lcd.setCursor(12, 1);
    lcd.print(fade);
    analogWrite(ledPin_fade, fade);
  }
  if(menu == 3)
  {
    lcd.setCursor(0, 1);
    lcd.print("Hello Menu Item 3");
  }
  // and so on... 
}


/***  do not modify  ***********************************************/
template< typename T, size_t NumberOfSize > 
size_t MenuItemsSize(T (&) [NumberOfSize]){ return NumberOfSize; }
int numberOfMenuItems = MenuItemsSize(MenuItems) - 1;
int currentMenuItem = 0;
int previousMenuItem = 1;
byte button_flag = 0;
unsigned long previousMillis = millis();
const int note = 4699;
void beepsOnce()
{
  tone(tweeterPin, note, 125);
  delay(60);
  noTone(tweeterPin);
}
/*******************************************************************/

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin_fade, OUTPUT);

  /************************************/
  pinMode(upButtonPin, INPUT_PULLUP);
  pinMode(downButtonPin, INPUT_PULLUP);
  pinMode(rightButtonPin, INPUT_PULLUP);
  pinMode(leftButtonPin, INPUT_PULLUP);
  
  lcd.begin();
  // lcd.noBacklight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Simple");
  lcd.setCursor(3, 1);
  lcd.print("Dynamic Menu");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("myhomethings.eu");
  delay(5000);
  lcd.clear();
}

void loop() 
{
  if(digitalRead(rightButtonPin) == LOW && button_flag == 0)
  {
    menuFunctions(currentMenuItem + 1, 1, 0);
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(digitalRead(leftButtonPin) == LOW && button_flag == 0)
  {
    menuFunctions(currentMenuItem + 1, 0, 1);
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(digitalRead(upButtonPin) == LOW && button_flag == 0)
  {
    ++currentMenuItem;
    if(currentMenuItem > numberOfMenuItems )
    {
      currentMenuItem = numberOfMenuItems ;
    }
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  else if(digitalRead(downButtonPin) == LOW && button_flag == 0)
  {
    currentMenuItem--;
    if(currentMenuItem < 0)
    {
      currentMenuItem = 0;
    }
    button_flag = 1;
    previousMillis = millis();
    beepsOnce();
  }
  if(currentMenuItem != previousMenuItem)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(MenuItems [currentMenuItem]);
    menuFunctions(currentMenuItem + 1, 0, 0);
    previousMenuItem = currentMenuItem;
  }
  if(millis() - previousMillis >= 400) 
  {
    previousMillis = millis();
    button_flag = 0;
  }
}

I hope you enjoyed this article, similar to Arduino sketches you can find at the link below. Have fun.

ESP32, ESP8266, Arduino sketches, examples


SAMSUNG Galaxy Tab S7 11-inch Android Tablet 128GB Wi-Fi Bluetooth S Pen Fast Charging USB-C Port

SAMSUNG Galaxy Tab S7 11-inch Android Tablet 128GB

Wi-Fi Bluetooth S Pen Fast Charging USB-C Port

advertising – amazon.com

Smart Lock - Keyless Entry Door Locks Fingerprint, Passcode, Fob, Smart Phone

Smart Lock – Keyless Entry Door Locks

Fingerprint, Passcode, Fob, Smart Phone Door

Locksadvertising – Amazon.com