Advertisement
Kanamex

S02Practica-Calculadora

Jun 23rd, 2021
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. // C++ code
  2. //
  3. #include <Keypad.h>
  4. const byte FILAS = 4; //Cantidad de filas
  5. const byte COLS = 4; //Cantidad de columnas
  6. //Matriz de valores
  7. char teclas[FILAS][COLS] = {
  8.   {'1','2','3','+'},
  9.   {'4','5','6','-'},
  10.   {'7','8','9','*'},
  11.   {'*','0','=','/'},
  12. };
  13.  
  14. //Configuración de conexión
  15. byte PinFila[FILAS] = {9,8,7,6};//conexión de filas
  16. byte PinCols[COLS] = {5,4,3,2};//conexión de columnas
  17.  
  18. //Crear objeto de la clase Keypad
  19. Keypad teclado = Keypad(makeKeymap(teclas), PinFila, PinCols, FILAS, COLS);
  20. int paso =0;
  21. int numero1=0;
  22. int numero2=0;
  23. int operacion=0;
  24. int resultado=0;
  25. int r=0;
  26. void setup()
  27. {
  28.  
  29.   Serial.begin(9600);
  30.   Serial.println();
  31.  
  32. }
  33.  
  34. void loop()
  35. {
  36.  
  37.   if(paso==0){
  38.   Serial.println();
  39.     Serial.print("primer numero:");
  40.  paso++;
  41.  
  42.   }
  43.  if(paso==1){
  44.     char tecla = teclado.getKey();
  45.      if (tecla != 0x00)
  46.   {
  47.   Serial.print(tecla);
  48.   numero1 = String(tecla).toInt();
  49.     paso++;
  50.     }      
  51.   }
  52.  
  53.   if(paso==2){
  54.     Serial.println();
  55.   Serial.print("Operador:");
  56.   paso++;
  57.  
  58.  
  59.   }
  60.  
  61.   if(paso==3){
  62.      char tecla = teclado.getKey();  
  63.     if (tecla != 0x00)
  64.   {
  65.   Serial.print(tecla);
  66.   operacion = tecla;
  67.     paso++;
  68.     }      
  69.   }
  70.  
  71.    if(paso==4){
  72.     Serial.println();
  73.      Serial.print("segundo numero:");
  74.        paso++;
  75.      
  76.   }
  77.    
  78.    if(paso==5){
  79.     char tecla = teclado.getKey();
  80.      if (tecla != 0x00)
  81.   {
  82.   Serial.print(tecla);
  83.   numero2 = String(tecla).toInt();
  84.     paso++;
  85.     }      
  86.   }
  87. if(paso==6){
  88. Serial.println();
  89.    Serial.print("Resultado:");
  90.   if(operacion=='+')
  91.   {
  92.   resultado=numero1+numero2;
  93.   }
  94.    if(operacion=='-')
  95.   {
  96.   resultado=numero1-numero2;
  97.   }
  98.    if(operacion=='*')
  99.   {
  100.   resultado=numero1*numero2;
  101.   }
  102.    if(operacion=='/')
  103.   {
  104.      
  105.      if(numero2!=0)
  106.      {
  107.   resultado=numero1/numero2;
  108.      }
  109.      else{
  110.      Serial.println("Infinito");
  111.      }
  112.    }
  113.    
  114.       Serial.println(resultado);
  115.  
  116.   paso =0;
  117.   }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement