// Pines para los displays de 7 segmentos (cátodo común)
const int segPins[] = {21, 19, 18, 5, 17, 16, 4}; // a,b,c,d,e,f,g
// Pines para los comunes de los displays
const int common1 = 15;
const int common2 = 23;
// Patrones de segmentos para números 0-9
const bool digitSegments[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};
// Configuración del keypad 4x4
const byte ROWS = 4;
const byte COLS = 4;
// Mapa de teclas
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Pines de conexión del keypad
byte rowPins[ROWS] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
// Variables para la suma
int firstDigit = -1;
int secondDigit = -1;
int resultado = -1;
bool mostrarResultado = false;
unsigned long tiempoMostrarResultado = 0;
const unsigned long RETARDO_RESULTADO = 2000; // 2 segundos antes de mostrar la suma
void setup() {
Serial.begin(115200);
// Configurar pines del display como salidas
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT);
digitalWrite(segPins[i], LOW);
}
// Configurar pines comunes
pinMode(common1, OUTPUT);
pinMode(common2, OUTPUT);
digitalWrite(common1, HIGH);
digitalWrite(common2, HIGH);
// Configurar keypad
for (byte i = 0; i < ROWS; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], HIGH);
}
for (byte i = 0; i < COLS; i++) {
pinMode(colPins[i], INPUT_PULLUP);
}
Serial.println("POR FAVOR, INGRESE DOS DIGITOS.");
Serial.println("LA TECLA C LIMPIARA LOS VALORES.");
}
// Escanear keypad
char scanKeypad() {
for (byte r = 0; r < ROWS; r++) {
digitalWrite(rowPins[r], LOW);
for (byte c = 0; c < COLS; c++) {
if (digitalRead(colPins[c]) == LOW) {
delay(50);
if (digitalRead(colPins[c]) == LOW) {
digitalWrite(rowPins[r], HIGH);
return keys[r][c];
}
}
}
digitalWrite(rowPins[r], HIGH);
}
return '\0';
}
// Limpiar operación
void limpiarOperacion() {
firstDigit = -1;
secondDigit = -1;
resultado = -1;
mostrarResultado = false;
digitalWrite(common1, HIGH);
digitalWrite(common2, HIGH);
Serial.println("SE LIMPIARON LOS VALORES, POR FAVOR INGRESE NUEVAMENTE.");
}
// Mostrar dígito en ambos displays usando multiplexado
void mostrarMultiplex(int digito1, int digito2) {
// Mostrar primer dígito
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], digitSegments[digito1][i] ? HIGH : LOW);
}
digitalWrite(common1, LOW);
digitalWrite(common2, HIGH);
delay(5);
digitalWrite(common1, HIGH); // apagar display1
// Mostrar segundo dígito
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], digitSegments[digito2][i] ? HIGH : LOW);
}
digitalWrite(common1, HIGH);
digitalWrite(common2, LOW);
delay(5);
digitalWrite(common2, HIGH); // apagar display2
}
// Bucle principal
void loop() {
char key = scanKeypad();
if (key) {
Serial.print("NO.: ");
Serial.println(key);
if (key == 'C') {
limpiarOperacion();
}
else if (key >= '0' && key <= '9') {
int num = key - '0';
if (firstDigit == -1) {
firstDigit = num;
Serial.print("INGRESE PRIMER DIGITO: ");
Serial.println(firstDigit);
}
else if (secondDigit == -1) {
secondDigit = num;
Serial.print("INGRESE SEGUNDO DIGITO: ");
Serial.println(secondDigit);
resultado = firstDigit + secondDigit;
tiempoMostrarResultado = millis() + RETARDO_RESULTADO;
mostrarResultado = false; // aún no mostrar suma
Serial.print("EL RESULTADO SE MOSTRARA EN: ");
Serial.println(RETARDO_RESULTADO / 1000);
}
}
}
// Mostrar dígitos o suma
if (!mostrarResultado) {
// Si no hay dígitos ingresados, apagar ambos displays
if (firstDigit == -1 && secondDigit == -1) {
digitalWrite(common1, HIGH);
digitalWrite(common2, HIGH);
} else {
int d1 = (firstDigit != -1) ? firstDigit : 0;
int d2 = (secondDigit != -1) ? secondDigit : 0;
mostrarMultiplex(d1, d2);
}
// Verificar si ya pasó el tiempo para mostrar el resultado
if (resultado >= 0 && millis() >= tiempoMostrarResultado) {
mostrarResultado = true;
}
} else {
int decenas = resultado / 10;
int unidades = resultado % 10;
mostrarMultiplex(decenas, unidades);
}
}