I now have a functioning chess clock. Lots of work yet to be done, but the basic functions are all working.
I took the code from the earlier clock test, and changed it from sending the output to the serial port, to writing the time to the displays. I also made the updates every 1/10 second.
This picture shows the two buttons and the green LEDs which indicate whose turn it is to move:
The buttons are wired to pins 2 and 3, with 10 K ohm pull-up resistors to +5V. Closing the switch pulls the pin to ground.
The LED anodes are connected to pins 4 and 5, with 560 ohm resistors between the cathode and ground. (similar to the schematics published earlier, but the pins have changed).
This next picture shows the display for player 2. Note, it is counting down tenths of seconds.
I will attach the sketch at the bottom of this post.
Big Numbers
I also did some work on making large numbers for the time display. (I don't know why this blog page insists on turning the image sideways, it is horizontal on my PC, but you can still see the large digits).
These large numbers were made by combining 7 custom characters. The custom characters are numbered 0 through 7, but I used 1 through 7 because those were the numbers I used when I drew out the patterns on graph paper. Note, if you use character 0, the data type must be declared something like this: lcd.write((byte)0); It has something to do with control codes vs. custom characters. Luckily, since I used 1-7, I didn't run into this issue, but read about it on other web pages.
Some pieces of the sketch that created these large numbers:
#include <LiquidCrystal.h>
LiquidCrystal lcd1(6,7,9,10,11,12);
LiquidCrystal lcd2(6,8,9,10,11,12);
// create bit maps of the custom characters. 5 columns by 8 rows.
byte customChar1[8] = {
B00000,
B00000,
B00000,
B00000,
B00011,
B00011,
B00011,
B00011
};
.
.
.
void setup() {
lcd1.begin(20, 4);
lcd2.begin(20, 4);
lcd1.createChar(1,customChar1); // assign the bitmap pattern to character 1
.
.
.
}
void loop() {
lcd1.setCursor(3,0);
lcd1.write(1); // write character 1
.
.
.
}
And now, my most up to date clock sketch (from the photos at the top of this post):
// // Chess Clock Practice Sketches // // Bob Nolan // 3/26/2015 // // Include LCD display library and set up pinouts #include <LiquidCrystal.h> LiquidCrystal lcd1(6,7,9,10,11,12); LiquidCrystal lcd2(6,8,9,10,11,12); // Declare Global Variables unsigned int timeR1; unsigned int timeR2; unsigned long lastMilliS; int turn; int ledPin1 = 4; int ledPin2 = 5; int switchPin1 = 2; int switchPin2 = 3; boolean checkButton; boolean flag1=0; boolean flag2=0; void setup() { timeR1 = 150; // set initial time. Time is in 1/10 sec timeR2 = 150; // need to add user input routine turn=0; pinMode(ledPin1,OUTPUT); pinMode(ledPin2,OUTPUT); pinMode(switchPin1,INPUT); pinMode(switchPin2,INPUT); lcd1.begin(20,4); lcd2.begin(20,4); lcd1.print("Player 1"); lcd2.print("Player 2"); } void loop() { if (turn==0){ // dont start counting until 1st button press lastMilliS=millis(); } if (millis()-lastMilliS >= 100) { // every 100 msec, update time lastMilliS=millis(); if((turn==1)&&(!flag1)){ timeR1--; } if((turn==2)&&(!flag2)){ timeR2--; } printTime(); } checkButton=digitalRead(switchPin1); // player 1 button press if(!checkButton && (turn!=2)){ digitalWrite(ledPin2,HIGH); digitalWrite(ledPin1,LOW); turn = 2; printTime(); } checkButton=digitalRead(switchPin2); // player 2 button press if(!checkButton && (turn!=1)){ digitalWrite(ledPin1,HIGH); digitalWrite(ledPin2,LOW); turn = 1; printTime(); } if((timeR1<=0)&&(!flag1)){ // player 1 flag lcd1.setCursor(0,2); lcd1.print("Flag"); flag1=1; } if((timeR2==0)&&(!flag2)){ // player 2 flag lcd2.setCursor(0,2); lcd2.print("Flag"); flag2=1; } } void printTime() { // print time function lcd1.setCursor(0,1); lcd1.print(timeR1); lcd1.print(" "); lcd2.setCursor(0,1); lcd2.print(timeR2); lcd2.print(" "); }
No comments:
Post a Comment