#include <MD_MAX72xx.h>
// Número de matrices LED conectadas
#define NUM_MATRICES 2
//Constantes para el joystick
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
// Mover hacia abajo
if (vert < 500) {
y = min(y + 1, maxY);
}
// Mover hacia arriba
if (vert > 520) {
y = max(y - 1, 0);
}
// Mover a la derecha
if (horz > 520) {
x = min(x + 1, maxX);
}
// Mover a la izquierda
if (horz < 500) {
x = max(x - 1, 0);
}
// 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
}