Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <Keypad.h>
- #define I2C_ADDR 0X27
- //Crear el objeto lcd dirección 0x3F y 16 columnas x 2 filas
- LiquidCrystal_I2C lcd(0x27,16,2); //
- const byte Columnas = 4; //Cuatro filas
- const byte Filas = 4; //Cuatro columnas
- //definir el mapa de teclas
- char teclas [Filas] [Columnas] = {
- {'1','2','3','+'},
- {'4','5','6','-'},
- {'7','8','9','*'},
- {'C','0','=','/'}
- };
- byte filasPins[Filas] = {9,8,7,6}; //Definir filas
- byte columnasPins[Columnas] = {5,4,3,2}; //Definir columnas
- //crear el teclado
- Keypad miTeclado = Keypad( makeKeymap(teclas), filasPins, columnasPins, Filas, Columnas );
- //declaración de variables
- boolean ValorActual = false;
- boolean siguiente = false;
- boolean final = false;
- String num1, num2, num3;
- int calculoTotal;
- int movimiento;
- char operacion;
- float r1,r2,r3,r4;
- float decimal;
- int contador=0;
- void setup(){
- // Inicializar el LCD
- lcd.init();
- //Encender la luz de fondo.
- lcd.backlight();
- lcd.begin(16,2);
- lcd.clear();
- lcd.setCursor(4,0); //centrar la palabra en la pantalla LCD
- lcd.print("Claculadora");
- lcd.setCursor(3,1); //centrar la palabra en la pantalla LCD
- lcd.print("Arduino");
- delay(2500);
- lcd.clear(); //borra la pantalla LCD y coloca el cursor en la esquina superior izquierda.
- }
- //Estructura del bucle del programa
- void loop()
- {
- char tecla = miTeclado.getKey();
- int longitudDelNumero;
- //Verificando que se presiono una tecla determinada
- if(tecla != NO_KEY && (tecla == '1'|| tecla== '2'|| tecla== '3'|| tecla== '4'|| tecla== '5'|| tecla== '6'|| tecla== '7' || tecla== '8' || tecla== '9'|| tecla== '0'))
- {
- //Inicializacion de las variables
- if (contador == 1){
- lcd.clear();
- ValorActual = false;
- final = false;
- num1 = "";
- num2 = "";
- calculoTotal - 0;
- operacion = ' ';
- contador=0;
- }
- if (ValorActual != true){
- num1 = num1 + tecla;
- longitudDelNumero= num1.length();
- lcd.clear();
- lcd.setCursor(15 - longitudDelNumero, 0); //para ajustar un espacio en blanco para el operador
- lcd.print(num1);
- } else {
- num2 = num2 + tecla;
- longitudDelNumero = num2.length();
- lcd.clear();
- lcd.setCursor(15 - longitudDelNumero, 1);
- lcd.print(num2);
- final = true;
- }
- } else if ((tecla == '+' || tecla == '-' || tecla == '*' || tecla == '/' ) && ValorActual == false && tecla != NO_KEY)
- {
- if (ValorActual == false){
- ValorActual = true;
- operacion = tecla;
- lcd.clear();
- lcd.setCursor(15,0); //operador en la esquina derecha
- lcd.print(operacion);
- }
- } else if (final == true && tecla != NO_KEY && tecla == '=')
- {
- //depediendo de la operacion seleccionada, procedera al calculo de las distintas operaciones
- switch (operacion){
- case '+':
- calculoTotal = num1.toInt() + num2.toInt();
- break;
- case '-':
- calculoTotal = num1.toInt() - num2.toInt();
- break;
- case '*':
- calculoTotal = num1.toInt() * num2.toInt();
- break;
- case '/':
- if(num2.toInt()==0){
- calculoTotal = 'E';
- }else
- {
- calculoTotal = (num1.toInt()) / (num2.toInt());
- r1=num1.toInt();
- r2=num2.toInt();
- decimal=r1/r2;
- }
- break;
- default:
- break;
- }
- num3=String(calculoTotal);
- lcd.clear();
- lcd.setCursor(15,0);
- lcd.autoscroll();
- if (calculoTotal=='E'){
- lcd.print("Err. de sintaxis");
- } else if (num3.length()>4);{
- lcd.print("Sin Memoria");
- }
- }else{
- if (operacion == '/')(lcd.print(decimal,2));
- else (lcd.print(calculoTotal));
- contador=1;
- }
- lcd.noAutoscroll();
- if (tecla != NO_KEY && tecla == 'C'){
- lcd.clear();
- ValorActual = false;
- final = false;
- num1 = "";
- num2 = "";
- calculoTotal = 0;
- operacion = ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement