STEMGAME - INV
#define BUTTON_A 25
#define BUTTON_B 27
#define BUTTON_C 14
#define BUTTON_D 13
#define BUZZER 23
// Variables del juego
const int botones[] = {BUTTON_A, BUTTON_B, BUTTON_C, BUTTON_D};
int respuestasCorrectas[40] = {0, 0, 1, 1, 2,
0, 1, 1, 2, 2,
1, 0, 0, 2, 0,
1, 0, 1, 2, 1,
2, 0, 1, 2, 0,
2, 2, 2, 0, 0,
2, 2, 0, 1, 2,
1, 1, 1, 1, 1}; // 0=A, 1=B, 2=C, 3=D
// Variables para selección aleatoria
int preguntaActual = 0;
int preguntasAleatorias[10];
float puntuacion = 0.0;
bool esperandoRespuesta = false;
void setup() {
#ifdef ESP32
FPSerial.begin(9600, SERIAL_8N1, 16, 17);
#else
FPSerial.begin(9600);
#endif
Serial.begin(115200);
Serial.println(F("Inicializando..."));
if (!myDFPlayer.begin(FPSerial)) {
Serial.println(F("Error en DFPlayer!"));
while(true);
}
myDFPlayer.volume(30);
// Configurar pines
for(int i = 0; i < 4; i++) {
pinMode(botones[i], INPUT);
}
pinMode(BUZZER, OUTPUT);
seleccionarPreguntasAleatorias();
reproducirPregunta();
}
void seleccionarPreguntasAleatorias() {
randomSeed(analogRead(19));
for (int i = 0; i < 10; i++) {
int pregunta;
bool repetida;
do {
repetida = false;
pregunta = random(1, 41);
for (int j = 0; j < i; j++) {
if (preguntasAleatorias[j] == pregunta) {
repetida = true;
break;
}
}
} while (repetida);
preguntasAleatorias[i] = pregunta;
Serial.print("Pregunta ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(pregunta);
}
}
void reproducirPregunta() {
int numeroPregunta = preguntasAleatorias[preguntaActual];
Serial.print(F("Reproduciendo pregunta "));
Serial.println(numeroPregunta);
// Audio introductorio
switch(preguntaActual) {
case 0: myDFPlayer.playMp3Folder(3); break;
case 1: myDFPlayer.playMp3Folder(4); break;
case 2: myDFPlayer.playMp3Folder(5); break;
case 3: myDFPlayer.playMp3Folder(6); break;
case 4: myDFPlayer.playMp3Folder(7); break;
case 5: myDFPlayer.playMp3Folder(8); break;
case 6: myDFPlayer.playMp3Folder(9); break;
case 7: myDFPlayer.playMp3Folder(10); break;
case 8:
myDFPlayer.playMp3Folder(11);
delay(2500); // Delay especial para pregunta 9
while(myDFPlayer.readState() == 512) delay(100);
break;
case 9: myDFPlayer.playMp3Folder(12); break;
}
delay(2000);
// Audio pregunta principal
myDFPlayer.play(numeroPregunta);
delay(2000);
esperandoRespuesta = true;
}
void repetirPregunta() {
Serial.println(F("Repitiendo pregunta..."));
myDFPlayer.stop();
delay(200);
reproducirPregunta();
}
void loop() {
if(esperandoRespuesta) {
if(digitalRead(BUTTON_D) == HIGH) { // Botón D para repetir
delay(300);
repetirPregunta();
}
else {
for(int i = 0; i < 3; i++) { // Solo verifica botones A, B, C
if(digitalRead(botones[i]) == HIGH) {
verificarRespuesta(i);
delay(300);
break;
}
}
}
}
}
void verificarRespuesta(int opcion) {
esperandoRespuesta = false;
int numeroPregunta = preguntasAleatorias[preguntaActual];
int indiceRespuesta = numeroPregunta - 1;
if(opcion == respuestasCorrectas[indiceRespuesta]) {
Serial.println(F("¡Respuesta correcta!"));
tonoCorrecto();
myDFPlayer.playMp3Folder(1);
puntuacion += 0.5;
if (puntuacion > 5.0) puntuacion = 5.0;
} else {
Serial.println(F("¡Respuesta incorrecta!"));
tonoIncorrecto();
myDFPlayer.playMp3Folder(2);
}
delay(2000);
preguntaActual++;
if(preguntaActual < 10) {
delay(1000);
reproducirPregunta();
} else {
finalizarJuego();
}
}
void finalizarJuego() {
Serial.println(F("¡Juego completado!"));
Serial.print(F("Puntuación final: "));
Serial.println(puntuacion);
int audioPuntuacion = 13 + (int)(puntuacion * 2) - 1;
myDFPlayer.playMp3Folder(audioPuntuacion);
delay(4000); // Cambiado de 500 a 3000 como solicitaste
tonoVictoria();
Serial.println(F("Presiona el botón A para reiniciar"));
myDFPlayer.playMp3Folder(23);
delay(3000);
while(true) {
if(digitalRead(BUTTON_A) == HIGH) {
delay(300);
puntuacion = 0.0;
preguntaActual = 0;
seleccionarPreguntasAleatorias();
reproducirPregunta();
break;
}
delay(100);
}
}
// Funciones del buzzer (sin cambios)
void tonoCorrecto() {
tone(BUZZER, 1000, 200);
delay(100);
tone(BUZZER, 1500, 300);
}
void tonoIncorrecto() {
tone(BUZZER, 300, 500);
delay(100);
tone(BUZZER, 200, 500);
}
void tonoVictoria() {
for(int i = 0; i < 3; i++) {
tone(BUZZER, 1000 + (i * 200), 200);
delay(150);
}
}
Comentarios
Publicar un comentario