#include <MD_MAX72xx.h>
// Numero de matrices LED conectadas
#define NUM_MATRICES 1 //En caso de que quiera hacer crecer la matriz,
//El json solo me permite cambiar las columans de la matriz pero no las filas
//El numero que defino debe estar cambiado en el Json
//Zonas sensibilidad joystick
#define ZONA_MUERTA 100
#define CENTRO 512
//Limites
#define LIMITE_INFERIOR (CENTRO - ZONA_MUERTA)
#define LIMITE_SUPERIOR (CENTRO + ZONA_MUERTA)
const int maxX = NUM_MATRICES * 8 - 1; //Qui se calcula el maximo de columnas en el eje X, el
const int maxY = 7; //Siempre seran 7 filas, en json no se me permite cambiarlas
//Pines Matrz
#define PIN_CLK 13
#define PIN_DATO 11
#define PIN_CS 10
//Joystick
#define PIN_VERT A0 //0-1025
#define PIN_HORZ A1
#define PIN_SEL 2
//Variable contador
int contador = 0;
MD_MAX72XX matriz = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, PIN_CS, NUM_MATRICES);
int x = 0, y = 0; // Punto del jugador
int puntoX = 5, puntoY = 5; // Punto objetivo aleatorio
const int DISTANCIA_MINIMA = 2; // Minima distancia entre jugador y punto
void setup() {
matriz.begin();
matriz.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2);
matriz.clear();
pinMode(PIN_VERT, INPUT);
pinMode(PIN_HORZ, INPUT);
pinMode(PIN_SEL, INPUT_PULLUP);
randomSeed(analogRead(A2)); // Semilla para generar numeros aleatorios
generarNuevoPunto(); // Crear primer punto aleatorio
}
void loop() {
int horz = analogRead(PIN_HORZ);
int vert = analogRead(PIN_VERT);
if (vert < LIMITE_INFERIOR) {
y = min(y + 1, maxY); // Abajo
}
if (vert > LIMITE_SUPERIOR) {
y = max(y - 1, 0); // Arriba
}
if (horz > LIMITE_SUPERIOR) {
x = min(x + 1, maxX); // Derecha
}
if (horz < LIMITE_INFERIOR) {
x = max(x - 1, 0); // Izquierda
}
// Si se presiona el boton del joystick, limpiar todo
if (digitalRead(PIN_SEL) == LOW) {
matriz.clear();
}
// Revisar si el jugador alcanzo el punto aleatorio
if (x == puntoX && y == puntoY) {
generarNuevoPunto();
contador++; //Aumento el contador para la puntuacion
}
//Para mostar numeros
void mostrarNumero(byte numero) {
if (numero > 9) numero = 9; // Limita a 9
for (int fila = 0; fila < 8; fila++) {
// El segundo módulo es el índice 1 (el primero es 0)
matriz.setRow(1, fila, numeros[numero][fila]);
}
matriz.update();
}
matriz.clear(); // Limpia la estela antes de actualizar
matriz.setPoint(y, x, true); // Jugador
matriz.setPoint(puntoY, puntoX, true); // Punto objetivo
matriz.update();
delay(100);
}
void generarNuevoPunto() {
do {
puntoX = random(0, maxX + 1);
puntoY = random(0, maxY + 1);
} while (abs(puntoX - x) <= DISTANCIA_MINIMA && abs(puntoY - y) <= DISTANCIA_MINIMA);
}
byte numeros[10][8] = {
{ // 0
B00111100,
B01100110,
B01101110,
B01110110,
B01100110,
B01100110,
B00111100,
B00000000
},
{ // 1
B00011000,
B00111000,
B00011000,
B00011000,
B00011000,
B00011000,
B01111110,
B00000000
},
{ // 2
B00111100,
B01100110,
B00000110,
B00001100,
B00110000,
B01100000,
B01111110,
B00000000
},
{ // 3
B00111100,
B01100110,
B00000110,
B00011100,
B00000110,
B01100110,
B00111100,
B00000000
},
{ // 4
B00001100,
B00011100,
B00101100,
B01001100,
B01111110,
B00001100,
B00001100,
B00000000
},
{ // 5
B01111110,
B01100000,
B01111100,
B00000110,
B00000110,
B01100110,
B00111100,
B00000000
},
{ // 6
B00111100,
B01100110,
B01100000,
B01111100,
B01100110,
B01100110,
B00111100,
B00000000
},
{ // 7
B01111110,
B01100110,
B00000110,
B00001100,
B00011000,
B00011000,
B00011000,
B00000000
},
{ // 8
B00111100,
B01100110,
B01100110,
B00111100,
B01100110,
B01100110,
B00111100,
B00000000
},
{ // 9
B00111100,
B01100110,
B01100110,
B00111110,
B00000110,
B01100110,
B00111100,
B00000000
}
};