#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonTotal = 18;
const int buttonUp = 5;
const int buttonDown = 4;
const int buttonReset = 19;
int counterTotal = 0;
int counterGood = 0;
int totalState = 0;
int upState = 0;
int downState = 0;
int resetState = 0;
int lastTotalState = 0;
int lastUpState = 0;
int lastDownState = 0;
int lastResetState = 0;
unsigned long resetPressedTime = 0;
const unsigned long resetHoldTime = 3000;
void setup() {
lcd.init();
lcd.backlight();
pinMode(buttonTotal, INPUT_PULLUP);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
totalState = digitalRead(buttonTotal);
upState = digitalRead(buttonUp);
downState = digitalRead(buttonDown);
resetState = digitalRead(buttonReset);
if (totalState == LOW && lastTotalState == HIGH) {
delay(50); // Debounce delay
counterTotal++;
counterGood++;
}
lastTotalState = totalState;
if (upState == LOW && lastUpState == HIGH) {
if (counterGood < counterTotal) {
delay(50);
counterGood++;
}
}
lastUpState = upState;
if (downState == LOW && lastDownState == HIGH) {
delay(50); // Debounce delay
if (counterGood > 0) {
counterGood--;
}
}
lastDownState = downState;
if (resetState == LOW) {
if (lastResetState == HIGH) {
resetPressedTime = millis();
} else if (millis() - resetPressedTime >= resetHoldTime) {
counterTotal = 0;
counterGood = 0;
lcd.clear();
}
}
lastResetState = resetState;
lcd.setCursor(0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Total: ");
lcd.print(counterTotal);
lcd.setCursor(0, 1);
lcd.print("Good: ");
lcd.print(counterGood);
delay(500);
}