Raymond Ogunjimi Raymond Ogunjimi - 4 months ago 29
C Question

How to program Arduino to take and save two keypad inputs in separate variables?

Trying to make a simple Arduino adder but I am having trouble with the programming. I am not sure where I made a mistake. Any help would be much appreciated.

The user would have to enter two numbers n a keypad and their sum would be displayed on an LCD. Each of the two numbers entered by the user are stored in their variables respectively. The problem is that after entering the first number the program automatically plugs in 0 (since no key is pressed at that moment) for the second variable. I have tried 'if', 'while' and 'do while' statements to eliminate NO_KEY but none of these seem to be working.

The question is if there is a way to store a number entered by the user in a variable and not let the program overwrite it with 0 because no key is being pressed at that moment. Or also, to not let the Arduino receive any more inputs from the keypad after a key is pressed (maybe disable the keypad somehow)...

If you are willing to spend some time on looking over the code, the website linked here contains both the code and the whole lab setup.

#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;
lcd.setCursor(8,0);
lcd.write(z);
}

void setup() {

}

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 loop(){

int a;
int b;



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


do {
b = keypad.getKey();
}while (b == NO_KEY);


pinMode (backLight, OUTPUT);
digitalWrite(backLight, HIGH);
lcd.begin(16,2);
lcd.clear();

lcd.setCursor(0,0);
lcd.write(a);

lcd.setCursor(2,0);
lcd.write("+");

lcd.setCursor(4,0);
lcd.write(b);

lcd.setCursor(6,0);
lcd.write("=");

Addition(a,b);
delay(10000);

}

}

Answer

You have a section of code like this:

do {
    b = keypad.getKey();
} while (b = NO_KEY);

In the while condition, it looks like you're assigning NO_KEY to b with (b = NO_KEY), instead of checking equality with (b == NO_KEY). The assignment returns true, hence the loop won't exit. It should look like this:

do {
    b = keypad.getKey();
} while (b == NO_KEY);

For the Addition problem, your keymap contains chars rather than ints. So a and b should be of type char, and you can use atoi() to convert them to ints:

char a;
char b;

// ...assign values to a and b as you already do...

Addition(atoi(a), atoi(b)); // use atoi() (ascii to int) to pass integer values to Addition

Alternatively, you could change your keymap to be:

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

}; and used lcd.print(a, DEC) to print the values. Then you'd have no need of atoi() and it'd be cleaner in my opinion. Addition function:

Addition(int x, int y) {
    int z;
    z = x+y; // z is now the integer sum
    lcd.setCursor(8,0);
    lcd.print(z, DEC); // print the decimal value of z (default is ascii)
}