system
February 4, 2012, 10:51pm
1
I'm trying to capture multiple key values from a Keypad.
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
system
February 4, 2012, 11:35pm
2
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.
system
February 5, 2012, 12:16am
3
I was trying to limit the amount of code posted.
Here is all the 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
system
February 5, 2012, 12:17am
4
// Serial.println(digittogether);
Was not commented out - that was just me debugging since the post
This:
sprintf(digittogether, "%d%d", digitOneChar, digitTwoChar);
tells sprintf you're passing it two ints, but you're not. Try this:
sprintf(digittogether, "%s%s", digitOneChar, digitTwoChar);
system
February 5, 2012, 4:11pm
6
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!