I have a code which prints an array as an integer after taking inputs from a 4x4 matrix keypad. A 5 digit number is displayed only after the 'D' key is pressed from the keypad. But what I want is that I must be able to see the digits I am entering, but the number must be accepted only after the 'D' key is pressed. Please suggest a way to do so. Following is the code.
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 6, 7, 8, 9 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 12, 11};
int i=0;
char count[6]; //this stores the user defined count
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
}
//lcd.clear();
void loop()
{
char key= kpd.getKey();
if(key!=NO_KEY) // Check for a valid key.
{
switch (key)
{
case 'D':
Serial.println();
count[6]='\0';
Serial.println("Count set to:");
for(i=0;i<6;i++)
{
Serial.print(count[i]);
}
break;
default:
count[i]=key;
i++;
}
}
}
Print the key each time you add one in the array or always put a trailing '0' after the current index and print the whole array as a c-string
careful with count[6]='\0';, index starts at 0 so your last entry is 5 not 6, you are overflowing your array and writing at a random space in memory here, will lead to disappointment
count[ i]=key; you should ensure i never gets above 4 if you want to save index 5 for the trailing '\0' but it's useless to put a '\0' if you use a for loop to print each char
You should probably have a separate index variable than i for position in array of the current char and then printing with a for loop. This way you won't print garbage if the user only entered 2 or 3 keys
yes so that you don't mess up the variable [color=blue]i[/color] that you use to decide at what index in the array the key the user entered is being stored (and fix the bugs listed above)
I have tried the following code now. But on serial monitor when i press 'A' I expect counter to be reset and then I go on to enter the 5 digit number. On pressing A i get the message set count. After entering the number I press D, which displays 'count set to'. But I am not getting the 5 digit number.
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 6, 7, 8, 9 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 12, 11};
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int i=0;
void setup()
{
Serial.begin(9600);
}
//lcd.clear();
void loop()
{
// user input array of 5 digits and nul character
static char count[6];
// variable to remember where in array we will store digit
static int x = 0;
char key= kpd.getKey();
if(key!=NO_KEY) // Check for a valid key.
{
switch (key)
{
case NO_KEY:
// nothing to do
break;
case 'A':
Serial.println("Set Count:");
// clear the current user input
memset(count, 0, sizeof(count));
// reset the counter
x = 0;
break;
case 'D':
Serial.println();
Serial.println("Count set to:");
for(i=0;i<5;i++)
{
Serial.print(count[i]);
}
break;
default:
// if not 5 characters yet
if (count < 5)
{
// add key to userinput array and increment counter
count[x++] = key;
}
break;
}
}
}
you probably need to only print from 0 to x-1 as the others are not used
i does not need to be a global any more, you can do for (int i = 0; i < x; i++) ...
when I press A to set count, I get on serial monitor Set Count: .So then I start entering a random 5 digit number. After that I press D expecting Count set to: 54321 for example. But instead I am getting nothing.
if(key!=NO_KEY) // Check for a valid key.
{
switch (key)
{
case NO_KEY:
// nothing to do
break;
If key is NOT NO_KEY, execute this switch statement. Why, then, do you have case for NO_KEY? If key IS NO_KEY, the switch statement won't be executed.
if (count < 5)
count is an array. It is silly to compare the address of the array to 5.
Of course, it is equally silly to call the array containing the characters count.
I suspect that you meant to compare x, which IS a count, to 5.
}
}
}
When it comes to dancing, I'll bet you have two left feet, too. Use Tools + Auto Format, so your code doesn't look like it was typed by a drunken monkey.
. this makes the code more readable, for example when you enter key instead of checking
if (x < 5)
you can do
if (x < entryMaxSize)
memset(count, 0, sizeof(count)); is not really necessary as x will tell you how many keys have been typed in. just set x to zero when you want to reset
you should get rid of //lcd.clear(); which is hanging as a comment outside any function
// variable to remember where in array we will store digit
static byte x = 0;
That variable is often called index.
What do you intend to do with the array called count? You can't pass it to atoi(), because atoi() expects a string, and count is not a string. It could be, with one more line of code...
My next step is to store the array of 5 digits into the internal EEPROM, but I also want count to be displayed as a 5 digit integer and not a string of 5 digits. I want to use the 'atoi' function but wont storing count as a string consume more memory?
GautamD:
I have made the changes. Is this correct now?
besides the other comments to get a "nice" c-string (null terminated), you could use 115200 bauds instead of 9600 for communication with your PC, that will be faster
remember though that your keypad has also * # A B C D so a passcode (if this is what you capture) could possibly not be a number. Also you need to decide if 00012 is the same password as 12 or as 0012... if you deal only with numbers then they will convert to the same thing.
you could also press ctrl-T in the IDE to nicely indent your code
My project involves storing a 5 digit count defined by the user using matrix keypad in the Arduino Uno's internal EEPROM. Later using a circuitry I will be counting IR pulses and when the pulses counted become equal to the set count I will stop a certain operation. So, the 5 digit count will only involve numbers from zero to nine. The maximum count that can be set by the user will be 99999.