// made by rexday improved by ChatGPT :)
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int counter = 0; // Counter variable
int count_pin = 2; // Button pin
int count_state = LOW; // Current state of the button
int last_count_state = LOW; // Previous state of the button
void setup() {
pinMode(count_pin, INPUT); // Set button pin as input (external pull-up in place)
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Counter: ");
Serial.begin(9600); // Initialize serial communication
}
void loop() {
count();
}
void count() {
count_state = digitalRead(count_pin); // Read button state
// Detect button press (state change from LOW to HIGH)
if (count_state == HIGH && last_count_state == LOW) {
counter++;
Serial.println(counter); // Print counter value to Serial Monitor
lcd.setCursor(9, 0); // Adjust position for counter display
lcd.print(counter);
}
// Update last state for next loop iteration
last_count_state = count_state;
delay(50); // Short delay for debouncing
}