
Tennis game counter
​​
​​
Just a fun small project.
​
​​
1. General description
This device consists of an Arduino Nano, a 7 elements LED display a battery pack (6V), 2 push buttons and 2 resistors. ​​​​​​​​​​​​​​​​​​​​​​​​


2. Code
Probably not the most efficient code, but it works!
​​
[code]
​
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);
int buttonA = 4;
int buttonB = 5;
int A=0;
int B=0;
int pa[] = {00,15,30,40,50};
int pb[] = {00,15,30,40,50};
int gameA[] = {0,1,2,3,4,5,6,7};
int gameB[] = {0,1,2,3,4,5,6,7};
int scoreA = pa[0];
int scoreB = pb[0];
int indexGA = 0;
int indexGB = 0;
int indexA = 0;
int indexB = 0;
int actA = 0;
int actB = 0;
int buttonStateA; // Current state of the button
int lastButtonStateA = LOW; // Previous state, assuming pull-up resistor
int buttonStateB; // Current state of the button
int lastButtonStateB = LOW; // Previous state, assuming pull-up resistor
bool readyNext = true; // Prevent button pressed again
int next = 0;
int cs = 0;
void setup()
{
// Initialize the display
display.setBrightness(6);
Serial.begin(9600),
//Set the button pin as input using internal resistor
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
}
void loop() {
if (digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH) { //When both are buttons pressed, the games are displayed
String f = String(gameA[indexGA]) + String(gameB[indexGB]);
int fs = f.toInt();
display.showNumberDec(fs,true);
Serial.println(fs);
delay(3000);
}
// Reset when button is released
if (buttonStateA == LOW) {
readyNext = true;
}
lastButtonStateA = buttonStateA; // Update last state
// Reset when button is released
if (buttonStateB == LOW) {
readyNext = true; // Since button is HIGH again, the readynext variable allows the code to run, until the end when it becomes false
}
lastButtonStateB = buttonStateB; // Update last state
// Detect a press only if the button was released before
buttonStateA = digitalRead(buttonA);
buttonStateB = digitalRead(buttonB);
if (buttonStateA == HIGH && lastButtonStateA == LOW && readyNext) {
actA = 1;
}
if (buttonStateB == HIGH && lastButtonStateB == LOW && readyNext) {
actB = 1;
}
delay(100); // Con este nuevo codigo puedo bajar el delay a muy poco
//-----------------------------Cases------------------------
// Caso 1 ------------------------------------------------------
if ((actA == 1) && (scoreA <= 30)) {
indexA ++; // Incrementar el índice (pasar a la segunda posición)
scoreA = pa[indexA];
//Serial.println(scoreA);
actA = 0; // If the code runs through this if statement, the active button becomes 0 again so as not to activate the rest of the following conditions
}
if ((actB == 1) && (scoreB <= 30)) {
indexB ++;
scoreB = pb[indexB];
//Serial.println(scoreB);
actB = 0; // If the code runs through this if, then active button becomes 0 again so as not to activate the rest of the following conditions
}
// Caso 2 Winning Game ------------------------------------------------------
if ((actA == 1) && (scoreA == 40) && (scoreB != 40) && (scoreB != 50)) {
indexA = 0;
indexB = 0;
scoreA = pa[indexA];
scoreB = pb[indexB];
indexGA++;
gameA[indexGA]; // The game info is stored in varaible gameA, so I can access it later.
actA = 0;
}
if ((actB == 1) && (scoreB == 40) && (scoreA != 40) && (scoreA != 50)) {
indexA = 0;
indexB = 0;
scoreA = pa[indexA];
scoreB = pb[indexB];
indexGB++;
gameB[indexGB];
actB = 0;
}
// Caso 3, Ventajas Tipo 1 ------------------------------------------------------
if ((actA == 1) && (scoreA == 40) && (scoreB == 40)) {
indexA ++;
scoreA = pa[indexA]; //Se vuelve 50, porque la condicion para este caso es partir de scoreA = 40
actA = 0;
}
if ((actB == 1) && (scoreB == 40) && (scoreA == 40)) {
indexB ++;
scoreB = pb[indexB]; //Se vuelve 50, porque la condicion para este caso es partir de scoreB = 40
actB = 0;
}
// Caso 4, Ventajas Tipo 2 ------------------------------------------------------
if ((actB == 1) && (scoreA == 50) && (scoreB == 40)) {
indexA --;
scoreA = pa[indexA]; //Se vuelve 40, porque la condicion para este caso es partir de scoreA = 50
actB = 0;
}
if ((actA == 1) && (scoreB == 50) && (scoreA == 40)) {
indexB--;
scoreB = pb[indexB]; //Se vuelve 50, porque la condicion para este caso es partir de scoreB = 50
actA = 0;
}
// Caso 5, Final ------------------------------------------------------
if ((actA == 1) && (scoreA == 50) && (scoreB == 40)) {
indexA = 0;
indexB = 0;
scoreA = pa[indexA];
scoreB = pb[indexB];
indexGA++;
gameA[indexGA];
actA = 0;
}
if ((actB == 1) && (scoreB == 50) && (scoreA == 40)) {
indexA = 0;
indexB = 0;
scoreA = pa[indexA];
scoreB = pb[indexB];
indexGB++;
gameB[indexGB];
actB = 0;
}
// Cuando ya ha procesado todos los condicionantes, llega esta parte del codigo--------------------------
// Convert numbers to strings and concatenate
//Serial.println(scoreA);
//Serial.println(scoreB);
if (scoreA == 0) {
String c = String(scoreA) + "0" + String(scoreB);
int cs = c.toInt();
display.showNumberDec(cs,true);
}
if (scoreB == 0) {
String c = String(scoreA) + String(scoreB) + "0";
int cs = c.toInt();
display.showNumberDec(cs,true);
}
if ((scoreA != 0) && (scoreB !=0)) {
String c = String(scoreA) + String(scoreB);
int cs = c.toInt();
display.showNumberDec(cs,true);
}
//Serial.println(c);
// Serial.println(String(indexA) + " " + String(scoreA));
readyNext = false; // Block further presses until released
}
​
[/code]