#include <LedControl.h>

LedControl matriz = LedControl(12, 11, 10, 1);

const int VRx = A0;
const int VRy = A1;
const int SW = 2;  // botón del joystick

int posX = 3; // posición inicial del punto
int posY = 3;

void dibujarPunto(int x, int y) {
  matriz.setLed(0, y, x, true);
}

void setup() {
  matriz.shutdown(0, false);
  matriz.setIntensity(0, 8);
  matriz.clearDisplay(0);

  pinMode(SW, INPUT_PULLUP);  // usar el botón SW como reset

  dibujarPunto(posX, posY);
}

void loop() {
  int xVal = analogRead(VRx);
  int yVal = analogRead(VRy);

  // Movimiento del joystick (con zona muerta)
  if (xVal < 400 && posX > 0) posX--;  
  else if (xVal > 600 && posX < 7) posX++;

  if (yVal < 400 && posY > 0) posY--;  
  else if (yVal > 600 && posY < 7) posY++;

  dibujarPunto(posX, posY);

  // Reset con SW del joystick
  if (digitalRead(SW) == LOW) {
    matriz.clearDisplay(0);
    posX = 3;
    posY = 3;
    dibujarPunto(posX, posY);
    delay(200); // anti-rebote
  }

  delay(100);
}

