Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
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?
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
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 |
Experimental Procedures¶
Step 1: Build the circuit.
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 ~/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 Install and Check the WiringPi.
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:
Use system time to generate a random 0 or 1.
Display a random left or right arrow pattern.
Press the key and determine the result.
Display the right or wrong pattern.
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.
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.