#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int i=0;
int s;
char op;
float o;
int nr1[4];
int nr2[4];
float ch1=0;
float ch2=0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the Rows of the keypad pin 8, 7, 6, 5 respectively
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
lcd.init();
lcd.init();
lcd.backlight();
lcd.clear();
Serial.begin(9600);
}
void loop(){
while(nr1[i-1]<10){
while(!nr1[i]){
nr1[i] = keypad.getKey()-'0';
}
i++;
}
s=i;
for(int f=0; f<i; f++){
ch1+=nr1[f]*pow(10, i-1-f);
}
lcd.print(ch1);
while(!op){
op = keypad.getKey();
}
if(op=='A'){
lcd.print("+");
}else if(op=='B'){
lcd.print("-");
}else if(op=='C'){
lcd.print("*");
}else if(op=='D'){
lcd.print("/");
}else{
lcd.clear();
lcd.print(":(");
}
while(!ch2){
ch2 = keypad.getKey();
}
lcd.print(ch2);
lcd.print("=");
if(op=='A'){
o=ch1+ch2;
Serial.println(o);
lcd.print(o);
}else if(op=='B'){
o=ch1-ch2;
Serial.println(o);
lcd.print(o);
}else if(op=='C'){
o=ch1*ch2;
Serial.println(o);
lcd.print(o);
}else if(op=='D'){
o=ch1/ch2;
Serial.println(o);
lcd.print(o);
}
while(!keypad.getKey()){};
}
I'm trying to make a calculator. When I upload this code -532963.87 appears an the lcd. Why is that? How do I fix this?