Arduino C ++ Calculator

1

Hello, I have made a calculator with an LCD and a keyboard and I have almost ready it but when adding any number it gives me 0, could you tell me what I have done wrong? Thank you. The part of the sua is complete but I do not add it to it.

#include <Keypad.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
String a = "";
char hexaKeys[ROWS][COLS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'-','0','=','/'}
};
byte rowPins[ROWS] = {46, 47, 48, 49}; //connect to the row pinouts of 
the keypad
byte colPins[COLS] = {50, 51, 52, 53}; //connect to the column pinouts 
of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, 
ROWS, COLS); 


void setup() {
   Serial.begin(9600);
   // set up the LCD's number of columns and rows:
   lcd.begin(199, 1);
   // Print a message to the LCD.

}
int suma = 0;
int resta = 0;
int multi = 0;
int divi = 0;
bool si = true;

char signo = "";
int primer_numero = 0;
int segundo_numero = 0;


void loop() {
  char c = customKeypad.getKey();




if (c){


  if (c == hexaKeys[0][3]) {  //SUMA
     suma = 1; 
     signo = char("+";
  }
 if (c == hexaKeys[1][3]) {  //RESTA
     resta = 1;
     signo = char("-") ;
  }if (c == hexaKeys[2][3]) {  //MULTIPLI
     multi = 1; 
     signo = char("*");
  }if (c == hexaKeys[3][3]) {  //DIVISIO
     divi = 1; 
     signo = char("/");
  }







  if (c == hexaKeys[3][2] && suma == int(1))  {
    si = false;
    lcd.clear();
    lcd.print(primer_numero + segundo_numero);
  }
  if(c == hexaKeys[3][2] && resta == int(1)){
    si = false;
    lcd.print(primer_numero - segundo_numero);

  } if (c == hexaKeys[3][2] && multi == int(1)) {
     lcd.print(primer_numero * segundo_numero);
     si = false;
    }

    if (c == hexaKeys[3][2] && divi == int(1)) {
      lcd.print(primer_numero / segundo_numero);
      si = false;
    }


if(si == true){


     a += String(c);
    lcd.print(a);
    Serial.println(c);
    // set the cursor to column 0, line 1 


   lcd.setCursor(0, 1);
  }
   if (c != hexaKeys[0][3] && hexaKeys[1][3] && hexaKeys[2][3] && 
   hexaKeys[3][3] ){
   String(primer_numero) +=  String(c);
  if(primer_numero > 0){
    String(segundo_numero) += String(c);
     }
   } 
  }
}
    
asked by Ismael 07.01.2018 в 10:27
source

1 answer

2
if (c == hexaKeys[3][2] && suma == int(1)) {

As far as I get after reading your code, hexaKeys[3][2] corresponds to the * key, when in c you will find + ... do not you trust the state of the variables of your own program?

With this it should be worth:

void loop () {
  suma = 0;

  if (suma == 1) {

But of course ... it can be an authentic chaos to have to manage 4 variables that are mutually exclusive ... that is, there can only be one active ... for these types of situations the ones listed were invented:

enum Operacion
{
  NINGUNA,
  SUMA,
  RESTA,
  PRODUCTO,
  DIVISION,
};

With what your code would now look like this:

void loop()
{
  char c = customKeypad.getKey();
  Operacion op = NINGUNA;

  if (c == 0 )
    return;

  if (c == hexaKeys[0][3])
    op = SUMA;
  else if (c == hexaKeys[1][3])
    op = RESTA;
  else if (c == hexaKeys[2][3])
    op = PRODUCTO;
  else if (c == hexaKeys[3][3])
    op = DIVISION;

  switch( op )
  {
    case SUMA:
      // si = false; <<--- no es necesario
      lcd.clear();
      lcd.print(primer_numero + segundo_numero);
      break;

    case RESTA:

    // ...

    default:
      // aqui el codigo de si == true
  }
}

Although notice that you could put it all inside the chain of if that determine the operation ... I have not done it so that both codes resemble each other a bit.

    
answered by 07.01.2018 / 18:19
source