// ============================================
// Actividad 3 - Parte 2
// Juego del dado con ESP32 + Display 7 segmentos
// Se usa secuencia de leds para sensación de dado.
// Modo ÁNODO COMÚN
// ============================================
// Pines segmentos (a b c d e f g)
int segPins[7] = {23, 22, 21, 19, 18, 5, 4};
int dpPin = 15;
int botonPin = 13; // Botón con resistencia interna.
bool estadoAnterior = HIGH;
// Tabla de segmentos para números 0–9
// 1 -> Apagado y 0-> Encendido
byte digits[10][7] = {
{0,0,0,0,0,0,1}, // 0
{1,0,0,1,1,1,1}, // 1
{0,0,1,0,0,1,0}, // 2
{0,0,0,0,1,1,0}, // 3
{1,0,0,1,1,0,0}, // 4
{0,1,0,0,1,0,0}, // 5
{0,1,0,0,0,0,0}, // 6
{0,0,0,1,1,1,1}, // 7
{0,0,0,0,0,0,0}, // 8
{0,0,0,0,1,0,0} // 9
};
// --------------------------------------------
// FUNCIONES
// --------------------------------------------
void limpiarDisplay() {
for (int i = 0; i < 7; i++)
digitalWrite(segPins[i], HIGH); // HIGH = apagado en ánodo
digitalWrite(dpPin, HIGH);
}
void mostrarNumero(int num) {
for (int i = 0; i < 7; i++)
digitalWrite(segPins[i], digits[num][i]);
digitalWrite(dpPin, HIGH); // punto apagado
}
void animacion() {
int ciclos = 2; // puedes ajustar
for (int r = 0; r < ciclos; r++) {
for (int i = 0; i < 7; i++) {
limpiarDisplay();
digitalWrite(segPins[i], LOW); // LOW = encender
delay(80);
}
mostrarNumero(8);
delay(100);
}
}
// --------------------------------------------
// SETUP
// --------------------------------------------
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
digitalWrite(segPins[i], HIGH); // iniciar apagado
}
pinMode(dpPin, OUTPUT);
digitalWrite(dpPin, HIGH);
pinMode(botonPin, INPUT_PULLUP);
randomSeed(analogRead(34));
}
// --------------------------------------------
// LOOP
// --------------------------------------------
void loop() {
bool estadoActual = digitalRead(botonPin);
if (estadoAnterior == HIGH && estadoActual == LOW) {
delay(50); // anti-rebote
animacion();
int resultado = random(1, 7);
mostrarNumero(resultado);
while (digitalRead(botonPin) == LOW);
delay(200);
}
estadoAnterior = estadoActual;
}