#include <Keypad.h>
const byte ROWS = 4;
// four rows const byte
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}};
byte rowPins[ROWS] = {5, 4, 3, 2}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, A2, A1, A0}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int numberArray[10]; // array for storing input numbers from keypad v
void setup()
{
Serial.begin(9600); // start serial connection for debugging/testing purposes
}
void loop()
{
char keypressed = keypad.getKey();
// read the pressed key from the keypad
if (keypressed != NO_KEY)
{ // if a valid key is pressed
if (keypressed >= '0' && keypressed <= '9')
{
// check if it is a number between 0 and 9
int numpressed = (int)keypressed - 48; // convert char to int by subtracting 48
Serial.println(numpressed); // print out value of numpressed for debugging/testing purposes
static int i; // declare and initialize static variable i to count number of times a valid number is entered
numberArray[i] = numpressed; // store number in array at index i
i++; // increment i
if (i == 10)
{ // if all 4 numbers are entered
i = 0;
// reset counter i to 0
Serial.println("All numbers entered"); // print out message for debugging/testing purposes
}
}
}
}