# Chess Stopwatch

971

4

## Introduction: Chess Stopwatch

Hello guys , welcome to another fun project to make. In this tutorial we will make an Arduino based chess stop watch.

So, without further ado let's get started.

Note:

This project was made in FABLAB Dhahran by Hussain AlHudhud & Salam Al-Saif.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Requirement

For electronics we will need:

• Arduino uno.
• LCD display
• jumper wires.
• Red & Blue led.
• Round Push Button Switch On/Off

For the box design we need:

• 3mm Acrylic sheets. (we used black and gray)
• laser cutter machine to cut the design.

for the chess pieces, we just 3D printed them using our 3D printer in the lab.

## Step 2: Box Design

There are couple of online box designing websites. We used Box Designer with these dimensions 28CM x 28CM x 5CM.

During the design, don't forget to make some openings for the power cable, the LCD display, LED lights, and the push buttons on the top.

## Step 3: Electronic Circuit

1. Connect all cathodes (short lead) of LEDs to the arduino GND.
2. Connect the anode of Red LED and Blue LED to the arduino digital pin 9 and 10 Respectively.
3. Mark each button with the with corresponding function (Red player, Blue player, Reset).
4. Connect one side from each button to the arduino GND.
5. Connect the other side of Red, Blue, and Reset button to the arduino digital pin ( 6, 7, 8) Respectively.
6. Connect the LCD as the schematics.
 Arduino pin Connected to Component Pin 6 Red Button Any 7 Blue Button Any 8 Reset Button Any 9 Red LED Anode 10 Blue LED Anode 2 LCD 14 3 LCD 13 4 LCD 12 5 LCD 11 11 LCD 6 12 LCD 4

## Step 4: Code

The CODE starts by declaring the variables, and including the library that we will use.

```// Defining the pins that we used by their function.
#define ButR 6 // Red Button
#define ButB 7 // Blue Button
#define ButF 8 // Reset Button
#define RedL 9 // Red LED
#define BlueL 10 // Blue LED

#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //initialize the LCD

void(* resetFunc) (void) = 0; // This function is responsible to reset the arduino

unsigned long red_time = 0 ;
unsigned long blue_time = 0 ;
unsigned long current_time ;
unsigned long start_time ;
byte turn ; ```

Setup function:

Arduino has internal pull-up resistors (resistors that connect to power internally).

I used them so I don't need to add external resistor on the buttons.

When the button is pressed, the signal is 0. And vice versa.

```void setup() {
Serial.begin ( 9600 ) ;
lcd.begin(16, 2);
pinMode( ButR, INPUT_PULLUP ); // INPUT_PULLUP >> The button is 0 when is it pressed
pinMode( ButB, INPUT_PULLUP );
pinMode( ButF, INPUT_PULLUP );
pinMode( RedL, OUTPUT );
pinMode( BlueL, OUTPUT );

digitalWrite(RedL, HIGH);
digitalWrite(BlueL, HIGH);
Serial.println("Press the Button to start");
lcd.setCursor(0, 0);
lcd.print("Press the Button");
lcd.setCursor(0, 1);
lcd.print("  to start");

while (1) // loop until the player press the button
{
if ( digitalRead(ButR) == 0 ) // If red player press the button, he will start first
{
while (digitalRead(ButR) == 0 ); // Wait until he removes his finger.
Serial.println("Player Red play first");
blink_Led(1); // Turn on the LEDs in a specific sequence to indicate that red player start first
turn = 1 ; // This variable is used to know who is playing now. 1 indicates that it is Red player turn.
break ;
}
if ( digitalRead(ButB) == 0 )
{
Serial.println("Player Blue play first");
turn = 0 ; // 0 indicates that it is Blue player turn.
break ;
}
}
}
```

Main function:

The main function is very simple. It is toggling between Red player function and Blue player function depending on the variable "turn" .

```void loop() {

if (turn) // Red player turn
{
Red_Turn();
}
else // Red player turn
{
Blue_Turn() ;
}
}
```

Functions:

1- Red_Turn

This function does all actions that related to red player turn. It starts the red player counter and turns on the red led to indicate that it is red player turn, also it show the time you spent during the current turn. After you finished your turn, you can end the game by pressing the reset button. Or you can press your button to move to next turn.

```void Red_Turn()
{
digitalWrite(RedL, HIGH);
digitalWrite(BlueL, LOW);
start_time = millis();
Serial.println("Red palyer: Press the Button to end your turn");
lcd.clear();
lcd.print("Red player");
{
current_time = millis();
lcd.setCursor(0,1);
lcd.print("You take :");
lcd.print( (current_time - start_time)/1000.0 ,1);
}
current_time = millis();
red_time = red_time + ( current_time - start_time );
Serial.print("You take :");
Serial.print( ( current_time - start_time )/1000.0, 2);
Serial.print(" and the total time is :");
Serial.println(red_time/1000.0, 2);
Serial.println();
if (digitalRead(ButF) == 0) // If the reset button pressed, proceed to End_game function.
{
End_game();
}
turn = 0 ; // To end your turn, set "turn" to be 0 which means it is blue player turn.
}
```

2- Blue_Turn

This function is similar to Red_Turn. However, the values here is related to blue player.

```void Blue_Turn()
{
digitalWrite(RedL, LOW );
digitalWrite(BlueL, HIGH);
start_time = millis();
Serial.println("Blue palyer: Press the Button to end your turn");
lcd.clear();
lcd.print("Blue player");
{
current_time = millis();
lcd.setCursor(0,1);
lcd.print("You take :");
lcd.print( (current_time - start_time)/1000.0 ,1);
}
current_time = millis();
blue_time = blue_time + ( current_time - start_time );
Serial.print("You take :");
Serial.print( ( current_time - start_time )/1000.0 ,2 );
Serial.print(" and the total time is :");
Serial.println(blue_time/1000.0, 2);
Serial.println();
{
End_game();
}
turn = 1 ;
}
```

3- End_game

This function shows the time that each player took. If you press the button again, it will reset the arduino.

```void End_game()
{
Serial.println("Show Result");
digitalWrite(RedL, HIGH);
digitalWrite(BlueL, HIGH);

lcd.clear();
lcd.print("Red:  ");
lcd.print(red_time/1000.0);
lcd.setCursor(0,1);
lcd.print("Blue: ");
lcd.print(blue_time/1000.0);
delay(3000);
resetFunc();
}
```

This function turns on the LED in a specific sequence.

```void blink_Led(byte pin)
{
byte R,B;
if (pin)
{
R= RedL ;
B = BlueL ;
}
else
{
B= RedL ;
R= BlueL ;
}
digitalWrite(R, HIGH);
digitalWrite(B, LOW);
delay(300);
digitalWrite(R, LOW);
delay(300);
digitalWrite(R, HIGH);
delay(300);
digitalWrite(R, LOW);
delay(300);
digitalWrite(R, HIGH);
}
```

## Step 5: Final Product

Here's how the project works: