Smart 4 in a Row Game

by asterchys in Circuits > Raspberry Pi

277 Views, 0 Favorites, 0 Comments

Smart 4 in a Row Game

kabelsWeggewerkt.jpg

I am a leader of scouts in Moen Belgium and I wanted to make a game for my kids. After some brainstorming I had an idea; a game based on four in a row that gives small tasks to earn a disc of the four in a row. If you also want to give the members of your youth movement a fun afternoon, read on to find out how!

Supplies

In the file above you get a list with al the electronics I used for this project(everything with a link). You can also find a file with a list of the materials and tools I used.

Building the Circuit

electronics_bb1024_1.jpg

The first thing you need to do before you start programming your electronics is building the circuit.

Let's look at each component separately.

  • IR sensors: There are 7 of them. All 7 are connected to the Ground pin of the Raspberry Pi and the 5V pin. Then you have to connect each data pin to a GPIO pin of the Raspberry Pi. Which pin doesn't really matter, you can easily chance the pins in the code. Choose the left over pins.
  • Arduino: You connect the Arduino to the Raspberry Pi with a level shifter. The low voltage(lv) side is for the Pi and the high voltage(hv) is for the Arduino. Lv is for the 3v3 of the Pi, the gnd next to lv is for the ground of the PI. Lv1 is for the tx of the Pi and lv2 for the rx. Hv is for the 5V of the Arduino and the gnd next to the hv is for the gnd of the Arduino. Hv1 is for the rx of the Arduino and hv2 for the tx. They need to be crossed.
  • RFID: The RFID works on 3v3, connect the vcc pin to the 3v3 from the arduino. The rst pin to D9, gnd to the ground, miso to D10, Mosi to D11, SCK to D12 and SDA to D13.
  • Buzzer: There are 3 buzzers in this project, a buzzer is easy to connect. One side is for the gnd and the other side is for a GPIO pin. You can choose wich GPIO pins you use, just chance them easily in the code.
  • 8x8 led matrix: There are 2 led matrixen, they use both a MAX7219 chip. Connect the both VCC to the 5v of the PI, the gnd to the gnd , DIN to the MOSI, CLK to the clk and one cs to cs0 and the other to cs1.
  • One-Wire Temperature Sensor: This is a little bit of a difficult one. This one needs a 10K resistor that you put between the data pin and the vcc. Then you can connect the data pin to GPIO, the vcc to 5v and the gnd to the gnd of the Pi.
  • LCD: I used a 4 bit connection for the LCD because I don't have enough GPIO pins. I think it's clear enough on the picture how to connect it. The used resistor is a 10k one. It's al powered with 5V.

Create a Place to Put Away the Circuit

IMG_6605.jpg
IMG_6612.jpg
IMG_6614.jpg
IMG_6620.jpg
IMG_6625.jpg
IMG_6618.jpg
IMG_6628.jpg
IMG_6629.jpg
IMG_6630.jpg
IMG_6631.jpg
IMG_6666.jpg

In this step I will explain how I put everything away.

I had a big 4 in a row to use but you can also make one yourself.

  1. I disassembled the 4 in a row frame
  2. With the multitool I sawed out the pieces that were in the way. Now you can see there is room to put in the ir sensors.
  3. I made my own cables with the connectors en cables in the materials and tools.pdf file.
  4. I connect the cables with the ir sensors and put tham in the 4 in a row.
  5. With a sort of gum for sticking pictures to the wall i made sure they wont move.
  6. I drilled a hole in the legs of the 4 in a row so I can lead the cables true it.
  7. Now you have to assemble the 4 in a row and done with that.

For the rest of the components you can make whatever you want. I made a stond where I can put my laptop on but this is not necessary. Let your imagination run wild.

Setup the Raspberry Pi

First of al download the image from the link.

Once it is downloaded you can write your sd-card with a program like balenaEtcher or win32 disk imager.

Now we can start with changing the image so we can use it.

  1. Put the sd-card in the Raspberry Pi and connect the Pi to power.
  2. Connect the Pi to your pc with a netwerk cable. Make an SSH-connection to 192.168.168.169 with Putty or your basic cmd interface. The user is student and the password is W8w00rd.
  3. Now your logged in go to 'sudo raspi-config' choose (6)Advanced > (1)Expand Filestystem and reboot the Pi.
  4. Make sure your Pi has wifi acces
  5. get administrator rights with 'sudo -i'
  6. wpa_passphrase 'your wifi name' 'wifi passowrd' >> /etc/wpa_supplicant/wpa_supplicant.conf
  7. wpa_cli -i wlan0
  8. wget www.google.com (to test)
  9. Make sure you have all the latest updates with:
  10. apt update (look if there are updates available)
  11. apt upgrade
  12. Now we gonna configure MySQLWorkbench
  13. Start MySQLWorkbench
  14. Make a new connection with Connection Method: Stadarc TCP/IP over SSH
  15. ssh hostname: 192.168.168.169
  16. ssh username: student
  17. ssh password: W8w00rd (save if possible)
  18. MySQL hostname: 127.0.0.1
  19. MySQL server port: 3306
  20. Username: student
  21. Password: W8w00rd
  22. Visual Studio Configuration
  23. Open visual studio code
  24. Install the Remote-SSH extension
  25. Press F1 and search SSH, choose for 'Remote-SSH: Add New SSH Host'
  26. ssh student@192.168.168.169
  27. Choose where to save the file
  28. press F1 again and search SSH. choose for 'Remote-SSH: Connect To Host'
  29. Choose the option 192.168.168.169
  30. There will be a new window and you need to give the password (W8w00rd)
  31. Visual Studio will open your connection
  32. Clone the GitHub repo
  33. Press the Source Control logo and choose for Clone Repository
  34. Paste the link from the repo
  35. place in the '/home/student/' folder
  36. Make sure Apache shows your front end
  37. sudo -i
  38. nano /etc/apache2/sites-available/000-default.conf
  39. change DocumentRoot /var/www/html or DocumentRoot /home/student/.../front-end to DocumentRoot /home/student/2021-2022-projectone-ChysAster/front-end
  40. save with Ctl + x , Y and enter
  41. restrat apache with: service apache2 restart
  42. Now the rights one the root folder have to be right
  43. nano /etc/apache2/apache2.conf , go down to <Directory/> Optinas FollowSymLinks ... </Directory>
  44. chance to <Directory/> Options Indexes FollowSymLinks Includes ExecCGI (enter) AllowOverride All (enter) Require all granted (enter) </Directory>
  45. save with ctrl + x, y and enter
  46. Restart apache with 'service apache2 restart'

Now your Rasperry Pi is ready to use!

The Code

opdrachtflow.drawio.png

The schema above shows simply how the code works.

Setup Arduino Code

Start your Arduino IDE and paste the code below:

#include <AddicoreRFID.h>
#include <SPI.h>

#define uchar unsigned char
#define uint unsigned int

uchar fifobytes;
uchar fifoValue;

AddicoreRFID myRFID;

const int chipSelectPin = 10;
const int NRSTPD = 5;

void setup() {
 Serial.begin(9600);

 SPI.begin();
  
 pinMode(chipSelectPin,OUTPUT);        
  digitalWrite(chipSelectPin, LOW);  
 pinMode(NRSTPD,OUTPUT);           
  digitalWrite(NRSTPD, HIGH);

 myRFID.AddicoreRFID_Init();  
}
void loop() {
// Serial.println("Hello from Arduino!");
// delay(1000);

 uchar i, tmp, checksum1;
 uchar status;
    uchar str[MAX_LEN];
    uchar RC_size;
    uchar blockAddr;  
    String mynum = "";

    str[1] = 0x4400;
 status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str); 
 if (status == MI_OK)
 {
//     Serial.println("RFID tag detected");
 }

 status = myRFID.AddicoreRFID_Anticoll(str);
 if (status == MI_OK)
 {
//     Serial.print("Nummer:\t");
//     Serial.println(str[0]);

      if(str[0] == 188)            
      {
        Serial.print(str[0]);
      } else if(str[0] == 129) {       
        Serial.print(str[0]);
      }
      Serial.println();
      delay(1000);
 }
   
    myRFID.AddicoreRFID_Halt();        

}

Now just push the code to the Arduino and you are done. Make sure the tx and rx pins are not connected


Database

Screenshot 2022-06-19 at 22.50.07.png

For the Database we use MySQLWorkbench, we already did this setup in step 3.

In the photo above you can see the schema of the needed database. If you have questions about this just leave a comment and I will help you.

Enjoy This Game!

Now we have done the complete setup. All you have to do is start the app.py script in the code and browse to the ip-adress given on the LCD and your done.You can see there are different other tabs on the site, I think the nicest thing to do is to find them out yourself. I hope you guys enjoy this!