Dokumen
Dokumen
ronaldo.rrj at gmail.com
08/2018
https://github.com/naldin/ArduinoChessClock
http://xyzturbo.blogspot.com
Features:
* Complete timer adjust using keypad to hour, minutes and seconds, of 1 sec
up to 10 hours
* Pause timer using any keypad key and release using game buttoon
* Sound to check that game button was pressed and when game is over
3D parts:
https://www.thingiverse.com/thing:3043109
Libraries used:
https://www.arduino.cc/en/Reference/EEPROM
https://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay
https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)
*/
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
//constants
int lcd_key = 0;
int adc_key_in = 0;
int countMenu = 0;
int blinkTime;
int initBlinkTime;
int cTemp = 0;
int cSecWhite = 0;
int cSecBlack = 0;
//Increment variable
int setInc;
int setSecWhite;
int horWhite;
int minWhite;
int secWhite;
int setSecBlack;
int horBlack;
int minBlack;
int secBlack;
//LCD setup
adc_key_in = analogRead(analogPin);
//https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009
)
int read_LCD_buttons()
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for
speed reasons since it will be the most likely result
*/
// For V1.0 comment the other threshold and use the one below:
//set up menu
void menuSetUp() {
case btnRIGHT:
countMenu++;
debounceKey();
break;
case btnLEFT:
countMenu--;
debounceKey();
break;
case btnUP:
horWhite++;
debounceKey();
minWhite++;
debounceKey();
secWhite++;
debounceKey();
setInc++;
debounceKey();
else { //change WB to BW
sidePlayer ^= 1;
debounceKey();
break;
}
case btnDOWN:
horWhite--;
debounceKey();
minWhite--;
debounceKey();
secWhite--;
debounceKey();
setInc--;
debounceKey();
else { //change WB to BW
sidePlayer ^= 1;
debounceKey();
break;
{
exitMenu = 0;
break;
case btnNONE:
break;
void printTimerWhite() {
lcd.setCursor(0, 1);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
lcd.setCursor(0, 1);
lcd.print(horWhite);
blinkState = 1;
initBlinkTime = millis();
else {
lcd.setCursor(0, 1);
lcd.print(horWhite);
lcd.setCursor(1, 1);
lcd.print(":");
lcd.setCursor(2, 1);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
//this fix the print value below 10 moving the cursor one point and put 0
to left
lcd.setCursor(2, 1);
lcd.print(0);
lcd.setCursor(3, 1);
lcd.print(minWhite);
else {
lcd.setCursor(2, 1);
lcd.print(minWhite);
blinkState = 1;
initBlinkTime = millis();
else {
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(2, 1);
lcd.print(0);
lcd.setCursor(3, 1);
lcd.print(minWhite);
else {
lcd.setCursor(2, 1);
lcd.print(minWhite);
lcd.setCursor(4, 1);
lcd.print(":");
lcd.setCursor(5, 1);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
//this fix the print value below 10 moving the cursor one point and put 0
to left
lcd.setCursor(5, 1);
lcd.print(0);
lcd.setCursor(6, 1);
lcd.print(secWhite);
else {
lcd.setCursor(5, 1);
lcd.print(secWhite);
blinkState = 1;
initBlinkTime = millis();
else {
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(5, 1);
lcd.print(0);
lcd.setCursor(6, 1);
lcd.print(secWhite);
else {
lcd.setCursor(5, 1);
lcd.print(secWhite);
void printTimerBlack() {
//printing hour
lcd.setCursor(9, 1);
lcd.print(horBlack);
lcd.setCursor(10, 1);
lcd.print(":");
//printing minutes
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(11, 1);
lcd.print(0);
lcd.setCursor(12, 1);
lcd.print(minBlack);
else {
lcd.setCursor(11, 1);
lcd.print(minBlack);
lcd.setCursor(13, 1);
lcd.print(":");
//print seconds
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(14, 1);
lcd.print(0);
lcd.setCursor(15, 1);
lcd.print(secBlack);
else {
lcd.setCursor(14, 1);
lcd.print(secBlack);
void printMenu() {
lcd.setCursor(0, 0);
lcd.print("SET UP W/ KEYPAD");
lcd.setCursor(8, 1);
lcd.print("Inc");
if (countMenu == 3) {
lcd.setCursor(11, 1);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
//this fix the print value below 10 moving the cursor one point and put 0
to left
lcd.setCursor(11, 1);
lcd.print(0);
lcd.setCursor(12, 1);
lcd.print(setInc);
}
else {
lcd.setCursor(11, 1);
lcd.print(setInc);
blinkState = 1;
initBlinkTime = millis();
else {
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(11, 1);
lcd.print(0);
lcd.setCursor(12, 1);
lcd.print(setInc);
else {
lcd.setCursor(11, 1);
lcd.print(setInc);
lcd.setCursor(14, 1);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
if (sidePlayer == 1) {
lcd.setCursor(14, 1);
lcd.print("WB");
else {
lcd.setCursor(14, 1);
lcd.print("BW");
blinkState = 1;
initBlinkTime = millis();
else {
if (sidePlayer == 1) {
lcd.setCursor(14, 1);
lcd.print("WB");
else {
lcd.setCursor(14, 1);
lcd.print("BW");
}
//printing labels
void printLabels() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WHITE");
lcd.setCursor(11, 0);
lcd.print("BLACK");
else {
lcd.setCursor(0, 0);
lcd.print("BLACK");
lcd.setCursor(11, 0);
lcd.print("WHITE");
lcd.setCursor(7, 1);
lcd.print("||");
lcd.setCursor(6, 0);
lcd.print("| |");
//this fix the print value below 10 moving the cursor one point and put 0 to
left
lcd.setCursor(7, 0);
lcd.print(0);
lcd.print(setInc);
lcd.setCursor(8, 0);
else {
lcd.setCursor(7, 0);
lcd.print(setInc);
digitalWrite(LED_BUILTIN, LOW);
printTimerWhite();
printTimerBlack();
void pauseGameWhite() {
//with game paused in timer white, for exit is necessary push button black
while (digitalRead(buttonBlack) == 0) {
lcd.setCursor(5, 0);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
lcd.setCursor(5, 0);
lcd.print("<");
blinkState = 1;
initBlinkTime = millis();
lcd.setCursor(5, 0);
lcd.print(" ");
void pauseGameBlack() {
//with game paused in timer white, for exit is necessary push button black
while (digitalRead(buttonWhite) == 0) {
lcd.setCursor(10, 0);
lcd.print(" ");
blinkState = 0;
initBlinkTime = millis();
lcd.setCursor(10, 0);
lcd.print(">");
blinkState = 1;
initBlinkTime = millis();
lcd.setCursor(10, 0);
lcd.print(" ");
void timerWhite() {
//cSecWhite is a counter that will increase with millis() due SEC define.
//if seconds timer below 0(-1) and minutes >=0 plus counter seconds with 59 to
minWhite--;
//if seconds and minutes timer below 0(-1) and hour is > 0 plus 59 to
minWhite = 59;
horWhite--;
//if seconds, minutes and hour timers go to 0 loop forever and print end game
while (1) {
lcd.setCursor(10, 0);
lcd.print(" ");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
lcd.setCursor(10, 0);
lcd.print("WINNER");
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
void timerBlack() {
printTimerBlack();
//timer
minBlack--;
minBlack = 59;
horBlack--;
}
//end game
while (1) {
lcd.setCursor(0, 0);
lcd.print(" ");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
lcd.setCursor(0, 0);
lcd.print("WINNER");
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
//increment to white
void incWhite() {
if (setInc > 0) {
//if seconds timer + increment > 59 and < 120 add 1 to minutes and remove 60
seconds to seconds timer
//if seconds timer + increment > 119 add 2 to minutes and remove 120 seconds
to seconds timer
minWhite = minWhite + 2;
//if minutes timer > 59 add 1 to hour and remove 60 to minutes timer
horWhite++;
//increment to black
void incBlack() {
if (setInc > 0) {
minBlack++;
minBlack = minBlack + 2;
horBlack++;
printTimerBlack();
void readEeprom() {
secWhite = EEPROM.read(0);
minWhite = EEPROM.read(1);
horWhite = EEPROM.read(2);
setInc = EEPROM.read(3);
sidePlayer = EEPROM.read(4);
}
void writeEeprom() {
EEPROM.write(0, secWhite);
EEPROM.write(1, minWhite);
EEPROM.write(2, horWhite);
EEPROM.write(3, setInc);
EEPROM.write(4, sidePlayer);
//Arduino setup
void setup() {
//setup pins
//start LCD
lcd.begin(16, 2);
lcd.clear();
//start message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Simple Arduino");
lcd.setCursor(0, 1);
lcd.print("Chess Clock");
delay(2000);
lcd.clear();
digitalWrite(LED_BUILTIN, HIGH);
horBlack = horWhite;
minBlack = minWhite;
secBlack = secWhite;
setSecBlack = secWhite;
setSecWhite = secWhite;
//Arduino loop
void loop() {
if (digitalRead(buttonBlack)) {
cTemp = SEC - cSecWhite; //difference between SEC and previous seconds count
pauseGameWhite();
}
else {
if (digitalRead(buttonWhite)) {
timerBlack();
adc_key_in = analogRead(analogPin);
pauseGameBlack();
else {
incBlack();