ChessWatch 2.0

This is my second Chess clock project (the first one can be found in the My Projects menu). I wanted to intruce a number of new features which the previous version annoyingly didn’t have. These features includes:
* Light indicating which player’s turn it is. This light will also warn the player that his time is running low – the light will flash at a frequency of about 1 Hz when remaining time is between 1min and 30 seconds, a bit faster when remaining time is between 30 seconds and 10 seconds and then even faster when remaining time is below 10 seconds to 0 seconds.
* Sound indicator. In the previous version we found ourselves playing even after a player was out of time because there was no sound.
* Fischer time. This is a common term of chess people, but in the off-chance your not one of them: Fischer time is when players are given an X amount of seconds in bonus each time the player makes a move. This allows a player to increase his remaining time by moving quickly.
* Automatically identify white and black player. Whether you prefer to have the clock to the left or right of the chess board, this clock will recognize which player is what after the black player starts the game. If that explanation was insufficient then post a commend and ill demonstrate it in a new video and upload it!
Below is a video showing the functionality.

Here are some pictures from the design and build process:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#define F_CPU 8000000UL
////// FILE INCLUDES /////////
#include <stdio.h>
#include <avr/io.h>

#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#define NORMAL  2

enum { P1=1,P2,STARTUP};
enum { BOOT=0,FISCHER,MODE,TIME,START,END, PAUSE};

//////// DEFINES //////////
#define BLACK 10
#define WHITE 0
#define PUSHED 0
#define RELEASED 1

#define LEFTUP (PINC & 8)
#define LEFTDOWN (PINC & 16)
#define RIGHTUP (PINC & 2)
#define RIGHTDOWN (PINC & 4)
#define OK (PINC & 32)
#define PLAYER2  (PIND & 4)
#define PLAYER1 (PIND & 8)
#define P2LEDON     PORTD |= (1<<0)
#define P2LEDOFF    PORTD &= ~(1<<0)
#define P2LEDTOGGLE PORTD ^= (1<<0)
#define P1LEDON     PORTD |= (1<<1)
#define P1LEDOFF    PORTD &= ~(1<<1)
#define P1LEDTOGGLE PORTD ^= (1<<1)
           


//////////  TYPEDEFS  /////////////////////
typedef struct{
    int s;
    int ms;
    int min;
} time;

//////// GLOBAL VARIABLES  /////////////////
volatile time p1;
volatile time p2;
volatile time tikk;
volatile char turn = STARTUP;
volatile char started = 1;
volatile int test =0;
volatile char fischer=3;
volatile char mode=NORMAL;
volatile char white=0;
volatile int n = 30;
volatile int p1blink=0,p2blink=0;
volatile char start=BOOT;
volatile char rep=0;

/////// FUNCTION PROTOTYPES ///////////////
void brdinit(void);
void lcd_putsxy(char x, char y, char *s);
void updatelcd();

//////////  TIMER INTERRUPT ROUTINE  /////
ISR(TIMER1_COMPA_vect){
    p1blink++;
    p2blink++;
  if (start == START){
    switch (turn)
    {
   
      case P1:
        if ((p1.s==0) && (p1.min >0)) {
          p1.min--;
          p1.s=59;
        }
        if (p1.ms ==0) {
          p1.ms=99;
          p1.s--;
          break;
        }
        else p1.ms--;
        break;
   
      case P2:
      if ((p2.s==0) && (p2.min >0)) {
        p2.min--;
        p2.s=59;
      }
      if (p2.ms ==0) {
        p2.ms=99;
        p2.s--;
        break;    //NEEDED????????
       }
     else p2.ms--;
     break;
   
     case STARTUP:
     case PAUSE:
        tikk.ms++;
        break;
    }   // END SWITCH
  }
  if ((start == END) && ( rep < 10) ){

    if (test < n){
      PORTD |= (1<<7);
    }
    else PORTD &= ~(1<<7);
    if (test >= (n*2)){
      test = 0;
    }
    else test++;
  }


}

int main(){
  char buffer[20];
  char update=0;
  char p1first=1,p2first=1;
  char winner = 0;
  brdinit();
  // START MAIN LOOP
  lcd_clrscr();
  while(1)
  {
    while (start == BOOT)
    {
      lcd_putsxy(0,0,"  Sjakklokke  v2.0  ");
      lcd_putsxy(0,1,"     laget av       ");
      lcd_putsxy(0,2," Alexander Midteide ");  
      lcd_putsxy(0,3,"     20 (C) 09      ");
      _delay_ms(1000);  
      lcd_clrscr();
      start = MODE;
    }
     
    update =1;
    while (start == MODE)
    {
      if (update){
        lcd_putsxy(0,0,"Choose mode:");
        if (mode == FISCHER) {
          lcd_putsxy(0,2,"   Normal");
          lcd_putsxy(0,3,"*  Fischer");
        }
        else {
          lcd_putsxy(0,2,"*  Normal");
          lcd_putsxy(0,3,"   Fischer");
        }
        update = 0;
      }

      if (LEFTDOWN == PUSHED )
      {
        while (LEFTDOWN == PUSHED );
       
       
        mode = NORMAL;
        update=1;
      }
      if (LEFTUP == PUSHED )
      {
        while(LEFTUP == PUSHED );
        mode = FISCHER;
        update=1;
      } // if
      if (OK == PUSHED){
        _delay_ms(350);
        while (OK == PUSHED);
        if (mode == FISCHER) start = FISCHER;
        else start = TIME;
        lcd_clrscr();
        while (OK == PUSHED);
      }
    } // end while start
    update =1;
    while(start == PAUSE)
    {
      lcd_putsxy(0,0,"********************");
      lcd_putsxy(0,1,"*    GAME PAUSED   *");
      if (((turn ==P2) && (white == P2)) || ((turn == P1) && (white == P1)))
        lcd_putsxy(0,2,"*   WHITE'S MOVE   *");
      else lcd_putsxy(0,2,"*   BLACK'S MOVE   *");  
      lcd_putsxy(0,3,"********************");
      if (OK == PUSHED) {
        while (OK == PUSHED);
        start = START;
        lcd_clrscr();
        _delay_ms(50);
        while (OK == PUSHED);
      }
    }
     
    while(start == FISCHER)
    {
      if (update){
        lcd_clrscr();
        lcd_putsxy(0,0,"Enter amount of");
        lcd_putsxy(0,1,"additional seconds");
        lcd_putsxy(0,2,"per move:");
        sprintf(buffer,"%d seconds",fischer);
        lcd_putsxy(0,3,buffer);
        //lcd_putsxy(15,3," OK");
        update = 0;
      }
      if ( LEFTUP == PUSHED )  
      {
        while(LEFTUP == PUSHED);
        _delay_ms(10);
        while(LEFTUP == PUSHED);
       
        if (fischer >0) fischer--;
        update = 1;
      }
      if ( LEFTDOWN == PUSHED )  
      {
        while(LEFTDOWN == PUSHED);
        _delay_ms(10);
        while(LEFTDOWN == PUSHED);
       
        if (fischer < 59) fischer++;
        update = 1;
      }

      if ( OK == PUSHED )  
      {
        while(OK == PUSHED);
        _delay_ms(350);
        while(OK == PUSHED);
        start = TIME;
        lcd_clrscr();
        while (OK == PUSHED);
      } // end if Btm
    } // end while temp

    update = 1;
    while (start == TIME)
    {
      if (update){
        lcd_putsxy(0,0,"Set up player times:");
        lcd_putsxy(0,2,"Player 1  Player 2");
        sprintf(buffer,"%dm,%d.%02d ",p1.min,p1.s,p1.ms);    
        lcd_putsxy(WHITE,3,buffer);  
        sprintf(buffer,"%dm,%d.%02d ",p2.min,p2.s,p2.ms);    
        lcd_putsxy(BLACK,3,buffer);  
        update = 0;
      }

      if (LEFTUP == PUSHED )
      {
        while(LEFTUP == PUSHED );
        _delay_ms(10);
        while(LEFTUP == PUSHED );
        if (p1.min > 1) p1.min--;
        update =1;
      }  
      if (LEFTDOWN == PUSHED )
      {
        while(LEFTDOWN == PUSHED );
        _delay_ms(10);
        while(LEFTDOWN == PUSHED);
        if (p1.min < 60) p1.min++;
        update =1;
      }  
      if (RIGHTUP == PUSHED )
      {
        while(RIGHTUP == PUSHED );
        _delay_ms(10);
        while(RIGHTUP == PUSHED );
        if (p2.min > 1) p2.min--;
        update =1;
      }  
      if (RIGHTDOWN == PUSHED )
      {
        while(RIGHTDOWN == PUSHED );
        _delay_ms(10);
        while(RIGHTDOWN == PUSHED);
        if (p2.min < 60) p2.min++;
        update =1;
      }  
      if ( OK == PUSHED )  
      {
        while(OK == PUSHED);
        _delay_ms(10);
        while(OK == PUSHED);
        start = START;
        lcd_clrscr();
        while(OK == PUSHED);
      } // end if Btm
    }


    while(start == START){
      if (white == 0){
        lcd_putsxy(0,0,"BLACK STARTS GAME");
        lcd_putsxy(0,1,"BY PUSHING TIME");
        lcd_putsxy(0,2,"BUTTON.");
        while(PLAYER1 == PUSHED);
        while(PLAYER2 == PUSHED);
        while (white ==0){
          if (PLAYER1 == PUSHED) {
            white = P2;
            turn = P2;
        P1LEDOFF;
        P2LEDON;
          }
          if (PLAYER2 == PUSHED){
            white = P1;
            turn = P1;
        P1LEDON;
        P2LEDOFF;
          }
        }
        lcd_clrscr();
      }
       
      updatelcd();
      if (turn == P1){
        if (update){
            P1LEDON;
            P2LEDOFF;
          if (white == P1) {
            lcd_putsxy(0,3,"   WHITE TO MOVE! ");
          }
          if (white == P2) {
            lcd_putsxy(0,3,"   BLACK TO MOVE! ");
          }
          update = 0;
        }
        if ( (p1.min == 0) && (p1.s > 29)) {
          if (p1blink > 100) {
            P1LEDTOGGLE;
            p1blink = 0;
          }
        }
        if ( (p1.min == 0) && (p1.s < 29) && (p1.s > 10)) {
          if (p1blink > 30) {
            P1LEDTOGGLE;
            p1blink = 0;
          }
        }
        if ( (p1.min == 0) && (p1.s <= 10) ) {
          if (p1blink > 10) {
            P1LEDTOGGLE;
            p1blink = 0;
          }
        }
      }
   
       
      if (turn == P2){
        if (update){
            P1LEDOFF;
            P2LEDON;
          if (white == P2) {
            lcd_putsxy(0,3,"   WHITE TO MOVE! ");
          }
          if (white == P1) {
            lcd_putsxy(0,3,"   BLACK TO MOVE! ");
          }
          update = 0;
        }
        if ( (p2.min == 0) && (p2.s > 29)) {
          if (p2blink > 100) {
            P2LEDTOGGLE;
            p2blink = 0;
          }
        }
        if ( (p2.min == 0) && (p2.s < 29) && (p2.s > 10)) {
          if (p2blink > 30) {
            P2LEDTOGGLE;
            p2blink = 0;
          }
        }
        if ( (p2.min == 0) && (p2.s <= 10) ) {
          if (p2blink > 10) {
            P2LEDTOGGLE;
            p2blink = 0;
          }
        }
     }  
      if ( OK == PUSHED )  
      {
        while(OK == PUSHED);
        _delay_ms(10);
        while(OK == PUSHED);
        start = PAUSE;
        lcd_clrscr();
        while (OK == PUSHED);
      } // end if Btm
     if ( (PLAYER2 == PUSHED) && (turn != P1) ){
       if ((mode == FISCHER) && (p1first == 0) ) {
         if (white == P2) {
           p1.s += fischer;
           if (p1.s > 59) {
             p1.min++;
             p1.s -= 60;
           }
         }
         if (white == P1) {
           p2.s += fischer;
           if (p2.s > 59) {
             p2.min++;
             p2.s -= 60;
           }
         }
       }
       turn = P1;
       p1first =0;
       update = 1;
     }    
     if ( (PLAYER1 == PUSHED) && (turn != P2) ){
       if ((mode == FISCHER) && (p2first == 0) ) {
         if (white == P1) {
           p1.s += fischer;
           if (p1.s > 59) {
             p1.min++;
             p1.s -= 60;
           }
         }
         if (white == P2) {
           p2.s += fischer;
           if (p2.s > 59) {
             p2.min++;
             p2.s -= 60;
           }
         }
       }
       turn = P2;
       p2first = 0;
       update = 1;
      }  
      if ( (p1.s == 0) && (p1.min <= 0) && (p1.ms <= 0) ) {
        winner = P2;
        start = END;     
        P1LEDON;
        P2LEDON;
      }
      if ( (p2.s == 0) && (p2.min <= 0) && (p2.ms <= 0) ) {
        winner = P1;
        start = END;     
        P1LEDON;
        P2LEDON;
      }

    }
    update = 1;
    while(start == END){
      //////////// TEST IF ANY OF THE PLAYERS ARE OUT OF TIME ///////////////
      if (update){
        TIMSK &= ~(1 << OCIE1A);                //Timer/Counter1, Output Compare A Match Interrupt Enable 0x10;
        TCCR1B = (1 << CS11) | (1 << WGM12);    //0x0C;     // 256 prescale
        OCR1A = 1;                          // Gives frequency of ((8 MhZ)/256)/313) = 99.8Hz
        update =0;
        TIMSK = (1 << OCIE1A);              //Timer/Counter1, Output Compare A Match Interrupt Enable 0x10;
      }
      if ( (winner == P1) && (white == P1)){
        updatelcd();
        lcd_putsxy(0,3,"   WHITE WINS!   ");
      }
      if ( (winner == P2) && (white == P1)){
        updatelcd();
        lcd_putsxy(0,3,"   BLACK WINS!   ");
      }
      if ( (winner == P1) && (white == P2)){
        updatelcd();
        lcd_putsxy(0,3,"   BLACK WINS!   ");
      }
      if ( (winner == P2) && (white == P2)){
        updatelcd();
        lcd_putsxy(0,3,"   WHITE WINS!   ");
      }

      if (p2blink > 1) {
        P2LEDTOGGLE;
        P1LEDTOGGLE;
        p2blink = 0;
      }
      if (rep < 3) {
        rep++;
        for (int i=20;i<80;i++){
          n=i;
          _delay_us(500);
          if ((LEFTUP == PUSHED) || (LEFTDOWN == PUSHED) || (RIGHTUP == PUSHED) || (RIGHTDOWN == PUSHED) || (OK == PUSHED) || (PLAYER1 == PUSHED) || (PLAYER2 == PUSHED) ) {
            cli();
            lcd_clrscr();
            lcd_putsxy(0,0,"Restarting...");
            _delay_ms(1000);
            brdinit();
            start = MODE;
            lcd_clrscr();
          }//if
        }//for
      }//if rep
      else if (rep < 50) rep++;
      if ((LEFTUP == PUSHED) || (LEFTDOWN == PUSHED) || (RIGHTUP == PUSHED) || (RIGHTDOWN == PUSHED) || (OK == PUSHED) || (PLAYER1 == PUSHED) || (PLAYER2 == PUSHED) ) {
        cli();
        lcd_clrscr();
        lcd_putsxy(0,0,"Restarting...");
        _delay_ms(1000);
        brdinit();
        start = MODE;
        lcd_clrscr();

      }

    }// while END
  }// END MAIN LOOP
 
} // END MAIN

void brdinit(void){
 
  // INITIALIZE LCD
   
  // GPIO REGISTERS
  DDRD = 0x83;
  PORTD = 0xef;
  DDRC = 0x00;
  PORTC = 0xff;
  DDRB = 0xff;
  // TIMER INITIALIZATIONS
  TIMSK = (1 << OCIE1A);                //Timer/Counter1, Output Compare A Match Interrupt Enable 0x10;
  TCCR1B = (1 << CS12) | (1 << WGM12);  //0x0C;     // 256 prescale
  OCR1A = 313;                          // Gives frequency of ((8 MhZ)/256)/313) = 99.8Hz
  _delay_ms(100);
  P1LEDOFF;
  P2LEDOFF;
  lcd_init(LCD_DISP_ON);
  // SETTING DEFAULT PLAYING TIME FOR BOTH PLAYERS
  p1.min=5;
  p1.s=0;
  p1.ms=0;
  p2.min=5;
  p2.s=0;
  p2.ms=0;
  turn = STARTUP;
  started = 1;
  test =0;
  fischer=3;
  mode=NORMAL;
  white=0;
  n = 30;
  p1blink=0;
  p2blink=0;
  start=BOOT;
  tikk.ms=0;
  tikk.s=0;
 
  sei();  // GLOBAL INTERRUPT ENABLE
}


// USED FOR EASY PRINTING TO LCD DISPLAY
void lcd_putsxy(char x, char y, char *s){
    lcd_gotoxy(x,y);
    lcd_puts(s);

}
void updatelcd(){
  char buffer[20];
        if (white == P1){
          sprintf(buffer,"%dm,%d.%02d ",p1.min,p1.s,p1.ms);  
          lcd_putsxy(0,1,buffer);    
          sprintf(buffer,"%dm,%d.%02d ",p2.min,p2.s,p2.ms);  
          lcd_putsxy(10,1,buffer);   
          lcd_putsxy(WHITE,0,"WHITE");   
          lcd_putsxy(BLACK,0,"BLACK");   
        }
        if (white == P2){
          sprintf(buffer,"%dm,%d.%02d ",p1.min,p1.s,p1.ms);  
          lcd_putsxy(0,1,buffer);    
          sprintf(buffer,"%dm,%d.%02d ",p2.min,p2.s,p2.ms);  
          lcd_putsxy(10,1,buffer);   
          lcd_putsxy(BLACK,0,"WHITE");   
          lcd_putsxy(WHITE,0,"BLACK");   
        }
}

5 thoughts on “ChessWatch 2.0

  1. neelaykulkarni

    hi
    First of all thanks for making such a clock.
    Secondly i wish to make a similar one
    Already have made the analog way back.
    Now its time 4 a digital one
    So just wanted to ask u about the details of ur clock
    The ics used,circuit diagram.
    I am trying mine using MC 14510 and a rocker switch but i can’t have modes such as fisher in that.
    So plz help
    Thanks

    1. AMElectronicsBlog Post author

      Hi,

      The ICs used are Atmel Mega8 microcontroller, a Lm7805 linear voltage regulator and a LM386 for amplifying the sound for the speaker.
      I don’t know if I have a circuit diagram for it, though the circuit is really simple. You just read the input of the buttons – the hard part is writing the code. I made this writing the code in C, using AVR Studio together win the WinAVR package. If you’re interested in the code let me know and I’ll post it on here as well.

      Good luck!

      - Alexander

      1. neelaykulkarni

        Ok u can put ur code plz
        i will refer that
        actually never wrote a c code for 8051 or pic micro-controllers
        i wrote an asm code though.
        thks for the advice
        -Neelay

  2. neelaykulkarni

    hi
    I am facing two problems.
    1.The LCD display u have used is too small.Hence i decided to use 4×40 char display
    Will there be any problems???
    2.Where (in the code)have u initialized frequency of 1 milli second(1/1000)hz.
    Or have u used a crystal that gives u this frequency.That is bcoz i need hrs to be displayed rather than milliseconds.

Leave a Reply