#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
int Team1Score = 0; // คะแนนทีม 1
int Team2Score = 0; // คะแนนทีม 2
int Team1ButtonPin = 5; // ขาของปุ่มสำหรับผู้เล่น 1
int Team2ButtonPin = 4; // ขาของปุ่มสำหรับผู้เล่น 2
int ResetButtonPin = 6; // ขาของปุ่ม Reset
void setup() {
lcd.init(); // กำหนดค่าเริ่มต้นของจอ LCD
lcd.backlight(); // เปิดไฟหลังจอ LCD
pinMode(Team1ButtonPin, INPUT_PULLUP); // กำหนดขาของปุ่มผู้เล่น 1 เป็น INPUT_PULLUP
pinMode(Team2ButtonPin, INPUT_PULLUP); // กำหนดขาของปุ่มผู้เล่น 2 เป็น INPUT_PULLUP
pinMode(ResetButtonPin, INPUT_PULLUP); // กำหนดขาของปุ่ม Reset เป็น INPUT_PULLUP
updateScoreDisplay(); // แสดงคะแนนเริ่มต้น
}
void loop() {
// ตรวจสอบการกดปุ่มและเพิ่มคะแนนตามทีม
if (digitalRead(Team1ButtonPin) == LOW) {
Team1Score++;
updateScoreDisplay();
delay(1000);
}
if (digitalRead(Team2ButtonPin) == LOW) {
Team2Score++;
updateScoreDisplay();
delay(1000);
}
// ตรวจสอบปุ่ม Reset และรีเซ็ตคะแนน
if (digitalRead(ResetButtonPin) == LOW) {
Team1Score = 0;
Team2Score = 0;
updateScoreDisplay();
delay(1000); // ป้องกันการรีเซ็ตซ้ำ
}
}
void updateScoreDisplay() {
lcd.clear(); // ลบข้อมูลที่แสดงอยู่บนจอ LCD
lcd.setCursor(2, 0); // ตั้งตำแหน่งเริ่มต้นของ cursor
lcd.print("Scoreboard Game");
lcd.setCursor(0, 1); // ตั้งตำแหน่งเริ่มต้นของ cursor
lcd.print("Team 1 : ");
lcd.print(Team1Score);
lcd.setCursor(0, 2); // ตั้งตำแหน่งเริ่มต้นของ cursor
lcd.print("Team 2 : ");
lcd.print(Team2Score);
}