Calculadora con arduino

creamos un calculadora con arduino



 Objetivo:

  • Ingresar desde el Monitor Serial una operación como:
    23 + 5
    10 / 2
    7 * 4

  • El Arduino procesará esa operación y mostrará el resultado en la LCD 16x2.


materiales con nuestro kit :

  • Arduino UNO

  • Pantalla LCD 16x2 con módulo I2C

  • Cable USB para enviar datos desde el Monitor Serial


🔌 Conexiones LCD I2C:

LCD I2C Arduino UNO
VCC 5V
GND GND
SDA A4
SCL A5

🧾 Código Arduino – Calculadora vía Monitor Serial

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Configura dirección I2C (0x27 o 0x3F según tu módulo)
LiquidCrystal_I2C lcd(0x27, 16, 2);

String input = "";
float num1 = 0, num2 = 0;
char operador;
bool listo = false;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Calculadora Serial");
  delay(2000);
  lcd.clear();

  Serial.println("Ingresa operacion (ej: 12 + 4):");
}

void loop() {
  if (Serial.available()) {
    input = Serial.readStringUntil('\n');
    input.trim(); // elimina espacios al inicio o final

    if (analizarOperacion(input)) {
      float resultado = calcular(num1, num2, operador);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(input);
      lcd.setCursor(0, 1);
      lcd.print("= ");
      lcd.print(resultado);

      Serial.print("Resultado: ");
      Serial.println(resultado);
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Error sintaxis");
      Serial.println("Error: usa formato como 7 * 8");
    }

    Serial.println("\nIngresa nueva operacion:");
  }
}

bool analizarOperacion(String texto) {
  texto.replace(" ", "");

  int posOp = -1;
  for (int i = 0; i < texto.length(); i++) {
    if (texto[i] == '+' || texto[i] == '-' || texto[i] == '*' || texto[i] == '/') {
      posOp = i;
      operador = texto[i];
      break;
    }
  }

  if (posOp == -1) return false;

  String parte1 = texto.substring(0, posOp);
  String parte2 = texto.substring(posOp + 1);

  if (parte1.length() == 0 || parte2.length() == 0) return false;

  num1 = parte1.toFloat();
  num2 = parte2.toFloat();
  return true;
}

float calcular(float a, float b, char op) {
  switch (op) {
    case '+': return a + b;
    case '-': return a - b;
    case '*': return a * b;
    case '/': return (b != 0) ? a / b : 0;
    default: return 0;
  }
}

🧪 ¿Cómo usarlo?

  1. Abre el Monitor Serial (Ctrl + Shift + M).

  2. Asegúrate de que esté en "Ambos NL y CR" y en 9600 baudios.

  3. Escribe operaciones como:

    12 + 5  
    8 * 7  
    18 / 3  
    9 - 4  
    
  4. El resultado aparecerá tanto en la pantalla LCD como en el Monitor Serial.


Comments

Popular posts from this blog

Te cuento mi aventura como mestra IB de Design y STEM

Cómo enseñar a mover motores con Arduino (RECOMENDABLE PARA COLLEGIOS, GOOGLE REFERENCE SCHOOL QUE USEN CHROMEBOOKS!)

Python con Arduino + Interfaz