#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BUTTON_PIN 2
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 or 0x3F
int buttonState = 0;
int lastButtonState = 0;
int pressCount = 0;
void setup() {
// Initialize the LCD
lcd.begin(16, 2, LCD_5x8DOTS); // Specify the dimensions of the LCD and character size
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Press the button!");
// Initialize the button pin as input
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600); // For debugging purposes
}
void loop() {
// Read the current state of the button
buttonState = digitalRead(BUTTON_PIN);
// Check if the button is pressed
if (buttonState == LOW && lastButtonState == HIGH) {
pressCount++;
delay(50); // Debounce delay
updateLCD();
}
// Update the last button state
lastButtonState = buttonState;
}
void updateLCD() {
lcd.clear();
switch (pressCount) {
case 1:
lcd.print("Don't touch that!");
break;
case 2:
lcd.print("I'm warning you.");
break;
case 3:
lcd.print("Seriously, stop.");
break;
case 4:
lcd.print("Get lost!!");
break;
default:
lcd.print("You've pressed it ");
lcd.setCursor(0, 1);
lcd.print(pressCount);
lcd.print(" times.");
}
// For debugging purposes
Serial.print("Button pressed ");
Serial.print(pressCount);
Serial.println(" times.");
}