Lima Moreira
Published © GPL3+

Treasure hunt Game with Arduino

You will learn how to build your treasure hunt game with Arduino.

BeginnerFull instructions provided2 hours1,421
Treasure hunt Game with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED Dot Matrix Display, Red
LED Dot Matrix Display, Red
×1
Breadboard (generic)
Breadboard (generic)
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Electronic Schematic of the Project

Code

Code of the game

Arduino
#include <SPI.h>
#include <Adafruit_GFX.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 linhaSorteada, colunaSorteada;

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();
}

void inicializarJogo(void)
{
	flagAcertou = false;

	Serial.println("Sorteando...");

	linhaSorteada = random(8);
	colunaSorteada = random(8);

	Serial.print("Linha Sorteada: ");
	Serial.println(linhaSorteada);
	Serial.print("Coluna Sorteada: ");
	Serial.println(colunaSorteada);

	linhaAtual = 0;
	colunaAtual = 0;

	linhaAnterior = linhaAtual;
	colunaAnterior = colunaAtual;
}

void limparMatriz(void)
{
	for (int i = 0; i < 8; i++) //limpa a matriz
	{
		for (int j = 0; j < 8; j++)
		{
			matriz.drawPixel(i, j, LOW); // Erase the old position of our dot
		}
	}

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

Credits

Lima Moreira

Lima Moreira

12 projects • 8 followers

Comments