Go Down

Topic: Multiple Key values from Keypad (Read 344 times) previous topic - next topic

Bucky99

I'm trying to capture multiple key values from a Keypad.

Code: [Select]
void loop(){
  char key = keypad.getKey();
  char testStr[10];
  int TestRnd;
  char TotalNum[5];
 
 if (key != NO_KEY){
    Serial.println(key);

  TestRnd = i;
  sprintf(testStr, "i = %d", TestRnd);
  Serial.println(testStr);
     
      switch (i){
         
         case 1:
           sprintf(digitOneChar, "%d", key-48); i++;
           Serial.println("First Digit");
           Serial.println(digitOneChar);

           break;

         case 2:
           
           sprintf(digitTwoChar, "%d", key-48); i++;
           sprintf(digittogether, "%d%d", digitOneChar, digitTwoChar);

           Serial.println(digitOneChar);
           Serial.println(digitTwoChar);
           Serial.println(digittogether);
             i = 1; //reset counter
           
           break;


In this case when I press 1 and then 2 I get the following.

1
2
332334

The digittogether returns 332334. What the heck am I missing? Thank you

PaulS

Quote
In this case when I press 1 and then 2 I get the following.

1
2
332334

No, you do not.
After printing one unidentified value (why is it unidentified?), you print a line starting with i = .  Your claimed output does not show this line.

Your code snippet does not define how the stuff that the keypad instance relies on is defined, so we have no idea whether that is right.

Post all of your code if you are serious about needing help.

Bucky99

I was trying to limit the amount of code posted.
Here is all the code.

Code: [Select]
#include <Keypad.h>

int i;
char digitOneChar[2];
char digitTwoChar[2];
char digitThreeChar[2];
char digittogether[4];

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {14, 15, 16}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  i = 1;
}

void loop(){
  char key = keypad.getKey();
  char testStr[10];
  int TestRnd;
  char TotalNum[5];
 
 if (key != NO_KEY){
    Serial.println(key);

  TestRnd = i;
  sprintf(testStr, "i = %d", TestRnd);
  Serial.println(testStr);
     
      switch (i){
         
         case 1:
           sprintf(digitOneChar, "%d", key-48); i++;
           Serial.println("First Digit");
           Serial.println(digitOneChar);

           break;

         case 2:
           
           sprintf(digitTwoChar, "%d", key-48); i++;
           sprintf(digittogether, "%d%d", digitOneChar, digitTwoChar);

           Serial.println(digitOneChar);
           Serial.println(digitTwoChar);
//            Serial.println(digittogether);
             i = 1; //reset counter
           
           break;
         case 3:
           
delay(1000);

   }
 }
}


Thank you

Bucky99

//            Serial.println(digittogether);
Was not commented out - that was just me debugging since the post

wildbill

This:
Code: [Select]
sprintf(digittogether, "%d%d", digitOneChar, digitTwoChar);
tells sprintf you're passing it two ints, but you're not. Try this:
Code: [Select]
sprintf(digittogether, "%s%s", digitOneChar, digitTwoChar);

Bucky99

THANK YOU - I knew it was something that "I" was missing. The %s, %d, %i where a mistory to me. I saw them in code and didn't know what they were called so I just used what others did. Now that you pointed out a difference I get the meaning. And it was enough for me to google and learn the rest. Thank you!

Go Up