Need help creating a simple addition calculator

I’m trying to create an adder. The user would have to enter two numbers on the keypad and their sum would be displayed on the LCD. For some reason it doesn’t work. Any advice would be much appreciated.

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

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


int Addition(int x, int y){
int z;
  z = x+y;
pinMode (backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write(z);  
}


const byte rows =4;
const byte cols =3;

char keys[rows][cols]={
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};

byte rowPins[rows] = {0,1,2,3};
byte colPins[cols] = {4,5,6};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
  
void setup() {

  
}
void loop(){

  int a;
  int b;
  
  if (a != NO_KEY) {
a = keypad.getKey();
    if (b != NO_KEY){
b = keypad.getKey();

  
  Addition(a,b);
  delay (3000);
    }
  }
  }

Please, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

Put your sketch between the code tags [code][/code]

Show us the actual wiring.

Do you have use hardware (Arduino board, lcd display, keypad) or only a simulation ? Simulations don't work.

  int a;
  int b;
  
  if (a != NO_KEY) {
a = keypad.getKey();
    if (b != NO_KEY){
b = keypad.getKey();

Since you don't put any data into "a" or "b" they will be random* data, and therefore there is no point in testing them for NO_KEY or anything else.


  • Strictly speaking, the data isn't random but undefined.