/* Erick Nuñez=>>Display de 7 segmentos con pulsador ESP32 */
const int a = 22; //Ping para el segmento a
const int b = 2; //Ping para el segmento b
const int c = 21; //Ping para el segmento c
const int d = 4; //Ping para el segmento d
const int e = 5; //Ping para el segmento e
const int f = 15;//Ping para el segmento f
const int g = 16; //Ping para el segmento g
long randomNumber;//Variable donde almacenaremos el numero aleatorio
const int segmentPins[] = {22, 2, 21, 4, 5, 15, 16,0};
const int dpPin = 18; // Pin para el punto del display
int Segmento = 0;
int Retardo = 50;
int Pulsador = 19;//Se declara PIN 19 de pulsador
// Declaración de la matriz de valores binarios de combinaciones del display
const byte numberDisplay[10] = {
B1000000, // Número 0
B1111001, // Número 1
B0100100, // Número 2
B0110000, // Número 3
B0011001, // Número 4
B0010010, // Número 5
B0000010, // Número 6
B1111000, // Número 7
B0000000, // Número 8
B0010000 // Número 9
};
void setup() {
Serial.begin(115200);
pinMode(c, OUTPUT);
pinMode(Pulsador, INPUT);
// Inicialización de los pines del display
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(dpPin, OUTPUT);
digitalWrite(dpPin, HIGH);
}
void loop() {
delay(Retardo);
// Variables internas del bucle
int longitudLista = 0;
int numero[longitudLista] = {};
if (digitalRead(Pulsador) == HIGH){
Segmento = Segmento +1; //Incremento +1
}
switch (Segmento){
case 0:
delay(40);
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW );
digitalWrite(e, LOW );
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;
case 1:
//Genera un numero aleatorio entre 1 y 6
randomNumber = random(0,6);
//Escribimos el numero aleatorio por el puerto serie
Serial.print("El numero aleatorio es = ");
Serial.println(randomNumber+1);
activarDisplay(randomNumber+1);
delay(1000);
Segmento= 0;
break;
case 2:
Segmento= 0;
break;
default:
Segmento = -1; //Delimitador -1
break;
}
}
void activarDisplay(int number) {
byte arrayBit = numberDisplay[number];
for (int i = 0; i < 7; i++) {
int bit = bitRead(arrayBit, i);
digitalWrite(segmentPins[i], bit);
}
}