Keypad, 3 key segments

Hello, I would like to be able to use the current keypad library here but be able to enter 3 keys. The current code only recognizes one key with one variable for that key. I need three keys recognized and three variables for those keys i.e. keyA, keyB, keyC. Does anyone know how to do this? Does my explanation make sense? Thank you.

Example: Currently, if I pressed 1,2 and 3 on the keypad the variable "key" would only be equal to 3.

I would like to be able to enter 1,2,3, and 4 on the keypad and have Serial.print show 234 with variable "keyA" = 2, "keyB" = 3 and "keyC" = 4.

I have figured this out so far:

void loop() { 

char key[2]; 

key[0] = kpd.get_key();
    if(key[0] != '\0')
    {
        Serial.begin (9600);  
        Serial.print("-key1-");
     }
key[1] = kpd.get_key();
    if(key[1] != '\0')
    {
        Serial.begin (9600);  
        Serial.print("-key2-");
        
    }
key[2] = kpd.get_key();
    if(key[2] != '\0')
    {
        Serial.begin (9600);  
        Serial.print("-key-3");
       
    }
     
                }

But for some reason it jumps randomly. How do I strictly control the order?

But for some reason it jumps randomly.

Yes it will. Each time round the loop you are looking for a key press and when a key is pressed the program can be looking at any one of those three sections.

Try using a structure to your program that waits until a key is pressed then reading it into where you want it.

Thank you that makes perfect sense. I understand what you are saying but unfortunately I am unaware of how to accomplish this with code. Heres how I interpret what you are saying:

-Wait until key is pressed -Define this initial key as variable1 -Then get second key that is pressed -Define as variable2 -Then get third key that is pressed -Define as variable -On forth key start over, overwriting exiting variables.

Could someone steer me in the correct direction?

do{
key[1] = kpd.get_key();
} while (key[1] == '\0');

Will hold until a key is pressed. Enough of a clue? ;)

Thanks Grumpy Mike for the help. I've tried implementing the code you suggested to no avail.

char key[2];

key[0] = kpd.get_key();
    if(key[0] != '\0')
    {
          Serial.begin (9600);
        Serial.print("-key1-");
   do{
key[1] = kpd.get_key();
          Serial.begin (9600);
        Serial.print("-key2-");
   }
while (key[1] == '\0');
    }
     }

First, saying: "char key[2];" means it will show key[0], key[1], key[2] correct?

Second, when I use the above code I get: -key?--key?--key?--key?--key?--key?-

Third, when I change it to this:

key[1] = kpd.get_key();
          Serial.begin (9600);
        Serial.print("-key2-");
   }
while (key[1] != '\0');
    }
     }

I get: -key?--key2

I understand why I am getting that. I'm just not sure how to use this code to achieve the desired goal. :-[

ok im not really sure what you want here (I didn't get what key? meant in your explanation), but here is my go at it. From my understanding you want keyC to hold your last press, keyB to hold the second last, and keyA to hold the third last, right?

Here is the code to do this:

void loop(){
  char keyA, keyB, keyC, keyTemp;  //varaibles
  do{                                              //do nothing till you recieve input
    keyTemp = kpd.get_key();
  } while (key[1] == '\0');
  
  keyA=keyB;                                 //current keyA = last keyB
  keyB=keyC;                                //current keyB = last keyC
  keyC=keyTemp;                          //current keyC = latest press
  
}

Hope this helps!

Thank you darudude and correct that is what I wish to accomplish. However, when I use the following code I get the response below:

void loop(){
  
  char keyA, keyB, keyC, keyTemp;  //varaibles
  do{                                              //do nothing till you recieve input
    keyTemp = kpd.get_key();
  } while (keyTemp == '\0');

  keyA=keyB;                                 //current keyA = last keyB
  keyB=keyC;                                //current keyB = last keyC
  keyC=keyTemp;                          //current keyC = latest press

Serial.begin(9600);
Serial.print("Key1 =");
Serial.print(keyA);
Serial.print("\n");
Serial.print("Key2 =");
Serial.print(keyB);
Serial.print("\n");
Serial.print("Key3 =");
Serial.print(keyC);
Serial.print("\n");

}

Key1 = [ch9633] Key2 = [ch9633] Key3 = 8 (results from 5th key press and upward)

Please note that I changed: while (key[1] == '\0'); to while (keyTemp == '\0');

do{
key[1] = kpd.get_key();
} while (key[1] == '\0')

do{
key[2] = kpd.get_key();
} while (key[2] == '\0')

do{
key[3] = kpd.get_key();
} while (key[3] == '\0')

Got it yet?