/*
Project: Tennis Scoreboard Simmple
Description: Use to keep score in a tennis match
In a real circuit add 330 ohm resistors on each segment
and transistors on each digit! (Wokwi doesn't care...)
The gray wires are digits, colored are the segments.
Creation date: 5/17/23
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
const int DIGIT_PINS[4] = {A0, A1, A2, A3}; // digits 0-3
const int SEG_PINS[7] = {4, 3, 5, 7, 6, 2, 8}; // segments A-G
const int NUM_SEGS[10][7] = { // segment lookup table
{1, 1, 1, 1, 1, 1, 0}, /* 0 */
{0, 1, 1, 0, 0, 0, 0}, /* 1 */
{1, 1, 0, 1, 1, 0, 1}, /* 2 */
{1, 1, 1, 1, 0, 0, 1}, /* 3 */
{0, 1, 1, 0, 0, 1, 1}, /* 4 */
{1, 0, 1, 1, 0, 1, 1}, /* 5 */
{1, 0, 1, 1, 1, 1, 1}, /* 6 */
{1, 1, 1, 0, 0, 0, 0}, /* 7 */
{1, 1, 1, 1, 1, 1, 1}, /* 8 */
{1, 1, 1, 1, 0, 1, 1} /* 9 */
};
const int SCORE_VALS[] = {0, 15, 30, 40, 41};
int player1Score = 0;
int player2Score = 0;
void checkButtons() {
static int lastPlayer1State = 1;
static int lastPlayer2State = 1;
// read the pushbutton input pins:
int player1State = digitalRead(10);
int player2State = digitalRead(9);
// Player 1 button code (with comments)
// compare the buttonState to its previous state
if (player1State != lastPlayer1State) {
// if the state has changed, show the change
if (player1State == LOW) {
// if the current state is LOW then the button went from off to on:
player1Score++;
if (player1Score > 4) player1Score = 0;
Serial.print("Player 1 score is now ");
Serial.println(SCORE_VALS[player1Score]);
} else {
// if the current state is HIGH then the button went from on to off:
Serial.println("Player 1 button released");
Serial.println();
}
// Delay a little bit to avoid bouncing
delay(20);
}
// save the current state as the last state, for next time through the loop
lastPlayer1State = player1State;
// Player 2 button code (same comments as above)
if (player2State != lastPlayer2State) {
if (player2State == LOW) {
player2Score++;
if (player2Score > 4) player2Score = 0;
Serial.print("Player 2 score is now ");
Serial.println(SCORE_VALS[player2Score]);
} else {
Serial.println("Player 2 button released");
Serial.println();
}
delay(20);
}
lastPlayer2State = player2State;
}
void updatePoints() {
int pointHi[2] = {0, 0};
int pointLo[2] = {0, 0};
pointHi[0] = (SCORE_VALS[player1Score] % 100) / 10; // tens
pointLo[0] = SCORE_VALS[player1Score] % 10; // ones
pointHi[1] = (SCORE_VALS[player2Score] % 100) / 10; // tens
pointLo[1] = SCORE_VALS[player2Score] % 10; // ones
writeDigit(0, pointHi[0]);
writeDigit(1, pointLo[0]);
writeDigit(2, pointHi[1]);
writeDigit(3, pointLo[1]);
}
void writeDigit(int digit, int number) {
// set up the segments
for (int segment = 0; segment < 7; segment++) {
digitalWrite(SEG_PINS[segment], NUM_SEGS[number][segment]);
}
// turn digit on
digitalWrite(DIGIT_PINS[digit], LOW);
// turn digit off
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
Serial.begin(9600);
// set up all the pins
for (int segs = 2; segs <= 8; segs++) {
pinMode(segs, OUTPUT);
}
for (int digs = A0; digs <= A3; digs++) {
pinMode(digs, OUTPUT);
}
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
}
void loop() {
checkButtons();
updatePoints();
}