Save value from keypad to empty array

Hi, there. I get some problems when I do the programming.

This is the keypad I used as input.
For the following code, I would like to store values '1' and '2' into the empty array when I pressed '1' and '2' at the keypad.

The current problem I had is, the value both '1' and '2' not coming to the array. And the value of the arrayIndex always give me "1".

if(customKey == '*')
  {      
    int theArray[4] = {0}; 
    byte arrayIndex = 0;
    if(customKey == '1' && customKey == '2')
    {
      theArray[arrayIndex] = customKey; 
      arrayIndex++;
    }
    else
    {
    Serial.println(arrayIndex);
    }
  }

Here is the result. When I press 'star' it shows 'star' and '0'. After I press five '1', I pressed '7' to see whether the value of arrayIndex changed or not. But the code is not working as expected. Can you please explain why is not working and what causes the problem?

image

Maybe you meant OR (||) and not AND (&&) ? ... customKey can only have one value at a time.

Yes, mate. That is the problem. And I am sure I want the value be saved in array when i pressed "1" and "2".

How are you reading the keypad? Post all your code.

If you are using a library does it support returning multiple values if more than 1 key is pressed? It obviously can't return multiple values in a single variable (customKey).

I will put section of how read keypad because whole code over 300 lines

#include<Keypad.h>
// Array to represent keys on keypad
char Keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Connections to Arduino
byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {10, 11, 12, 13};
 
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

That doesn't help. Post ALL your code. It's make it easier to debug.

Like I said... if you are using getKey to get the key pressed then how do you expect it to return 2 keys?

if(customKey == '*')
  {      
    int theArray[4] = {0}; 
    byte arrayIndex = 0;
    if(customKey == '1' && customKey == '2')
    {
      theArray[arrayIndex] = customKey; 
      arrayIndex++;
    }
    else
    {
    Serial.println(arrayIndex);
    }
  }

There are several problems with the code.

The first if statement tests for customKey equal to * , and the remainder of the code is only executed when this is true. The value of customKey does not change until you read the keypad again, so it can never be 1, 2, or any other value than *

You are testing for customKey being equal to 1 and 2 at the same time, which is impossible.

theArray and arrayIndex are declared as local variables, that will cease to exist at the end of the if statement.

This is the equivalent code that results when the parts that can never be executed are removed:

if(customKey == '*')
  {      
    Serial.println(0);
  }
#include<Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char Keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Connections to Arduino
byte rowPins[ROWS] = {6, 7, 8, 9};
byte colPins[COLS] = {10, 11, 12, 13};
 
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
void setup()
{
  Serial.begin(9600);
}
void loop()
{                                   
    char customKey = customKeypad.getKey();
  if (customKey) 
  {
    Serial.println(customKey);
  }

  if(customKey == '*')
  {      
    int theArray[4] = {0}; 
    byte arrayIndex = 0;

    if(customKey == '1' && customKey == '2')
    {
      theArray[arrayIndex] = customKey; 
      arrayIndex++;
    }
    else
    {
    Serial.println(arrayIndex);
    }
  }

Here is all code relevant to this problem.

Did you read @david_2018's post? It identifies a number of issues. Including one that I also pointed out... but you seem to be ignoring.

getKey returns ONE key value.

... in what universe will this ever be true?

This is how you do it:

#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char Keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

// Connections to Arduino
byte rowPins[ROWS] = { 6, 7, 8, 9 };
byte colPins[COLS] = { 10, 11, 12, 13 };

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

int theArray[4] = { 0 };
byte arrayIndex = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  char customKey = customKeypad.getKey();

  if (customKey) {
    Serial.println(customKey);
  }

  if (customKey == '1' || customKey == '2') {
    if (arrayIndex < 4) {
      theArray[arrayIndex] = customKey - '0';
      arrayIndex++;
    }
  }

  if (customKey == '*') {
    for (int i = 0; i < arrayIndex; i++)
      Serial.println(theArray[i]);
    arrayIndex = 0;
  }
}

hello, John.

May I ask what is the meaning of the '0' in the following code

theArray[arrayIndex] = customKey - '0';

It is the character code for the digit zero. The characters '0' through '9' are together in order in ASCII so subtracting the character code for zero from a digit character ('0' through '9') gives you the digit's decimal value. '0' - '0' is 0, '1' - '0' is 1, '2' - '0' is 2, etc.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.