#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with its I2C address (0x27 for a common 16x2 display)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int resetButtonPin = 2; // Pin for the reset button
const int countButtonPin = 3; // Pin for the count button
int count = 0; // Counter variable
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0); // Set the cursor to the top-left corner
lcd.print("Count: "); // Display initial text
// Set button pins as inputs with internal pull-up resistors
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(countButtonPin, INPUT_PULLUP);
}
void loop() {
// Check if the count button is pressed
if (digitalRead(countButtonPin) == LOW) {
// Increment the count and update the LCD
count++;
updateDisplay();
delay(200); // Debounce delay to avoid multiple increments
}
// Check if the reset button is pressed
if (digitalRead(resetButtonPin) == LOW) {
// Reset the count to zero and update the LCD
count = 0;
updateDisplay();
delay(200); // Debounce delay to avoid multiple resets
}
}
void updateDisplay() {
lcd.setCursor(7, 0); // Set cursor to the position after "Count: "
lcd.print(" "); // Clear the previous count
lcd.setCursor(7, 0); // Set cursor back
lcd.print(count); // Display the updated count
}