Problem in using matrix keypad with one pin

I found a code for using matrix keypad with a single analog pin.

int thresholds[16] =  {930, 912, 894, 877, 852, 837, 822, 808, 787, 774, 761, 749, 731, 719, 708, 698};
char keypad[16] =  {'1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'};
void setup() { 
Serial.begin(9600);
}
void loop() { 
int value = analogRead(A0);
for (int i = 0; i < 16; i++)
{
if ( abs(value - thresholds[i]) < 5 )
{Serial.println(keypad[i]);
while (analogRead(A0) < 1000) {delay(100);}
}
}
}

Currently I am trying it on Tinkercad.
Problem is, after running it, it only detects the first press of any key, then it stops working or doesn’t show any further press. I couldn’t detect what is the problem going on.
Please help…
Thanks in advance.

My guess would be that the program is stuck in this while loop

      while (analogRead(A0) < 1000)
      {
        delay(100);
      }

Print the value of analogRead(A0) within the loop to prove it. Does the value ever go above 1000 ?

The value doesn’t go more than 930, so it always remains below 1000.
But if I change the code to this:

while (analogRead(A0) < 100) {delay(100);}

which means if I lower the value from 1000 to 100, it detects more button press, but if I press a button for single time, it detects that: ‘several time the button has been pressed’, so result looks like this:

I couldn’t figure out how precisely it could be done :frowning:

You need to arrange that the input pin is pulled HIGH when no button is pressed so that the value returned by analogRead() is above 1000

Can you please provide a link to where you got the code and circuit diagram ?

I followed this youtube video - HariFun #143 - How to read a 4x4 keypad using just one Arduino pin! - YouTube

Can you please provide a circuit diagram of your setup. A photo of a hand drawn circuit is all that is required. Make sure that it shows your actual circuit rather than what you meant to build

I am sharing the link of tinkercad, maybe it can help you :slight_smile:

I solved my problem, and the code is working fine.

int thresholds[16] =  {930, 912, 894, 877, 852, 837, 822, 808, 787, 774, 761, 749, 731, 719, 708, 698};
char keypad[16] =  {'1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'};
void setup() { 
Serial.begin(9600);
}
void loop() { 
int value = analogRead(A0);
for (int i = 0; i < 16; i++)
{
if ( abs(value - thresholds[i]) < 5 )
{Serial.print(keypad[i]);
while (analogRead(A0) > 0) {delay(100);}
}
}
}

Physically I haven’t tried this project. So I couldn’t share any picture or physical diagram of this project yet.
But I am pretty sure from the link you can get the idea and code and diagram also :slight_smile:
thank you :slight_smile:

Be aware some of your values will have to change in the real world because the voltage will not be exactly 5.0000 volts.

Paul