#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Update with your I2C address
// Keypad Setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {25, 26, 27, 32}; // Row pins
byte colPins[COLS] = {12, 14, 33, 15}; // Column pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Potentiometer Setup
const int potPin = 34; // Analog input pin for potentiometer (ADC pin)
// LED Setup
const int ledPin = 23; // GPIO pin for LED
// Variables
int brightness = 0; // LED brightness (1-100%)
int lastBrightness = 0; // To detect brightness changes
bool isKeypadControl = false; // Toggle between keypad and potentiometer control
String keypadInput = ""; // Store multi-digit input from the keypad
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
// Initialize LED
pinMode(ledPin, OUTPUT);
// Confirmation
lcd.setCursor(0, 1);
lcd.print("System Ready!");
delay(2000);
lcd.clear();
}
void loop() {
// 1. Read Keypad Input
char key = keypad.getKey();
if (key) {
handleKeypadInput(key); // Process keypad input
}
// 2. Adjust Brightness
if (!isKeypadControl) {
// Potentiometer control (only if not using keypad)
int potValue = analogRead(potPin); // Read potentiometer value
brightness = map(potValue, 0, 4095, 1, 100); // Map to 1-100%
int pwmValue = map(brightness, 1, 100, 0, 255); // Map to PWM (0-255)
analogWrite(ledPin, pwmValue); // Update LED brightness
// Update LCD only if brightness changes
if (brightness != lastBrightness) {
lastBrightness = brightness;
updateLCD();
}
}
}
void handleKeypadInput(char key) {
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Key: ");
lcd.print(key);
lcd.print(" "); // Clear extra characters
// Handle special keys
if (key == '#') {
// Finalize keypad input
if (keypadInput.length() > 0) {
int value = keypadInput.toInt(); // Convert input to integer
if (value >= 1 && value <= 100) {
brightness = value; // Set brightness
int pwmValue = map(brightness, 1, 100, 0, 255); // Map to PWM (0-255)
analogWrite(ledPin, pwmValue); // Update LED brightness
isKeypadControl = true; // Switch to keypad control
lcd.setCursor(0, 1);
lcd.print("Set Bright: ");
lcd.print(brightness);
lcd.print("% ");
} else {
lcd.setCursor(0, 1);
lcd.print("Invalid Input ");
}
} else {
lcd.setCursor(0, 1);
lcd.print("No Input ");
}
keypadInput = ""; // Clear input buffer
} else if (key == '*') {
// Clear input
keypadInput = "";
lcd.setCursor(0, 1);
lcd.print("Input Cleared ");
} else if (key >= '0' && key <= '9') {
// Build multi-digit input
if (keypadInput.length() < 3) { // Limit input to 3 digits
keypadInput += key;
lcd.setCursor(0, 1);
lcd.print("Input: ");
lcd.print(keypadInput);
lcd.print(" ");
} else {
lcd.setCursor(0, 1);
lcd.print("Max 3 digits! ");
}
}
}
void updateLCD() {
// Display the current brightness on the LCD
lcd.setCursor(0, 0);
lcd.print("Brightness: ");
lcd.print(brightness);
lcd.print("% ");
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4