3.1.13 GAME– NotNot

Introduction

In this project, we will make an interesting game device, and we call it “Not Not”.

During the game, the dot matrix will refresh an arrow randomly. What you need to do is to press the button in the opposite direction of the arrow within a limited time. If the time is up, or if the button in the same direction as the arrow is pressed, you are out.

This game can really practice your reverse thinking, and now shall we have a try?

Schematic Diagram

T-Board Name

physical

wiringPi

BCM

GPIO22

Pin 15

3

22

GPIO23

Pin 16

4

23

SPIMOSI

Pin 19

12

MOSI

SPICE0

pin 24

10

CE0

SPISCLK

Pin 23

14

SCLK

../_images/Schematic_notnot.png

Experimental Procedures

Step 1: Build the circuit.

../_images/3.1.14game_notnot.png

Note

Turn on the SPI before starting the experiment, refer to SPI Configuration for details. And the BCM2835 library is also needed.

Step 2: Go to the folder of the code.

cd /home/pi/raphael-kit/c/3.1.13/

Step 3: Compile the code.

make

Step 4: Run the executable file.

sudo ./3.1.13_GAME_NotNot

After the program starts, a left or right arrow will be refreshed at random on the dot matrix. What you need to do is to press the button in the opposite direction of the arrow, then “√” appears on the dot matrix. If the button in the same direction as the arrow is pressed, you are out and the dot matrix displays “x”. You can also add 2 new buttons or replace them with Joystick keys for up, down, left and right— 4 directions to increase the difficulty of the game.

Note

If it does not work after running, or there is an error prompt: “wiringPi.h: No such file or directory”, please refer to C code is not working?.

Code Explanation

Based on 1.1.6 LED Dot Matrix Module, this project adds 2 buttons to make an amusing game device.

The whole program process is as below:

  1. Use system time to generate a random 0 or 1.

  2. Display a random left or right arrow pattern.

  3. Press the key and determine the result.

  4. Display the right or wrong pattern.

../_images/3.1.14_notnot1.png
int get_index()
{
    srand((unsigned)time(NULL));
    return rand()%2;
}

The seed of the system is changed by the system time, i.e. srand((unsigned)time(NULL)), so that each time the rand function is called the value obtained is completely random, and finally the result obtained is divided by 2, so that the values obtained are 0 and 1.

int get_key(uint num)
{
    while (1)
    {
        if (1 == bcm2835_gpio_lev(AButtonPin) && num == 0){
            return 1;
        }
        else if (1 == bcm2835_gpio_lev(BButtonPin) && num == 1){
            return 1;
        }
        else if (1 == bcm2835_gpio_lev(AButtonPin) && num == 1){
            return 0;
        }
        else if (1 == bcm2835_gpio_lev(BButtonPin) && num == 0){
            return 0;
        }
    }
}

Determines which button was pressed and compares it to the direction of the arrow on the dot matrix and gives the final result of 0 or 1.

../_images/3.1.14_getkey.png
void display(uint index){
    uchar i;
    if (stage == 0){
        for(i=1;i<9;i++)
        {
            Write_Max7219(i,arrow[index][i-1]);
        }
    }
    else if(stage == 1){
        for(i=1;i<9;i++)
        {
            Write_Max7219(i,check[index][i-1]);
        }
    }
}

Depending on the value of the stage and index to display the left or right arrow or the right or wrong pattern.

../_images/3.1.14_display.png