#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float num1, num2;
char operacion;
long tiempoInicio, tiempoFin;
bool operacionRealizada = false;
const byte filas = 4;
const byte columnas = 4;
byte pinesFilas[] = {34, 35, 32, 33};
byte pinesColumnas[] = {25, 26, 27, 14};
char teclas[4][4] = {{'1','2','3','*'},
{'4','5','6','/'},
{'7','8','9','+'},
{'.','0','=','-'}};
Keypad teclado1 = Keypad( makeKeymap(teclas), pinesFilas, pinesColumnas, filas, columnas);
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
}
void loop() {
Lectura();
mostrarResultado();
}
bool nuevaOperacion = false; // Paso 1: Agregar variable de estado
void Lectura() {
char tecla = teclado1.getKey();
if (tecla) {
if (nuevaOperacion) { // Comprueba si es el inicio de una nueva operación
num1 = 0; // Restablece num1
num2 = 0; // Restablece num2
operacion = 0; // Restablece la operación
nuevaOperacion = false; // Restablece la bandera de nueva operación
display.clearDisplay(); // Limpia la pantalla al inicio de una nueva operación
}
if (isdigit(tecla)) {
if (!operacion) {
num1 = num1 * 10 + (tecla - '0');
} else {
num2 = num2 * 10 + (tecla - '0');
}
} else if (tecla == '*' || tecla == '/' || tecla == '+' || tecla == '-') {
operacion = tecla;
} else if (tecla == '=') {
tiempoInicio = micros();
realizarOperacion();
tiempoFin = micros();
nuevaOperacion = true; // Establece la bandera para iniciar una nueva operación después de esta
}
}
}
// Asegúrate de que el resto del código permanezca igual. No necesitas modificar las otras funciones para agregar esta funcionalidad.
void realizarOperacion() {
switch (operacion) {
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '*':
num1 *= num2;
break;
case '/':
num1 = num1 / num2;
break;
}
operacion = 0;
}
void mostrarResultado() {
display.clearDisplay();
display.setCursor(0, 0);
if (operacion) {
display.print(num1);
display.print(" ");
display.print(operacion);
display.print(" ");
display.print(num2);
} else {
display.print(num1);
}
display.setCursor(0, 20);
display.print("Time: ");
display.print(tiempoFin - tiempoInicio);
display.print(" us");
display.display();
}