#include <LiquidCrystal_I2C.h> // Library for I2C LCD
#include <RotaryEncoder.h> // Library for Rotary Encoder
// LCD I2C setup (address: 0x27, columns: 16, rows: 4)
LiquidCrystal_I2C lcd(0x27, 16, 4);
// Rotary Encoder Pins
#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
// Rotary Encoder Initialization
RotaryEncoder encoder(CLK_PIN, DT_PIN);
// Menu Variables
int menuLevel = 0;
int menuIndex = 0;
const char *mainMenu[] = {"Univ Batna 1", "Univ Batna 2"};
const char *faculties[] = {"Faculty 1", "Faculty 2","Faculty 3","Faculty 4"};
const char *departments[] = {"Dept 1", "Dept 2","Dept 3","Dept 4"};
const char *specialties[] = {"Specialty 1", "Specialty 2","Specialty 3","Specialty 4"};
int studentCount = 100; // Default student count
void setup() {
pinMode(SW_PIN, INPUT_PULLUP); // Rotary encoder button setup
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome!");
delay(2000);
displayMenu();
}
void loop() {
static int lastPosition = 0;
encoder.tick();
// Check rotary encoder movement
int newPosition = encoder.getPosition();
if (newPosition != lastPosition) {
if (newPosition > lastPosition) {
menuIndex++;
} else {
menuIndex--;
}
lastPosition = newPosition;
// Wrap around menu index
if (menuLevel == 0) {
if (menuIndex >= 2) menuIndex = 0;
if (menuIndex < 0) menuIndex = 1;
}
displayMenu();
}
// Check button press
if (digitalRead(SW_PIN) == LOW) {
handleButtonPress();
delay(300); // Debounce delay
}
}
// Function to display the menu based on the current level
void displayMenu() {
lcd.clear();
lcd.setCursor(0, 0);
switch (menuLevel) {
case 0: // Main Menu
lcd.print(mainMenu[menuIndex]);
break;
case 1: // Faculties
lcd.print("Faculties:");
lcd.setCursor(0, 1);
lcd.print(faculties[menuIndex]);
break;
case 2: // Departments
lcd.print("Departments:");
lcd.setCursor(0, 1);
lcd.print(departments[menuIndex]);
break;
case 3: // Specialties
lcd.print("Specialties:");
lcd.setCursor(0, 1);
lcd.print(specialties[menuIndex]);
break;
case 4: // Student Count
lcd.print("Students Count:");
lcd.setCursor(0, 1);
lcd.print(studentCount);
break;
}
}
// Function to handle button press
void handleButtonPress() {
if (menuLevel < 4) {
menuLevel++;
menuIndex = 0; // Reset menu index for sub-menus
} else {
// Adjust student count
studentCount += (menuIndex > 0) ? 1 : -1;
if (studentCount < 0) studentCount = 0;
}
displayMenu();
}