Arduino Chess Clock

Joined
Apr 20, 2024
Messages
1
Reaction score
0
This is my arduino code for my chess clock:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Define button pins
int RS_MITTE = 2; // Connected to digital pin D2
int RS_L = 3; // Connected to digital pin D3
int RS_R = 4; // Connected to digital pin D4
int OBEN_R = 8; // Connected to digital pin D7
int OBEN_L = 7; // Connected to digital pin D8

int mittePressed = 0;
int RWasClicked = 0;
int LWasClicked = 0;

unsigned long whiteTimeMillis;
unsigned long blackTimeMillis;

double time = 900; // Zeit in Sekunden
double inc = 10; // 10 Sekunden Inkrement standardmäßig

// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("CHESS CLOCK");
delay(1000);
pinMode(RS_MITTE, INPUT);
pinMode(RS_L, INPUT);
pinMode(RS_R, INPUT);
pinMode(OBEN_R, INPUT);
pinMode(OBEN_L, INPUT);
lcd.clear();
}

void loop() {
if (digitalRead(OBEN_R) == HIGH) {
RWasClicked = 1;
lcd.clear();
}
delay(50);

if (digitalRead(OBEN_L) == HIGH){
LWasClicked = 1;
}
delay(50);

if (RWasClicked == 0 && LWasClicked == 0) {
timeAndInc();
}
// Add a small delay to avoid continuous execution
delay(100);

if (RWasClicked == 1 || LWasClicked == 1){
startGame();
}
}

void timeAndInc() {
// Read the state of the RS_MITTE button
if (digitalRead(RS_MITTE) == HIGH) {
mittePressed = 1;
} else {
mittePressed = 0;
}

// Adjust time or increment based on button presses
if (mittePressed == 0) {
// If in time mode
if (digitalRead(RS_L) == HIGH) {
if (time >= 60) {
time += 60; // Increase time by 60 seconds
}
} else if (digitalRead(RS_R) == HIGH) {
if (time > 60) {
time -= 60; // Decrease time by 60 seconds
}
}
} else {
// If in increment mode
if (digitalRead(RS_L) == HIGH) {
if (inc < 30) {
inc += 1; // Increase increment by 1 second
}
} else if (digitalRead(RS_R) == HIGH) {
if (inc > 0) {
inc -= 1; // Decrease increment by 1 second
}
}
}

// Convert time from seconds to milliseconds
whiteTimeMillis = time * 1000;
blackTimeMillis = time * 1000;

// Clear the LCD screen
lcd.clear();

// Display time or increment based on the mode
lcd.setCursor(0, 0);
if (mittePressed == 0) {
// If in time mode, display time in minutes
lcd.print("Time: ");
int minutes = time / 60;
if (minutes < 10) {
lcd.print(" ");
}
lcd.print(minutes);
lcd.print("min");
} else {
// If in increment mode, display increment in seconds
lcd.print("Increment: ");
if ((int)inc < 10) { // Cast inc to integer to remove decimal places
lcd.print(" ");
}
lcd.print((int)inc); // Cast inc to integer to remove decimal places
lcd.print("sec");
}

delay(200);
}

void startGame() {
lcd.clear();

// Initialize time in milliseconds for both players
whiteTimeMillis = time * 1000;
blackTimeMillis = time * 1000;

// Determine which button is for White and which button is for Black
int whiteButton = (RWasClicked == 1) ? OBEN_R : OBEN_L;
int blackButton = (RWasClicked == 1) ? OBEN_L : OBEN_R;

// Determine the positions for White and Black based on button clicks
int whitePosition = (RWasClicked == 1) ? 10 : 0;
int blackPosition = (RWasClicked == 1) ? 0 : 10;

lcd.setCursor(whitePosition, 0);
lcd.print("White");
lcd.setCursor(whitePosition, 1);
printFormattedTime(whiteTimeMillis);

lcd.setCursor(blackPosition, 0);
lcd.print("Black");
lcd.setCursor(blackPosition, 1);
printFormattedTime(blackTimeMillis);

int isWhiteTurn = true; // Flag to indicate if it's White's turn
unsigned long previousMillis = millis();
unsigned long interval = 50; // Check button press every 50 milliseconds

while (true) {
unsigned long currentMillis = millis();

// Check if one second has passed
if (currentMillis - previousMillis >= 1000) {
// Decrease time for the player whose turn it is
if (isWhiteTurn) {
whiteTimeMillis -= 1000;
lcd.setCursor(whitePosition, 1);
printFormattedTime(whiteTimeMillis);
} else {
blackTimeMillis -= 1000;
lcd.setCursor(blackPosition, 1);
printFormattedTime(blackTimeMillis);
}
previousMillis = currentMillis; // Reset the timer
}

// Check button press every 50 milliseconds
if (currentMillis - previousMillis >= interval) {
// Check if it's White's turn
if (isWhiteTurn) {
// Check if White's button is pressed to switch turns
if (digitalRead(whiteButton) == HIGH) {
// Wait for button release
while (digitalRead(whiteButton) == HIGH);
// Add increment time to White's time after the move
addIncrement(whiteTimeMillis, inc * 1000);
isWhiteTurn = false; // Switch to Black's turn
previousMillis = currentMillis; // Reset the timer
}
} else {
// Check if Black's button is pressed to switch turns
if (digitalRead(blackButton) == HIGH) {
// Wait for button release
while (digitalRead(blackButton) == HIGH);
// Add increment time to Black's time after the move
addIncrement(blackTimeMillis, inc * 1000);
isWhiteTurn = true; // Switch to White's turn
previousMillis = currentMillis; // Reset the timer
}
}
}

// Check if White's time is over
if (whiteTimeMillis <= 0) {
lcd.setCursor(0, 1);
lcd.print("White lost!");
break; // Exit the loop
}

// Check if Black's time is over
if (blackTimeMillis <= 0) {
lcd.setCursor(10, 1);
lcd.print("Black lost!");
break; // Exit the loop
}

delay(10); // Add a small delay between each iteration to debounce button presses
}
}

// Function to add increment to player's time
void addIncrement(unsigned long &playerTime, unsigned long incrementMillis) {
playerTime += incrementMillis; // Add increment time to player's time in milliseconds after the move
}

void printFormattedTime(unsigned long t) {
int seconds = t / 1000; // Convert milliseconds to seconds
int minutes = seconds / 60;
seconds = seconds % 60;
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
}

But the increment time is added always before the player has made their move, not after that, no matter what i do. Can someone please help me?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top