Hoi Guys,
i am using this code to read an 4x4 keypad matrix, and this works fine, but trying to control 2 LEDs with that fails.
Like to Switch one lED on when pressing 8, and switch it off when pressing 9
If i i.e. press 8, the 8 appears in the serial monitor but has no effect to the LED, the same with the 9...
only the LED1 will switch when ever i press the depending buttons (4 and 5), BUT when i press 9 the LED2 switches, but pressing 9 has no effect....4 and 5 is working
OK I did a quick Excel Spreadsheet to calculate the analog values expected from each key. Assuming the forward voltage on your diodes as being 0.6v I get these values
901,785,682,620,540,496,453,425,365,344,323,308,281,269,256,246
So then taking the midway point between each of those to use as thresholds I come up with the following as a replacement for your getKeypad
(although it returns a char NOT an integer)
Ideally you should just do a test first to see what your ACTUAL analog readings are for each key and adjust accordingly.
//NOTE THIS RETURNS A CHAR NOT AN INTEGER
//although it still returns -1 if no key is being pressed.
char getKeypad()
{
int threshold[16]={843,733,651,580,518,474,439,395,354,333,315,294,275,262,251,123};
char chars[17]="123A456B789C*0#D";
int v=analogRead (A0);
for(int n=0;n<16;n++)
{
if (v > threshold[n])
return chars[n];
}
return -1;
}