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);
}
}
}
}