ScoreCounter (soccer Table)

by Cy-jay in Circuits > Arduino

2031 Views, 15 Favorites, 0 Comments

ScoreCounter (soccer Table)

ppostn.jpg
BOM.png

My family plays a small tournament on the soccer table every Sunday. And now and then it happens that there is a discussion about the score. Someone says that they forgot to move their score up 1 point after a goal,...

so to avoid this kind of discussion (and to give the soccer table an upgrade) I want to make an automatic score counter, and by extension, a soccer table that measures how hard your shot is at a goal.

In this instructable, I'm going to share how I made this project, step by step

Supplies

Building the Circuit

schakeling.png

The first thing i did is building the circuit. As you can see in the picture i use both the 5V and 3.3V.

let's go through each component step by step:

  • IR Break Beam sensors: All 4 of them are connected to the 5V and Ground pins, only the 2 receivers have an extra cable, and that extra cable we put in port 4 and 5 of the Arduino.
  • RFID Reader:
    1. VCC is connected to 3.3V
    2. RST pin is connected to port 9
    3. GND pin is connected to the ground
    4. IRQ is connected to NOTHING
    5. MISO is connected to port 12
    6. MOSI is connected to port 11
    7. SCK is connected to port 13
    8. SS is connected to port 10
  • 2 Lasers: The ground pins are connected to the ground, the VCC pins are connected to port 6 and 7 of the arduino
  • Buzzer: the VCC pin is connected to port 2 of the Arduino with a resistor of 100 Ohm, the ground pin is connected to the ground
  • 2 LDR's: one side of the LDR's is connected to the 5V with an 10K Ohm resistor. Between the resistors and the LDR's you have a cable that is connected to A0 and A1 to the arduino (to read the value of the LDR's).
  • For the 3,5 inch screen: All the pins that are connected, are connected to the corresponding name on the Rpi.

At last we have the USB cable that connects the arduino and Raspberry Pi, we are going to use this to send data from the Arduino to the Pi. You can skip that part for now because we will need this USB cable first to upload our code to the Arduino via the computer.

Placing the Circuit in the Soccer Table

LDRMET.jpg
onderkant.jpg
scorebord.jpg
laser.jpg
LDRZONDER.jpg

The placement of all the components is pretty specific. The IR Break Beam sensor must be placed inside the goals, make sure you place it where it does not obstruct the ball. I made 2 little holes right underneath each sender and receiver of the IR Break Beam sensors, that way the cables can not obstruct the ball.

At one side of the table i made 2 bigger holes for the lasers, for the placement of the lasers there are 2 important things. The first one is to make sure you place it where it is not possible for the football players to interrupt the laser. The second one is to place it where the ball will always pass the laser if the attacker shoots the ball (that is between the attackers from the 1st player and the goalkeeper of the 2nd player).

The LDR's must be placed straight across the lasers (the laser must shine on the LDR). To do this perfectly you can put on the lasers and then make a hole in the middle of the light of the lasers.

I have put the RFID on the front of the table, but the placement of that component doesn't matter

On the back of the table I fixed a shelf so the raspberry pi can hang on 2 screws to display the score, I recommend to put heat shrink tubing on the 2 screws where the Pi is hanging on

In the middle under the table I made a little table with 3 planks where the Arduino and the breadboard lies on top, that way it is always easy to fix something if something gets broken.

The USB cable needs to connect the Arduino with the Raspberry Pi, i made a hole underneath and on top op the table cause my cable was to short, it is possible to just connect the Arduino with the Pi without making any more holes in the table.

Code for Arduino

To implement this code we first need to add a library to use the RFID reader. Search in Arduino IDE for the "MFRC522" library (by githubCommunity!).

If you don't know how to add an library, here is a link where it is explained how to do it: https://www.arduino.cc/en/guide/libraries

At first we start with including the libraries SPI (installed by default) and MFRC522. Once we done that we go over to the declarations of all the pins on the Arduino and all the variables we are going to use in the code. In the setup() we are setting up the pins we declared earlier.

The first thing we are going to do at the loop() function is checking wether the Raspberry Pi send us something (The Pi will send to the Arduino when a match is started and if they changed their score manually, more about this later!).

After that we are going to read our LDR's and IR Break Beam sensors. With the lasers pointed at the LDR's the value of it will always be below 25 so when the ball interrupt the laser pointing to the LDR the value will be above 100, and that where our IF statement is going to work. When the value is above 100 the Arduino will save the time that has passed since the millis() is counting. The IR Break Beam sensor will be 1 (HIGH) when nothing is interrupting the infrared beam from the sender to receiver. When the ball interrupts this beam the sensors value will be 0 (LOW) and when this happens the Arduino will add 1 point to the player who scored and will save a second timestamp from the millis(). The 2 timestamps (LDR and IR sensors) will be used to calculate the speed of the shot that has been scored with. We will print the score of the 2 players and the speed of the shot so the Raspberry Pi can receive these values via serial communication.

When that code is passed we will check wether one of the players scores are 10 or higher. When this is true we are going to watch if the difference between the 2 players their score is 2 or higher. If the difference isn't 2 or higher (example: 13-12) then the game will continue. If the difference is 2 or higher (example: 10-8) it means the player with the highest score won and the buzzer will play a little sound.

At last we are going to check whether the user is scanning a card with the RFID reader, if so, 2 things can happen. First option is: the card the user scanned doesn't have acces. The second option is: the card the user scanned does have acces, and then the Arduino will send that to the Raspberry Pi. All options have a corresponding sound on the buzzer.

All the voids are sounds i created for the buzzer.

Getting the Pi Ready

databank.png

To be able to work with your Raspberry Pi you need to download an image on an SD card that you can insert in your pi later. How to do it can be found on this link: https://www.raspberrypi.org/documentation/installa...

if that is done follow this:

# Getting things ready Raspberry Pi
---

## Configuraton 😵💫

### ⚠️**Wifi access** provided on the RasPi for home use:

- `sudo -i` to get administrator rights - `wpa_passphrase >> /etc/wpa_supplicant/wpa_supplicant.conf` Here, replace `your_SSID@Home> with the name of your home network and `your_wifi password> with the corresponding password.

- `wpa_cli -i wlan0 reconfigure` to reload your wireless network card in the RasPi. - `wget www.google.com` to see if the wireless internet works.

### **Full update**/ if you want to upgrade, please follow the list below:

- `apt update` to check what updates are available. (you still have sudo rights from the previous step, so don't need to put `sudo`) - `apt upgrade` to install the available updates. - `Y` if you are asked if you are sure. - Wait, wait, wait, ...

### install **Apache**

- `apt install apache2 -y` to install Apache, the web server - Since we are working with Github in this exercise we will make it easy for ourselves by putting all the material in one folder, both frontend and backend. To do this we will need to change Apache's default folder, along with folder and file permissions, but we will not do this until we have created our folder structure.

#### Install **MariaDB**

- `apt install mariadb-server mariadb-client -y` to install MariaDB, the fork of MySQL

#### Secure **MariaDB**

- `mysql_secure_installation` to better secure the MariaDB - First you are asked to enter the current root password for MariaDB. Since there is none yet you can just press _Enter_ here. - Next you can change the password. Choose a password that you **certainly** can remember!. - The next step is to remove anonymous users. Choose `y` here. - Prohibit root from logging in remotely. Choose `y` here. - Next remove test database and access? Choose `y`. - Finally reload privilege databases: `y`.

#### Create **MariaDB User**

- Next we configure the user _Your-Username_ with password _Your-Password_ on the MariaDB server - `mysql -u root -p` to access the MariaDB server - `grant all on *.* to 'Your-Username'@'localhost' identified by 'Your-Password'; grant option on *.* to 'Your-Username'@'localhost';` Creates a new user _Your-Username_ with password _Your-Password_ that is granted rights to all databases. - `flush privileges` Reload the privileges - Exit` exits the MariaDB server.

### ⚠️ Configure **MySQLWorkbench**

- Start MySQLWorkBench on your laptop. - Create a new connection. - At Connection Method, choose Standard TCP/IP over SSH. - SSH Hostname: `192.168.168`. - SSH Username: `Your-Username` - SSH Password: `Your-Password`. Save this if possible. - MySQL Hostname: `127.0.0.1`. - MySQL Server Port: `3306`. - Username: `Your-Username` - Password: `Your-Password` Save this if possible.

### ⚠️ Configure **Visual Studio**

- Open Visual Studio

- Install the extension _Remote-SSH_.

- Press F1 and tap SSH. Select the option _Remote-SSH: Add New SSH Host_.

- Type `ssh Your-Username@Your-RPI-IP-Address -A` - Choose an option to save the file. - Press F1 and tap SSH. Choose the option _Remote-SSH: Connect To Host_. - A new window will open and the password for the RasPi will be requested.

- Type `Your-Password`. - After this, Visual Studio Code connection will open and install some things on the RasPi. > Be patient. It takes a little longer the first time.

### ⚠️ Clone **GitHub** repo

- Press the _Source Control_ logo on the left and choose Clone Repository.

- In a browser, go to the GitHub Link https://github.com/SomersCy-Jay/scoreCounter Click the _Code_ button and copy the git link.

- Paste the copied link into Visual Studio Code and hit enter.

- Place the repo in the `/home/Your-Username/` folder. - Visual Studio Code will then ask to open this repo, click _Yes_ - After that, open the Code/Backend/app.py file and give Visual Studio Code a moment to load all the necessary things.

- In the folder "Code" there is another folder "Database-export" copy all the code inside the "voetbaltafel.sql" file and paste it in your Database to create the database.

### Getting Python ready

- Before we can run the app.py we need to install some packages for python. - Open a Terminal and type the following code:

- `pip3 install flask-cors`

- `pip3 install flask-socketio`

- `pip3 install mysql-connector-python`

- `pip3 install gevent`

- `pip3 install gevent-websocket`

### Import **Database**

- Import the database into MariaDB via MySQLWorkbench.

### ⚠️ Run app.py

- Try running _app.py_. If you can't press Play (the green triangle) in the window, look in the extensions at the Python extension and click _install on _ and wait. Reload if prompted. - If all went well the backend should now be running.

### ⚠️ Show frontend in Apache.

- On your PC, surf to http://Your-PI-IP-Address. - Normally you should see the _Apache2 Debian Default Page_, this is the default page for Apache currently in the _/var/www/html/_ folder on the RasPi. We will not use this default folder, but will use the Frontend folder from the repo you just cloned.

- If you are no longer sudo: `sudo -i` - `nano /etc/apache2/sites-available/000-default.conf`. - Use down arrow to go to line where it says _DocumentRoot /var/www/html_ or `DocumentRoot /home/student//Code/Frontend` and change it to `DocumentRoot /home/student//Code/Frontend`. - Save by doing _Ctrl + x_, followed by `Y` and _Enter_. - After that we restart Apache by doing `service apache2 restart`. - Now we have to set the permissions on the root folder correctly.

- Open `nano /etc/apache2/apache2.conf` and use the down arrow to look for following lines:

_{Directory />}

Options FollowSymLinks

AllowOverride All

Require all denied

\_.

and change it to:

_Directory>_.

Options Indexes FollowSymLinks Includes ExecCGI

AllowOverride All

Require all granted.

\_.

- Saving is done by doing _Ctrl + x_, followed by `Y` and _Enter_. - Then we restart Apache by doing `service apache2 restart`.

### ⚠️ automatic startup.

- Create a file named _myproject.service_. - Place the following code in the file:

`[Unit]`

`Description=ProjectOne Project`.

`After=network.target`

`[Service]`

`ExecStart=/usr/bin/python3 -u /home/student//Code/Backend/app.py` `WorkingDirectory=/home/student//Code/Backend/`

`StandardOutput=inherit`

`StandardError=inherit`

Restart=always`

`User=Your-Username`

`[Install]`

`WantedBy=multi-user.target`

- Copy this file as root user to _/etc/systemd/system_ with the command `sudo cp myproject.service /etc/systemd/system/myproject.service`.

- Now you can test the file by starting it: `sudo systemctl start myproject.service` - Stopping the file can be done with by using the command: `sudo systemctl stop myproject.service` - If all works well, you can have the script start automatically after booting: `sudo systemctl enable myproject.service`

RasPi Code Small Explanation

I'm going to start with the backend code. First we have the Database.py, with this file we connect with our database and we are able to do the CRUD actions (Create, Read, Update, Delete).

Datarepository.py is for all the sql statements we need to get our data that we are displaying on the website (all the players, the played matches,..). Here we use the class Database that we made in Database.py.

Then we got the App.py, here we create our application with Flask and socketio. It is also the place where we read and send information from and to the Arduino, this happens in a thread. All the app.route() functions is for getting the information out of our database with the help of the class Datarepository (from Datarepository.py). They will ask the information and send it to the frontend in a json format.

Socketio is a bit the same as app.route but with socketio we can send realtime information to the frontend. It means that you don't need to refresh the page to get the new values. The main use of this is to get the current score of the match.

Then for the frontend code. vapp.js is the place where you listen to all the clicks, display the information on the site, display pop-ups to show information to the user,..

All the rest in the frontend is html and css. I think you guys know what it does, so i will skip the information here.

LCD Touchscreen

scorebord.jpg

If all the steps above are done the only thing that is left is to set up the LCD-screen. To do type the following commands in your terminal:

- sudo rm -rf LCD-show

- git clone https://github.com/goodtft/LCD-show.git

- chmod -R 755 LCD-show

- cd LCD-show/

- sudo ./LCD35-show

Make sure you connect your screen correctly! Otherwise it can damage the touch function of your touchscreen.

Enjoy the Project!

If evrything is done the site for playing matches should automatically display on your RasPi screen. The IP-address at the bottom of the home page is the address you need to type to go to the site via your computer/cellphone (this only works if you are on the same WiFi network). The site will be bigger on your computer and cellphone. The screen on the Pi is only for displaying the score of the current match. Anywhere else you can add and delete players, look at matches and delete them and watch all the matches played between 2 specific players.

Enjoy!