/*
* Mando Arcade con Arduino Uno
*
* Configuración de Hardware:
* - Joystick Analógico: HORZ -> A0, VERT -> A1, Pulsador SEL -> Pin 10
* - Botones de juego (6 de acción):
* - Pin 2: Botón 2 (Azul - Columna 1 Superior)
* - Pin 3: Botón 1 (Rojo - Columna 2 Superior)
* - Pin 4: Botón 3 (Amarillo - Columna 3 Superior)
* - Pin 5: Botón 6 (Azul - Columna 1 Inferior)
* - Pin 6: Botón 5 (Rojo - Columna 2 Inferior)
* - Pin 7: Botón 4 (Amarillo - Columna 3 Inferior)
* - Botones auxiliares:
* - Pin 8: Botón 7 (Negro - Pausa)
* - Pin 9: Botón 8 (Negro - Select)
* - Luz testigo de encendido:
* - Pin 12: LED Verde (vía resistencia de 220 Ω)
*
* Formato de salida por Serial (115200 baudios):
* VALOR_X,VALOR_Y,TRAMA_BOTONES
* Ejemplo: "512,512,000000000"
*/
const int TOTAL_BOTONES = 9;
// Mapeo ordenado de pines: [0..8] correspondientes a BTN_0..BTN_8 en joystick_driver.py
const int pinesBotones[TOTAL_BOTONES] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const int PIN_LED_TESTIGO = 12;
const int PIN_JOYSTICK_X = A0;
const int PIN_JOYSTICK_Y = A1;
void setup() {
// Inicializar comunicación puerto serie a 115200 baudios
Serial.begin(115200);
// Configurar los 9 botones con resistencia Pull-Up interna (INPUT_PULLUP)
for (int i = 0; i < TOTAL_BOTONES; i++) {
pinMode(pinesBotones[i], INPUT_PULLUP);
}
// Activar luz testigo (LED Verde)
pinMode(PIN_LED_TESTIGO, OUTPUT);
digitalWrite(PIN_LED_TESTIGO, HIGH);
}
void loop() {
// 1. Lectura analógica del Joystick (0 a 1023)
int valorX = analogRead(PIN_JOYSTICK_X);
int valorY = analogRead(PIN_JOYSTICK_Y);
// 2. Construcción de la cadena de 9 botones (1 = Presionado, 0 = Suelto)
String tramaBotones = "";
for (int i = 0; i < TOTAL_BOTONES; i++) {
tramaBotones += String(!digitalRead(pinesBotones[i]));
}
// 3. Envío de datos por puerto Serie
Serial.print(valorX);
Serial.print(",");
Serial.print(valorY);
Serial.print(",");
Serial.println(tramaBotones);
// 4. Pequeño retardo para estabilidad de muestreo (~100 Hz)
delay(10);
}