#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Define pin connections
#define DIN 21 // Data input (MOSI)
#define CLK 17 // Clock signal
#define DC 19 // Data/Command pin
#define CE 18 // Chip Enable (Chip Select equivalent)
#define RST 5 // Reset pin
#define BL 15 // Backlight pin (PWM-compatible)
#define ENCODER_BUTTON_PIN 14 // Rotary encoder button pin
#define ENCODER_A 26 // Rotary encoder A pin
#define ENCODER_B 27 // Rotary encoder B pin
#define LED_PIN 25 // LED connected to this pin
// Initialize LCD
Adafruit_PCD8544 display = Adafruit_PCD8544(DIN, CLK, DC, CE, RST);
// State variables
bool menuActive = false; // Track whether the menu is active
unsigned long buttonPressTime = 0; // Track button press duration
void setup() {
// Initialize LCD
display.begin();
display.setContrast(50); // Adjust contrast as needed
display.clearDisplay();
display.display();
// Display default menu
display.setCursor(0, 0);
display.print("Menu");
display.display();
// Initialize backlight pin (optional)
pinMode(BL, OUTPUT);
digitalWrite(BL, HIGH); // Turn on backlight
// Initialize encoder and LED pins
pinMode(ENCODER_BUTTON_PIN, INPUT_PULLUP);
pinMode(ENCODER_A, INPUT);
pinMode(ENCODER_B, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Check button press
if (digitalRead(ENCODER_BUTTON_PIN) == LOW) {
if (buttonPressTime == 0) {
buttonPressTime = millis(); // Record the press time
} else if (millis() - buttonPressTime > 2000 && !menuActive) {
menuActive = true; // Activate menu after holding for 2 seconds
showMenuOptions();
}
} else {
buttonPressTime = 0; // Reset press time when button is released
}
}
void showMenuOptions() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("1. Contrast");
display.setCursor(0, 10);
display.print("2. Counter Type");
display.setCursor(0, 20);
display.print("3. Counter Number");
display.display();
// Example: Blink LED to indicate menu is active
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
}