Pages: [1]   Go Down
Author Topic: Multiple Key values from Keypad  (Read 327 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1]   Go Up
Jump to: