Do you know how to create the treasure hunt game? This game is very fun and I will teach you how to create this game with Arduino.

Are we going on a treasure hunt with the Arduino?

For this project, we will have a 64 position square map. This map will be represented by an 8x8 LED matrix. Through this map, the Arduino will choose a random position to hide the treasure.

### In this project, you'll learn:

• How to configure and use the 8x8 LED Array with Arduino;
• How to use the randomSeed function with Arduino;
• How to scan the position in the 8x8 matrix with the Arduino;
• Produce, download, and earn your own NEXTPCB printed circuit boards with Arduino.

Now, we'll understand how to develop this game with Arduino.

### How does this game work with Arduino?

This game can be played with one or several participants. Each participant has an attempt to find the position in which the treasure is hidden.

Initially, when turning on the system, the Arduino will randomly choose a position to hide the treasure. Then, each player must use the buttons shown in the Figure below.

Figure 1 - Circuit of thetreasure hunt game with Arduino.

In this game, there are 3 buttons. One button will be used to scroll through the lines, the second will be used to scroll through the columns and the third will be used to select the position desired by the user.

After the user selects the position, the system will check if the chosen coordinate is the same as the coordinate where the treasure is hidden.

If it is wrong, the system will have a circle on the screen.

Otherwise, the system will display a circle to indicate that the user was correct.

The two videos above show how the hunt treasure game with the Arduino project works.

Now, we'll show how works the code and offer a printed circuit board for you to construct your own project, too.

### Development of Project Programming Logic with Arduino

The code developed for this project is presented below.

```#include <SPI.h>
#include <Max72xxPanel.h>

#define BOTAO_LINHA  2
#define BOTAO_COLUNA 3
#define BOTAO_SELECIONA 4

#define PINO_RANDOM A1

#define MAX7219_CS  10 // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
#define QTD_MATRIZES_HORIZONTAIS 1
#define QTD_MATRIZES_VERTICAIS 1

Max72xxPanel matriz = Max72xxPanel(MAX7219_CS, QTD_MATRIZES_HORIZONTAIS, QTD_MATRIZES_VERTICAIS);

int linhaAtual, colunaAtual;

int linhaAnterior, colunaAnterior;

bool flagAcertou = false;

void setup() {
matriz.setIntensity(4); // Set brightness between 0 and 15
limparMatriz();

pinMode(BOTAO_COLUNA, INPUT);
pinMode(BOTAO_LINHA, INPUT);
pinMode(BOTAO_SELECIONA, INPUT);

randomSeed(analogRead(PINO_RANDOM)); // Initialize random generator

Serial.begin(115200);
Serial.println("Jogo_Matriz_8x8");

inicializarJogo();

}

void loop() {
while (digitalRead(BOTAO_SELECIONA) == LOW)
{

matriz.drawPixel(linhaAtual, colunaAtual, HIGH);
matriz.write(); // Send bitmap to display

if (digitalRead(BOTAO_LINHA) == HIGH)
{
linhaAtual++;

if (linhaAtual > 7)
{
linhaAtual = 0;
}

delay(200);
}

if (digitalRead(BOTAO_COLUNA) == HIGH)
{
colunaAtual++;

if (colunaAtual > 7)
{
colunaAtual = 0;
}

delay(200);
}

if ((linhaAtual != linhaAnterior) || (colunaAtual != colunaAnterior))
{
matriz.drawPixel(linhaAnterior, colunaAnterior, LOW); // Erase the old position of our dot

linhaAnterior = linhaAtual;
colunaAnterior = colunaAtual;
}
}

limparMatriz();

if ((linhaAtual == linhaSorteada) && (colunaAtual == colunaSorteada))
{
matriz.drawCircle(4, 4, 3, HIGH);

flagAcertou = true;

Serial.println("Acertou!");

}
else
{
matriz.drawLine(0, 0, 6, 6, HIGH);

matriz.drawLine(6, 0, 0, 6, HIGH);

Serial.println("Errou!");
}

matriz.write(); // Send bitmap to display

delay(3000);

limparMatriz();

if (flagAcertou)
inicializarJogo();
}
...```