#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BTN_SELECT 14 // Botón para elegir estudiante
#define BTN_RESET 27 // Botón para reiniciar la lista
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
const char* students[] = {"Randy","Mel","Andy","Andres"};
const int totalStudents = sizeof(students) / sizeof(students[0]);
bool selectedStudents[totalStudents] = {false}; // Para controlar los que ya salieron
int remaining = totalStudents; // Cantidad de estudiantes restantes
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
pinMode(BTN_SELECT, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
LCD.setCursor(0, 0);
LCD.print("Presiona boton");
LCD.setCursor(0, 1);
LCD.print("para elegir");
}
void loop() {
if (digitalRead(BTN_SELECT) == LOW) {
selectStudent();
delay(300); // Antirrebote
}
if (digitalRead(BTN_RESET) == LOW) {
resetSelection();
delay(300);
}
}
void selectStudent() {
if (remaining == 0) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Todos elegidos!");
return;
}
int index;
do {
index = random(totalStudents);
} while (selectedStudents[index]);
selectedStudents[index] = true;
remaining--;
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Seleccionado:");
LCD.setCursor(0, 1);
LCD.print(students[index]);
Serial.println("Elegido: " + String(students[index]));
}
void resetSelection() {
for (int i = 0; i < totalStudents; i++) {
selectedStudents[i] = false;
}
remaining = totalStudents;
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Lista Reiniciada");
LCD.setCursor(0, 1);
LCD.print("Presiona boton");
Serial.println("Lista reiniciada");
}