Go Down

Topic: Resistor based Keypad (Read 488 times) previous topic - next topic

LeChatQuiRit


Be gentle, I'm new to this.

So, I built a couple of resistor based keypad boards which worked well - no issues at all but really they were badly made, almost thrown together.

I decided it would be nice to learn to use Kicad and have some proper boards made (10 for £10 seemed like a bargain) and while they were being delivered I'd practice my soldering.



The one of the left works with no issues at all. The one on the right however, well I have some issues.

If I use one board it's fine, if I add a second I start to get keypresses from the first and second boards:

Code: [Select]
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 0 Original R value: 503 New R Value: 520 Key: 9
Keypad: 1 Original R value: 803 New R Value: 220 Key: 20
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 1 Original R value: 802 New R Value: 221 Key: 20
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 1 Original R value: 843 New R Value: 180 Key: 19
Keypad: 1 Original R value: 915 New R Value: 108 Key: 18
Keypad: 0 Original R value: 796 New R Value: 227 Key: 4
Keypad: 1 Original R value: 915 New R Value: 108 Key: 18
Keypad: 0 Original R value: 795 New R Value: 228 Key: 4
Keypad: 0 Original R value: 797 New R Value: 226 Key: 4
Keypad: 0 Original R value: 790 New R Value: 233 Key: 4



This didn't happen at all with my first two boards when using both at the same time. I've tested the resistor path and at each point after the resistors the increase is (approx) 10k. PCB Layout is below. (Ignore the cut off part at the bottom, that was for V2 with LED control. Also ignore jp5v1 and any connections - V2 also)

R1 -> R17 = 10K
R18 = 1M




... and the code to show what I'm doing.


Code: [Select]


/*

Test Keypads

*/

#define countof(a) (sizeof(a) / sizeof(a[0]))


unsigned int keyp[] = {A0,A1}; //Analog input to read

unsigned int variance=20; // How much leeway on the resistor values.

// Keypad resistance values

int resistors[] = {55,114,170,230,285,345,400,460,516,575,630,690,755,820,880,950};


unsigned int fnows = 0;
unsigned int lastKey = 0;
unsigned int lastReading = 0 ;
int lastKeypad = -1;

void setup() {
 
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);

    Serial.begin(115200);
    while (!Serial); // wait for serial attach   
}

void loop() {

      for (int i=0;i< countof(keyp);i++) {

        int reading = 0;
       
        delay(30);
       
        reading = analogRead(keyp[i]);

         if (reading < 1000) {
          if ((reading != lastReading) && lastKeypad != i) {

            lastKeypad = i;
            lastReading = reading;

            int keypress =  testkey(reading,i);
 
            if (keypress >0 && lastKey != keypress) {
              Serial.print("Keypad: \t");
              Serial.print(i);
              Serial.print("\tOriginal R value: \t");
              Serial.print(reading);
              Serial.print("\tNew R Value:\t");
              Serial.print(1023 - reading);             
              Serial.print("\tKey: \t");
              Serial.println(keypress);
             
              lastKey = keypress;
            }
          }
      } else {
               lastReading = reading;
     
      }
   }

// Holy fudge Batman.
    if (fnows >= 20) {
        lastKey = 0;
        lastKeypad = -1;
        fnows = 0;
    } else {
        fnows++;
    }
}


int testkey(int resistanceValue, int keypad) {

  int kppos = keypad * 16;
 
  unsigned int expectedResistorValue = 0;
 
  // flip it
  resistanceValue = 1023 - resistanceValue;

  if (resistanceValue == 0 )
      return 0;
 
  for (int b=0;b<16;b++) {

    expectedResistorValue=resistors[b];

    if ( resistanceValue >= expectedResistorValue-(variance/2)  && resistanceValue <= expectedResistorValue+(variance/2) ) {
      kppos=kppos+(b+1);
      return kppos;
    }
  }
 
  return 0;
}




Other than "don't use a resistor keypad", can anyone see what might be causing a problem?


Thanks



In before:

1) Don't use a resistor keypad
2) Why is everything spaced so far apart





PaulRB


LeChatQuiRit

Sure, it's pretty straight forward..




PaulRB

Analog pin A1 is not shown...

PerryBebbington

Quote
If I use one board it's fine, if I add a second I start to get keypresses from the first and second boards:
If you mean that key presses from the 2 boards get mixed up then it's because your code does not separate them. You have an array to read the 2 separate inputs but after that you only have variables to hold one set of results, not the results from the 2 keypads.

LeChatQuiRit

#5
Jul 31, 2020, 10:50 pm Last Edit: Jul 31, 2020, 10:51 pm by LeChatQuiRit
Analog pin A1 is not shown...
It's the same as A0 - The schematic is for a single board. All boards are identical. The A0 on the scematic basically means analog pin.




If you mean that key presses from the 2 boards get mixed up then it's because your code does not separate them. You have an array to read the 2 separate inputs but after that you only have variables to hold one set of results, not the results from the 2 keypads.
No sure what you mean. each keypad is read in turn. The first will return a value from 1 -> 16 the second will return a number from 17 -> 32 etc.


Specifically:


Code: [Select]
     int keypress =  testkey(reading,i);

.
.
.
.
int testkey(int resistanceValue, int keypad) {

  int kppos = keypad * 16;
.
.
.
.
.

    if ( resistanceValue >= expectedResistorValue-(variance/2)  && resistanceValue <= expectedResistorValue+(variance/2) ) {
      kppos=kppos+(b+1);
 
.
.
.

Wawa

So, I built a couple of resistor based keypad boards which worked well - no issues at all...
You could have issues over time with this setup.
Cheap audio gear used this principle in the eighties, and this was quickly abandoned because (small tact) switches get bad (resistive) over time.
Better to use a port expander, with the switches in a matrix.
Leo..

PerryBebbington

#7
Aug 01, 2020, 09:40 am Last Edit: Aug 01, 2020, 11:13 am by PerryBebbington
Quote
No sure what you mean. each keypad is read in turn. The first will return a value from 1 -> 16 the second will return a number from 17 -> 32 etc.
First, when it comes to reading other people's code I am not the best.

Code: [Select]
unsigned int keyp[] = {A0,A1}; //Analog input to read
Good start, 2 keypads, 2 inputs in an array, easy to know which is which.

Code: [Select]
      for (int i=0;i< countof(keyp);i++) {
So you read 1 keypad then go round and read the other one.

Code: [Select]
            lastReading = reading;
You have the variable lastReading, just one of them. Which keypad does it belong to? If you are reading 2 keypads alternately then lastReading will contain the value from the other keypad, is that what you planned?

After that I stopped trying to make sense of your code. If the above is what you planned and it fits into your logic of reading the 2 keypads then fine, but it's not what I would do or have ever seen on any working code that deals with 2 or more instances of the same thing.

PaulRB

Code: [Select]
   if ((reading != lastReading) && ...
I wonder if this could be part of the problem. There's always some noise in the analog inputs, as you know (I saw the "variance" checks in your code), so checking for the exact same reading feels wrong.

Paul__B

You could have issues over time with this setup.
Cheap audio gear used this principle in the eighties, and this was quickly abandoned because (small tact) switches get bad (resistive) over time.
You bet!  MY MP3 players died.

The same thing happened(/ happens?) to video monitors.  :smiley-roll-sweat:

Was it really abandoned or does this (design failure) still happen?  :smiley-eek:

LeChatQuiRit

#10
Aug 01, 2020, 04:59 pm Last Edit: Aug 01, 2020, 05:22 pm by LeChatQuiRit
Code: [Select]
    if ((reading != lastReading) && ...
I wonder if this could be part of the problem. There's always some noise in the analog inputs, as you know (I saw the "variance" checks in your code), so checking for the exact same reading feels wrong.
Yes you're right on the variance, I agree but I don't think it's the problem. If I reconnect the two original boards I don't get the problem. My problem is having one key pressed results in values for both boards  - So below, I'm pressing key "9" on board 1 (Keypad 0) and if I hold it down, I'm getting random key "20" presses on board 2 (Keypad 1)


Code: [Select]

Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 0 Original R value: 503 New R Value: 520 Key: 9
Keypad: 1 Original R value: 803 New R Value: 220 Key: 20
Keypad: 0 Original R value: 502 New R Value: 521 Key: 9
Keypad: 1 Original R value: 802 New R Value: 221 Key: 20



It makes me think that it's noise, but why would that not also appear of the rough-n-ready boards I made? A short in the board design?




Code: [Select]
            lastReading = reading;
You have the variable lastReading, just one of them. Which keypad does it belong to? If you are reading 2 keypads alternately then lastReading will contain the value from the other keypad, is that what you planned?
That only prevents the code that follows from executing if a key is pressed from the same keypad as last time, before the lastKey and lastKeypad are reset. Removing it make zero difference to the outcome.


Wawa

Was it really abandoned or does this (design failure) still happen?  :smiley-eek:
The newer generation designers might not know about the problems of the past.
Or the bosses just want that build-in obsolescence.
Leo..

PerryBebbington

You asked for help but have rejected both PaulRB's and my suggestions. I have nothing else to offer at this point. Good luck.

LeChatQuiRit



I have pointed out that removing the code you didn't like has zero effect and is not a solution.

That's not rejecting your suggestion, it's eliminating it as a solution.



Paul_KD7HB

Some questions about your design. What material are you using to make the contacts with the circuit board pads? Notice the board that works has bare copper. The one with problems has tin coating on the copper. Perhaps scrub the tin really well.

The size of the contacts seems to be extremely small based on the keypads I have seen in the past. Those had serpentine traces from both sides of the connection that provided a huge area for the contact.

The keys from years ago used carbon filled conductive foam. The foam would die from ozone and probably other stuff in the air.

Paul

Go Up