#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address and LCD dimensions based on your setup
int counter = 0; // Digital counter variable
int buttonUp = 2; // Connect the "Up" button to digital pin 2
int buttonDown = 3; // Connect the "Down" button to digital pin 3
int buttonReset = 4;
void setup() {
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Counter:");
// Load the counter value from EEPROM
counter = EEPROM.read(0);
// Display the initial counter value
lcd.setCursor(9, 0); // Set the cursor position to the right of "Counter:"
lcd.print(counter);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonUp) == LOW) {
incrementCounter();
delay(200); // Debounce delay
}
if (digitalRead(buttonDown) == LOW) {
decrementCounter();
delay(200); // Debounce delay
}
if (digitalRead(buttonReset) == LOW) {
resetCounter();
delay(200);
}
}
void incrementCounter() {
counter++;
updateLCD();
}
void decrementCounter() {
counter--;
updateLCD();
}
void resetCounter() {
counter = 0;
updateLCD();
}
void updateLCD() {
lcd.setCursor(9, 0);
lcd.print(" "); // Clear the previous counter value
lcd.setCursor(9, 0);
lcd.print(counter);
// Store the counter value in EEPROM
EEPROM.write(0, counter);
}