#include <MD_MAX72xx.h>
// Número de matrices LED conectadas
#define NUM_MATRICES 2
//Constantes para el joystick
const int ZONA_MUERTA = 100;
const int CENTRO = 512;
const int LIMITE_INFERIOR = CENTRO - ZONA_MUERTA; // 412
const int LIMITE_SUPERIOR = CENTRO + ZONA_MUERTA; // 612
const int maxX = NUM_MATRICES * 8 - 1; // Máxima coordenada en X (2 matrices de 8 columnas)
const int maxY = 7; // Máxima coordenada en Y (cada matriz tiene 8 filas)
#define PIN_CLK 13 // Pin de reloj
#define PIN_DATO 11 // Pin de datos
#define PIN_CS 10 // Pin de selección de chip
#define PIN_VERT A0 // Pin del eje vertical del joystick
#define PIN_HORZ A1 // Pin del eje horizontal del joystick
#define PIN_SEL 2 // Pin del botón del joystick
// Crear objeto para manejar la matriz LED
MD_MAX72XX matriz = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, PIN_CS, NUM_MATRICES);
// Coordenadas actuales del punto encendido
int x = 0;
int y = 0;
void setup() {
matriz.begin();
matriz.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2); // Ajustar intensidad
matriz.clear(); // Limpiar la pantalla
pinMode(PIN_VERT, INPUT);
pinMode(PIN_HORZ, INPUT);
pinMode(PIN_SEL, INPUT_PULLUP); // Botón con resistencia pull-up interna
}
void loop() {
int horz = analogRead(PIN_HORZ); // Leer valor horizontal del joystick
int vert = analogRead(PIN_VERT); // Leer valor vertical del joystick
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
}
matriz.clear(); // ← Esto borra la posible estela antes de cada movimiento
// Si se presiona el botón del joystick, limpiar la matriz
if (digitalRead(PIN_SEL) == LOW) {
matriz.clear();
}
matriz.setPoint(y, x, true); // Encender el punto en la coordenada (x, y)
matriz.update(); // Actualizar la matriz
delay(100); // Esperar un poco para evitar rebotes
}