@OP
1. For some reasons, your program gives the following result when executed in my NANO+LCD+Keypad system.
Press the button 1 for twice and then the LCD shows: 1Z1 = 98.
2. To show the following message on the LCD, I have corrected/adjusted your posted program
as listed beneath.
Press 1, LCD shows: 1+
Press 1 again, LCD show: 1+ 1 = 98.
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <stdio.h> //you must include this library to get the atoi function
#include <stdlib.h>
const byte rows = 4;
const byte cols = 4;
byte rowPins[rows] = {9, 8, 7, 6};
byte colPins[cols] = {5, 4, 3, 2};
char keys[rows][cols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
char a;
char b;
int charCounter = 0;
bool flag1 = false;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop()
{
a = myKeypad.getKey();
if (a != NO_KEY)
{
if (flag1 == false)
{
lcd.print(a);
lcd.print("+"); //LCD shows: 1 +
charCounter++;
if (charCounter == 1)
{
flag1 = true;
}
}
else
{
b = a;
lcd.print(b);
charCounter++;
if (charCounter == 2)
{
lcd.print("=");
int sum = Addition(a, b);
lcd.print(sum, DEC);
charCounter = 0;
}
}
}
}
int Addition(int a, int b)
{
int z;
z = a + b; // z is now the integer sum
return (z);
}
3. Let us investigate your codes (corrected/adjusted program),
why the program does produce: 1 + 1 = 98 and not 1 + 1 = 2.
....will continue after sometimes!
(1) Let us note that when we press 1 from the keypad, this bit pattern 00110001 (0x31 in hex format) is stored in 'character type a'. 00110001 (0x31) is called the ASCII coded value for the digit/numeral 1.
(2) Next time, the same bit pattern 00110001 (0x31) got stored in variable b when we entered 1 from the Keypad.
(3) Now we havee: a = 0x31 and b = 0x31.
(4) To get the result of a + b, we have called the following sub-program:
int sum = Addition (a, b);
int Addition(int a, int b)
{
int z;
z = a + b;
return(z);
}
The sub-program named Addition(int a, int b) has received the values of a (0x31) and b (0x31) and then added them to produce z = a + b = 0x31 + 0x31 = 0x62 which is 98 (6x 16 + 2x1) in decimal, which we have passed to the calling program via variable sum. After that we have executed this instruction: lcd.print(sum, DEC); as a result, 98 has appeared on the LCD. Everything is fine up to now except that we want to see 2 on the LCD.
4. How can we get final result of 2 from the apparent resulat of 0x62 (98 in decimal)?
(1) We must not allow the sub-program (Addition()) to add 0x31 and 0x31; we need to manipulate the received data in such a way so that the addition is done on 0x01 (= a) and 0x01 (=b).
(2) 0x01 for a can be obtained from 0x31 by subtracting 0x30. Thet is to say:
==> newA = 0x31 - 0x30 = 0x01
==> newA = received a - 0x30
==> newA = received a - '0' //'0' will be coded into 0x01 = 00000001
Similarly,
newB = received b - 0x30
==> newB = 0x31 - 0x30 = 0x01
==> newB = received b - '0'
(3) Finally, the sub-program (Addition()) is to be modified to take the following form:
int Addition(int a, int b)
{
int z;
int newA = a - '0'; //0x31 - 0x30 = 0x01
int newB = b - '0';
z = newA + newB; //z is now the integer sum
return (z);
}
5. The final program (it just works for: 1+ 1 = 2; 2+2=4;, ...) that will yield the expected result is:
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <stdio.h> //you must include this library to get the atoi function
#include <stdlib.h>
const byte rows = 4;
const byte cols = 4;
byte rowPins[rows] = {9, 8, 7, 6};
byte colPins[cols] = {5, 4, 3, 2};
char keys[rows][cols] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
char a;
char b;
int charCounter = 0;
bool flag1 = false;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop()
{
a = myKeypad.getKey();
if (a != NO_KEY)
{
if (flag1 == false)
{
lcd.print(a);
lcd.print("+"); //LCD shows: 1 +
charCounter++;
if (charCounter == 1)
{
flag1 = true;
}
}
else
{
b = a;
lcd.print(b);
charCounter++;
if (charCounter == 2)
{
lcd.print("=");
int sum = Addition(a, b);
lcd.print(sum, DEC);
charCounter = 0;
}
}
}
}
int Addition(int a, int b)
{
int z;
int newA = a - '0'; //0x31 - 0x30 = 0x01
int newB = b - '0';
z = newA + newB; //z is now the integer sum
return (z);
}