#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define constants
const int buttonPin = 3; // Change this to your actual button pin
const int lcdColumns = 20;
const int lcdRows = 4;
// Create LCD object
LiquidCrystal_I2C lcd(0x3F, lcdColumns, lcdRows);
// Variables for button state
int buttonState = HIGH; // Assume the button is not pressed initially
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
unsigned long servoStartTime = 0;
unsigned long servoDuration = 2000; // Adjust the duration as needed
int servoState = 0;
// Variable for button press count
int buttonPressCount = 0;
void setup() {
// Initialize LCD
lcd.begin(lcdColumns, lcdRows);
// Initialize button pin
pinMode(buttonPin, INPUT);
// Display initial count on LCD
updateLCD();
}
void loop() {
// Read the state of the button
int reading = digitalRead(buttonPin);
// Perform debouncing
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// Increment the button press count when the button is pressed
if (buttonState == LOW) {
buttonPressCount++;
updateLCD();
}
}
}
// Save the current button state for the next iteration
lastButtonState = reading;
}
void updateLCD() {
// Clear the LCD
lcd.clear();
// Display the button press count on the LCD
lcd.setCursor(0, 0);
lcd.print("Button Press Count:");
lcd.setCursor(0, 1);
lcd.print(buttonPressCount);
delay(1000); // Adjust delay as needed to control the refresh rate
}