Table Tennis Counter

by Hannibal68 in Outside > Sports

568 Views, 1 Favorites, 0 Comments

Table Tennis Counter

Pingpong1.jpeg
Pingpong2.jpeg

We have a lot of struggling with counting the points at a pingpong game. With this handy electronic counter there is no more arguing. Also the person to serve and which side to serve is pointed with the leds.
A very multi functioning gadget which is easy to use and install.

I added 2 gamemodes to choose:
The winner is who wins first 3 sets (best of five)

11 points winner - the original table tennis rules (switch serve by 2 points)
21 points winner - larger game (switch serve by 4 points)

For this counter you need the led&key module. This is a cheap arduino module with lots of possibilities.
It uses 3 control pins (SPI) and 2 pins for ground and VCC.

You need:

1. Led&Key TM 1638 module, a cheap module
2. Arduino, any model with SPI works (for example Uno, Nano)
3. 5V booster module (MT3608 step up power module)
4. 2 AA batteries
5. switch (19 x 6 x 13 mm)
6. battery contacts (buy or make)
7. 4 small screws
7. case is 3D-printed (thingiverse DIYhans)

Led & Key Module - TM1638

Led&Key.jpg
Led&Key2.jpg

The Led & Key module

This module is very cheap and has a lot of functions. There are 8 keys, 8 red leds and 8 digital led numbers.
For using it, it has to be connected to an Arduino or other microcomputer. There are 5 pins which has the following connections:

- VCC – 5v from Arduino

- Gnd – GND from Arduino

- STB – strobe pin, an output from your Arduino

- CLK – clock pin, an output from your Arduino

- DIO – data pin, another ouput from your Arduino

3D-printing Case

Led&Key case3.jpg
Led&Key case1.jpg
Led&Key_case2.png

The files for 3D printing can be found on Thingiverse.

Soldering Electronics

Pingpong3.jpeg
Tabletennis_counter_bb.png

The electronics are straight forward according to fritzing-image

Software

/*****************************
* TABLE TENNIS SCORE COUNTING
* DATE: 2-7-2019
* LED&KEY SHIELD
*****************************/

const int strobe = 10;
const int clk = 9;
const int data = 8;
/*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/
const byte digits[] = { 0x3F, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
/*A*/ /*B*/ /*b*/ /*C*/ /*d*/ /*G*/ /*H*/ /*n*/ /*O*/ /*E*/ /*L*/ /*P*/ /*r*/ /*S*/ /*t*/ /*u*/ /*y*/
const byte letter[] = { 0x77, 0x7f, 0x7c, 0x39, 0x5e, 0x7d, 0x76, 0x37, 0x3f, 0x79, 0x38, 0x73, 0x31, 0x6d, 0x78, 0x3e, 0x6e };

const byte buttonNo[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
const byte digitNo[] = {0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce};
const byte ledNo[] = {0x00, 0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf};
byte yesNoTXT[] = { letter[13], letter[14], letter[8], letter[11], 0x00, letter[16], 0x40, letter[7] };
byte serveTXT[] = { 0x00, letter[13], letter[9], letter[12], letter[15], letter[9], 0x00, 0x00 };

#define maxG11 11
#define maxG21 21
#define maxSet 5
#define minSet 3

const bool LEFT = true;
const bool RIGHT = false;
int gameMode = maxG11; // 11 or 21 point game
int p1Game = 0; // player 1 game score
int p2Game = 0; // player 2 game score
int p1Set = 0; // player 1 set score
int p2Set = 0; // player 2 set score
int serve = 0; // serve counter
bool serveSide = LEFT; // player to serve
bool serveStart = LEFT; // serve after set played
int serveChange = 2; // max serve
int serveChTemp = 1; // serve change after maxGx
bool gameON = false; // Play games
bool setON = false; // Play sets
bool score = false; // regular score variable
byte buttons = 0;

void setup() {
// Serial.begin(9600); // start serial for output
LK_setup(); // activate LED&KEY module
LK_reset();
}

void loop() {
p1Set = 0;
p2Set = 0;
kies11or21(); // choose game length 11 or 21
kiesServe();
setON = true;
do {
p1Game = 0;
p2Game = 0;
serve = 0;
serveChange = serveChTemp;
gameON = true;
do { // play game
LK_reset();
plotScore();
plotServe(1,serve);
score = false;
while (readButtons() != 0) {}; // wacht op loslaten knop
do { // play point
flashServe(serve+1);
buttons = readButtons();
if (buttons > 0) {
score = true;
if (buttons == buttonNo[1]) { p1Game++; serve++; }
if (buttons == buttonNo[2]) { p1Game--; serve--; }
if (buttons == buttonNo[7]) { p2Game--; serve--; }
if (buttons == buttonNo[8]) { p2Game++; serve++; }
if (buttons == buttonNo[4] || buttons == buttonNo[5]) {
plotTXT(yesNoTXT);
do {
buttons = chooseBut(); // buttonNo[6] = Yes = True = Quit
if (buttons == buttonNo[6]) {
gameON = false;
setON = false;
}
} while (buttons < buttonNo[6]);
}
if (p1Game < 0) p1Game = 0;
if (p2Game < 0) p2Game = 0;
if (serve < 0) {
serve = serveChange-1;
serveSide = !serveSide;
}
}
} while (score == false);
if (p1Game >= gameMode || p2Game >= gameMode) {
serveChange = 1;
serve = 0;
serveSide = !serveSide;
if (abs(p1Game-p2Game) > 1) {
gameON = false;
showWinner();
}
} else {
if (serve>0 && (serve % serveChange == 0)) {
serve = 0;
serveSide = !serveSide;
}
}
} while (gameON == true);

if (p1Game > p2Game) p1Set++;
else p2Set++;

if (p1Set >= minSet || p2Set >= minSet) {
setON = false;
showWinner();
}
else changeSide();
} while (setON == true);
}

void plotScore() {
LK_reset();
plotDigit(0,1,p1Game);
plotDigit(0,8,p2Game);
plotDigit(1,4,p1Set);
plotDigit(0,5,p2Set);
}

void kies11or21() {
byte side = 0;
LK_reset();
plotDigit(0,4,maxG11);
plotDigit(0,8,maxG21);
side = chooseBut();
if (side <= buttonNo[4]) {
gameMode = maxG11;
serveChange = 2;
} else {
gameMode = maxG21;
serveChange = 4;
}
serveChTemp = serveChange;
}

void kiesServe() {
byte but = 0;
plotTXT(serveTXT);
while (readButtons() != 0) {}; // wacht op loslaten knop
do {
but = readButtons();
setLed(1,1);
setLed(0,8);
delay(80);
if (but > 0) {
if (but <= 16) serveSide = LEFT;
else serveSide = RIGHT;
}
setLed(0,1);
setLed(1,8);
delay(80);
}
while (but == 0);
serveStart = serveSide;
}

void changeSide() {
int p1Temp;
// serveStart = !serveStart; // changed sides after set win
serveSide = serveStart;
serveChange = serveChTemp;
p1Temp = p1Set;
p1Set = p2Set;
p2Set = p1Temp;
}

void showWinner() {
byte pos;
clearLeds();
plotScore();
if (setON) { // set win
if (p1Game > p2Game) {
pos = 0;
flashScore(0,pos, p1Game);
} else {
pos = 6;
flashScore(0,pos, p2Game);
}
} else { // match win
if (p1Set > p2Set) {
pos = 3;
flashScore(0,pos, p1Set);
} else {
pos = 4;
flashScore(0,pos, p2Set);
}
}
}

Controlling Game

Controlling keys:

  1. Choose game mode 11 or 21 points to win (choose keys 1-4 or 5-8)
  2. Choose which player starts to serve (blinking leds, choose keys 1-4 or 5-8)
  3. Playing game:
  4. key 1 and 8 - increment points (also automatic increment serve)
  5. key 2 and 7 - decrement points (also automatic decrement serve)
  6. key 4 and 5 - stop game, asking for Yes (key 6) or No (key 7+8)

The blinking led is the side (person) who has to serve.

Setpoints are showed in the inner and game points on the outer side of the LCD-display

After a game won players has to switch side, the points also switch side

so the points are always at the right side of the players.