Advertisement
Guest User

Calculadora

a guest
Oct 24th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Keypad.h>
  4.  
  5. #define I2C_ADDR 0X27
  6. //Crear el objeto lcd  dirección  0x3F y 16 columnas x 2 filas
  7. LiquidCrystal_I2C lcd(0x27,16,2);  //
  8.  
  9. const byte Columnas = 4; //Cuatro filas
  10. const byte Filas = 4; //Cuatro columnas
  11. //definir el mapa de teclas
  12. char teclas [Filas] [Columnas] = {
  13.  {'1','2','3','+'},
  14.  {'4','5','6','-'},
  15.  {'7','8','9','*'},
  16.  {'C','0','=','/'}
  17. };
  18. byte filasPins[Filas] = {9,8,7,6}; //Definir filas
  19. byte columnasPins[Columnas] = {5,4,3,2}; //Definir columnas
  20.  
  21. //crear el teclado
  22. Keypad miTeclado = Keypad( makeKeymap(teclas), filasPins, columnasPins, Filas, Columnas );
  23.  
  24. //declaración de variables
  25. boolean ValorActual = false;
  26. boolean siguiente = false;
  27. boolean final = false;
  28. String num1, num2, num3;
  29. int calculoTotal;
  30. int movimiento;
  31. char operacion;
  32. float r1,r2,r3,r4;
  33. float decimal;
  34. int contador=0;
  35.  
  36. void setup(){
  37.               // Inicializar el LCD
  38.               lcd.init();
  39.               //Encender la luz de fondo.
  40.               lcd.backlight();
  41.               lcd.begin(16,2);
  42.               lcd.clear();
  43.               lcd.setCursor(4,0); //centrar la palabra en la pantalla LCD
  44.               lcd.print("Claculadora");
  45.               lcd.setCursor(3,1); //centrar la palabra en la pantalla LCD
  46.               lcd.print("Arduino");
  47.               delay(2500);
  48.               lcd.clear(); //borra la pantalla LCD y coloca el cursor en la esquina superior izquierda.  
  49. }
  50.  
  51. //Estructura del bucle del programa
  52. void loop()
  53. {
  54.   char tecla = miTeclado.getKey();
  55.   int longitudDelNumero;
  56.  
  57.   //Verificando que se presiono una tecla determinada
  58.   if(tecla != NO_KEY && (tecla == '1'|| tecla== '2'|| tecla== '3'|| tecla== '4'|| tecla== '5'|| tecla== '6'|| tecla== '7' || tecla== '8' || tecla== '9'|| tecla== '0'))
  59.   {
  60.     //Inicializacion de las variables
  61.     if (contador == 1){
  62.                       lcd.clear();
  63.                       ValorActual = false;
  64.                       final = false;
  65.                       num1 = "";
  66.                       num2 = "";
  67.                       calculoTotal - 0;
  68.                       operacion = ' ';
  69.                       contador=0;
  70.                     }
  71.     if (ValorActual != true){
  72.                               num1 = num1 + tecla;
  73.                               longitudDelNumero= num1.length();
  74.                               lcd.clear();
  75.                               lcd.setCursor(15 - longitudDelNumero, 0); //para ajustar un espacio en blanco para el operador
  76.                               lcd.print(num1);
  77.                             } else {
  78.                                       num2 = num2 + tecla;
  79.                                       longitudDelNumero = num2.length();
  80.                                       lcd.clear();
  81.                                       lcd.setCursor(15 - longitudDelNumero, 1);
  82.                                       lcd.print(num2);
  83.                                       final = true;
  84.                                    }
  85.   } else if ((tecla == '+' || tecla == '-' || tecla == '*' || tecla == '/' ) && ValorActual == false && tecla != NO_KEY)
  86.       {
  87.         if (ValorActual == false){
  88.                                     ValorActual = true;
  89.                                     operacion = tecla;
  90.                                     lcd.clear();
  91.                                     lcd.setCursor(15,0); //operador en la esquina derecha
  92.                                     lcd.print(operacion);
  93.                                  }
  94.       } else if (final == true && tecla != NO_KEY && tecla == '=')
  95.       {
  96.           //depediendo de la operacion seleccionada, procedera al calculo de las distintas operaciones
  97.           switch (operacion){
  98.             case '+':
  99.               calculoTotal = num1.toInt() + num2.toInt();
  100.               break;
  101.             case '-':
  102.               calculoTotal = num1.toInt() - num2.toInt();
  103.               break;
  104.             case '*':
  105.               calculoTotal = num1.toInt() * num2.toInt();
  106.               break;
  107.             case '/':
  108.               if(num2.toInt()==0){
  109.                                     calculoTotal = 'E';
  110.                                  }else
  111.                                       {
  112.                                         calculoTotal = (num1.toInt()) / (num2.toInt());
  113.                                         r1=num1.toInt();
  114.                                         r2=num2.toInt();
  115.                                         decimal=r1/r2;
  116.                                       }
  117.             break;
  118.             default:
  119.             break;  
  120.           }
  121.             num3=String(calculoTotal);
  122.             lcd.clear();
  123.             lcd.setCursor(15,0);
  124.             lcd.autoscroll();
  125.             if (calculoTotal=='E'){
  126.                                     lcd.print("Err. de sintaxis");
  127.                                   } else if (num3.length()>4);{
  128.                                                                 lcd.print("Sin Memoria");
  129.                                                               }
  130.                                                               }else{
  131.                                                                     if (operacion == '/')(lcd.print(decimal,2));
  132.                                                                     else (lcd.print(calculoTotal));
  133.                                                                     contador=1;                              
  134.                                                                    }
  135.             lcd.noAutoscroll();
  136.             if (tecla != NO_KEY && tecla == 'C'){                                            
  137.                                               lcd.clear();
  138.                                               ValorActual = false;
  139.                                               final = false;
  140.                                               num1 = "";
  141.                                               num2 = "";
  142.                                               calculoTotal = 0;
  143.                                               operacion = ' ';
  144.                                             }            
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement