#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16x2 LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the button pins
const int btn_blue = 9;
const int btn_green = 5;
const int btn_yellow = 4;
const int btn_red = 3;
// Variables to store button states
int Button1State = 0;
int Button2State = 0;
int Button3State = 0;
int Button4State = 0;
// Variable to store the count
int Counter = 0;
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Print initial message on the LCD
lcd.setCursor(5, 0);
lcd.print("SCORE!");
// Set button pins as inputs with internal pull-up resistors
pinMode(btn_blue, INPUT_PULLUP);
pinMode(btn_green, INPUT_PULLUP);
pinMode(btn_red, INPUT_PULLUP);
pinMode(btn_yellow, INPUT_PULLUP);
}
void loop() {
// Read the button states
Button1State = digitalRead(btn_blue);
Button2State = digitalRead(btn_green);
Button3State = digitalRead(btn_red);
Button4State = digitalRead(btn_yellow);
// Check if any button is pressed (LOW because of internal pull-up)
if (Button1State == LOW) {
Counter++;
delay(200); // Debounce delay
}
if (Button2State == LOW) {
Counter++;
delay(200); // Debounce delay
}
if (Button3State == LOW) {
Counter++;
delay(200); // Debounce delay
}
if (Button4State == LOW) {
Counter++;
delay(200); // Debounce delay
}
// Display the count on the LCD
lcd.setCursor(7,1);
lcd.print(Counter);
delay(100); // Small delay to avoid flicker
}