/*
// Include libraries
#include <Wire.h> // For I2C communication
#include <LiquidCrystal_I2C.h> // For LCD display
#include <LcdMenu.h> // For menu interface
#include <Encoder.h> // For encoder input
// Define pins
#define ENCODER_PIN_A 32 // Encoder pin A connected to digital pin 2
#define ENCODER_PIN_B 33 // Encoder pin B connected to digital pin 3
#define ENCODER_BUTTON_PIN 25 // Encoder button connected to digital pin 4
#define LCD_ADDRESS 0x27 // I2C address of LCD display
// Initialize objects
LiquidCrystal_I2C lcd(0x27, 16, 2); // Create LCD object
Encoder encoder(ENCODER_PIN_A, ENCODER_PIN_B); // Create encoder object
LcdMenu menu(&lcd, &encoder, ENCODER_BUTTON_PIN); // Create menu object
// Define variables
int option1 = 0; // Variable for option 1
int option2 = 0; // Variable for option 2
int option3 = 0; // Variable for option 3
// Function to initialize LCD display
void initLCD() {
lcd.init(); // Initialize LCD display
lcd.backlight(); // Turn on LCD backlight
}
// Function to initialize encoder
void initEncoder() {
pinMode(ENCODER_PIN_A, INPUT); // Set encoder pin A as input
pinMode(ENCODER_PIN_B, INPUT); // Set encoder pin B as input
pinMode(ENCODER_BUTTON_PIN, INPUT_PULLUP); // Set encoder button as input with internal pull-up resistor
}
// Function to initialize menu
// Function to handle encoder input
void handleEncoder() {
int encoderValue = encoder.read(); // Read encoder value
if (encoderValue > 0) { // If encoder is rotated clockwise
menu.next(); // Move to next menu item
} else if (encoderValue < 0) { // If encoder is rotated counterclockwise
menu.previous(); // Move to previous menu item
}
if (digitalRead(ENCODER_BUTTON_PIN) == LOW) { // If encoder button is pressed
menu.select(); // Select current menu item
}
}
*/
/*
* LiquidMenu library - I2C_menu.ino
* IMPORTANT: To configure the library for I2C connection define I2C
* as "true" in the "LiquidMenu_config.h" file.
*
* This is the "hello_menu" example configured for I2C connection.
*
* The difference in using an I2C display library instead of the
* official LiquidCrystal library is that void LiquidMenu::init()
* method needs to be called in setup() after the I2C display library
* is initialized. The other difference is that I2C needs to be defined
* as "true" in the "LiquidMenu_config.h" file.
*
* The circuit:
* https://raw.githubusercontent.com/VaSe7u/LiquidMenu/master/examples/I_I2C_menu/I2C_menu.png
* - PCF8574 module SCL to Arduino pin A5
* - PCF8574 module SDA to Arduino pin A4
* - PCF8574 module VCC to Arduino 5V
* - PCF8574 module GND to Arduino GND
*
* Created March 27, 2017
* by Vasil Kalchev
*
* https://github.com/VasilKalchev/LiquidMenu
*
*/
#include <Wire.h>
// The I2C LCD library
#include <LiquidCrystal_I2C.h>
// The menu wrapper library
#include "LiquidMenu.h"
#include "LiquidMenu_const.h"
// The I2C LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);
/*
* Variable 'analogReading' is later configured to
* be printed on the display. 'lastAnalogReading'
* is used to check if the variable has changed.
*/
const byte analogPin = 34;
unsigned short analogReading = 0;
unsigned short lastAnalogReading = 0;
/*
* Variables used for periodic execution of code. The first one is the period
* in milliseconds and the second one is the last time the code executed.
*/
unsigned int period_check = 1000;
unsigned long lastMs_check = 0;
unsigned int period_nextScreen = 5000;
unsigned long lastMs_nextScreen = 0;
/*
* LiquidLine objects represent a single line of text and/or variables
* on the display. The first two parameters are the column and row from
* which the line starts, the rest of the parameters are the text and/or
* variables that will be printed on the display. They can be up to four.
*/
// Here the line is set to column 1, row 0 and will print the passed
// string and the passed variable.
LiquidLine welcome_line1(1, 0, "LiquidMenu ", LIQUIDMENU_VERSION);
// Here the column is 3, the row is 1 and the string is "Hello Menu".
LiquidLine welcome_line2(1, 1, "Hello Menu I2C");
/*
* LiquidScreen objects represent a single screen. A screen is made of
* one or more LiquidLine objects. Up to four LiquidLine objects can
* be inserted from here, but more can be added later in setup() using
* welcome_screen.add_line(someLine_object);.
*/
// Here the LiquidLine objects are the two objects from above.
LiquidScreen welcome_screen(welcome_line1, welcome_line2);
// Here there is not only a text string but also a changing integer variable.
LiquidLine analogReading_line(0, 0, "Analog: ", analogReading);
LiquidScreen secondary_screen(analogReading_line);
/*
* The LiquidMenu object combines the LiquidScreen objects to form the
* menu. Here it is only instantiated and the screens are added later
* using menu.add_screen(someScreen_object);. This object is used to
* control the menu, for example: menu.next_screen(), menu.switch_focus()...
*/
LiquidMenu menu(lcd);
void setup() {
Serial.begin(250000);
pinMode(analogPin, INPUT);
// This is the I2C LCD object initialization.
lcd.init();
lcd.backlight();
// Menu initialization.
menu.init();
// This is the method used to add a screen object to the menu.
menu.add_screen(welcome_screen);
menu.add_screen(secondary_screen);
}
void loop() {
// Periodic reading of the analog pin.
if (millis() - lastMs_check > period_check) {
lastMs_check = millis();
analogReading = analogRead(analogPin);
/*
* Check if the analog value have changed
* and update the display if it has.
*/
if (analogReading != lastAnalogReading) {
lastAnalogReading = analogReading;
menu.update();
}
}
// Periodic switching to the next screen.
if (millis() - lastMs_nextScreen > period_nextScreen) {
lastMs_nextScreen = millis();
menu.next_screen();
}
}