Advertisement
Guest User

Calculadora

a guest
Mar 25th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <Keypad.h>
  3. const byte filas = 4;  
  4. const byte columnas = 4;
  5. byte pinsFilas[filas]={7, 6, 5,4};//pines para filas
  6. byte pinsColumnas[columnas]={3, 2, A4,A5};//pines para columnas
  7. //Defino el Keymap
  8. char  teclas[filas][columnas]={
  9.   {'1','2','3','A'},
  10.   {'4','5','6','B'},
  11.   {'7','8','9','C'},
  12.   {'*','0','#','D'}
  13. };
  14. Keypad teclado =Keypad(makeKeymap(teclas), pinsFilas, pinsColumnas, filas, columnas);
  15. //finnnnnnn
  16.  
  17. const int rs = 12, en = 13, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
  18. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  19.  
  20. char resultado=0,operacion,variable=0,variable2=0;
  21.  
  22. boolean primer_valor = false;
  23. boolean primer_operacion = false;
  24. boolean terminar_operacion = false;
  25. boolean es_negativo = false;
  26. void setup() {
  27.   Serial.begin(9600);
  28.    lcd.begin(16, 2);
  29. }
  30. void loop() {
  31. char boton = teclado.getKey();
  32. if(boton != NO_KEY){
  33.             //*******************************SUMA*************************
  34.             if((boton=='A')&&(primer_valor==true)&&(primer_operacion == false)){  
  35.               primer_operacion=true;
  36.               operacion=boton;
  37.               lcd.print ('+');
  38.              primer_valor=false;
  39.             }
  40.               //*******************************RESTA
  41.             if((boton=='B')&&(primer_valor==true)&&(primer_operacion == false)){  
  42.               primer_operacion=true;
  43.               operacion=boton;
  44.               lcd.print ('-');
  45.              primer_valor=false;
  46.             }
  47.             //*******************************VALOR NRGATIVO
  48.             if((boton=='B')&&(primer_valor==false)&&(primer_operacion == false)){
  49.                               es_negativo=true;  
  50.                                lcd.print ('-');
  51.             }
  52.            
  53.             //*******************************IGUAL
  54.                      if((boton =='D')){
  55.                      switch(operacion){
  56.                       case 'A':
  57.                      resultado=variable+variable2;
  58.                     if(es_negativo==true){resultado=-variable+variable2;}
  59.                       break;
  60.                       case 'B':
  61.                     resultado=variable-variable2;
  62.                     if(es_negativo==true){resultado=-variable-variable2;}
  63.                       break;
  64.                       }
  65. lcd.setCursor(0, 1);
  66. lcd.print(resultado);
  67.        }
  68.          //*****************************RESET******************************
  69.                                  if((boton =='C')){
  70.                                     lcd.clear();
  71.                                      primer_valor = false;
  72.                                      primer_operacion = false;
  73.                                      terminar_operacion = false;
  74.                                      es_negativo==false;
  75.                                      resultado=0;
  76.                                      variable=0;
  77.                                      variable2=0;
  78.                                      }
  79.            //*****************VEO SI ES UN NUMERO*************************
  80.            if((boton !='A')&&(boton !='B')&&(boton !='D')&&(boton !='C')){
  81.                                if(primer_operacion==false){  
  82. primer_valor=true;
  83.                               variable=10*variable+boton-48;
  84.                               lcd.print(boton);
  85.                                primer_valor=true;
  86.                                }
  87.                                if(primer_operacion==true) {
  88.                                 variable2=10*variable2+boton-48;
  89.                                 lcd.print(boton);
  90.                                 terminar_operacion=true;
  91.                                 }
  92.             }
  93.    
  94.            
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement