Shop OBEX P1 Docs P2 Docs Learn Events
Puzzle: 5 Switches 4pins available. — Parallax Forums

Puzzle: 5 Switches 4pins available.

tonyp12tonyp12 Posts: 1,951
edited 2013-10-24 23:15 in General Discussion
My 6P add-on board standard only have 4 mcu pins, though I have an idea how to solve this want to see what others come up with.
You can not use HC165 etc, can not use analog output.

6 points: Only one Switch will even be pressed at one time
8 points:Two Switches my be pressed at the same time, two neighboring switches or 1 switch plus center switch(5)
10 points: Two neighboring Switches plus center(5) can be pressed at the same time.

5switch.gif?psid=15switchRender.jpg?psid=1

diptrace schematic:https://skydrive.live.com/redir?resid=6D7787B33D967B1A!4006
The switch is a JS1300AQ

Comments

  • JordanCClarkJordanCClark Posts: 198
    edited 2013-10-22 06:23
    Charlieplexing was my first thought. Okay, really my third. Perhaps not fully fleshed out, but I'd think that the mcu would likely have a debounce routine going on.
    10-22-2013 9-00-59 AM.png
    744 x 644 - 8K
  • Martin_HMartin_H Posts: 4,051
    edited 2013-10-22 08:02
    Do you consider it cheating to use a 4x4 keypad with resistors to create a separate analog output for each column that controls the frequency of a 555 timer? Feed the output of the 4 555 timers into 4 pins and use pulsein to get the key pressed in each column?
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-10-22 09:32
    A 555 timer would fall in the HC165 category, so it's a no.
    Bonus1: If you can make it short circuit safe, if user by mistake put to many pins as output with 3.3v+gnd mixed, Jordan would only have to add two 1K resistors on two pins.
    Bonus2: Not using diodes, they cost more than resistors but if you have to use them they count as 2 parts as lower part count is a bonus too.
    Bonus3: One single hardware de-bounce that handles all 5 switches and preferred way is that a closed switch=high signal.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-22 12:34
    Why no analog? ... You'd just need two I/O pins (One I/O if you are tricky) and a few binary weighed resistors with a single capacitor. That would allow you to read any key press. All five if you wanted.
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-10-22 12:58
    No analog due to being generic add-on board, it should work on most MCU.
    No need to go below 4 pins, though 3 and one for a led to show feedback would be cool.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-22 13:12
    My thought was a Hybrid between RC-Time and a Sigma Delta ADC.


    The idea is that you read the I/O, and if it's Zero, you make the I/O an OUTPUT; HIGH, and then immediately back to an INPUT. If the I/O reads as a One, then leave the I/O as an INPUT.

    Note1: The Key is to keep the above function in a deterministic loop.
    Note2: The 4.7k resistor assumes that the I/O threshold is 1/2 Vdd


    By monitoring the state of the input, i.e. how many 1's versus 0's , you should have a ratio-metric relationship that corresponds to the button press combination.
            4.7K       470k
    I/O >---/\/\---o---/\/\--------->GND
                   |
                   |   220k   SW1
                   o---/\/\---o o--->GND
                   |
                   |   100k   SW2
                   o---/\/\---o o--->GND
                   |
                   |    47k   SW3
                   o---/\/\---o o--->GND
                   |
                   |    22k   SW4
                   o---/\/\---o o--->GND
                   |
                   |    10k   SW5
                   o---/\/\---o o--->GND
                   |
                   |    1uF
                   o----||---------->GND
                       +
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-10-22 17:43
    Beau,

    You might as well use 1% resistor values, since you'll need 1% resistors. 1/32 < 1/20, after all. :)

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-22 20:32
    "You might as well use 1% resistor values, since you'll need 1% resistors." - Not for 5 bit resolution ... On the software side of things your just looking for 32 different levels. As long as the resistors that you use are slightly greater and roughly double each time you step up you should be ok. Either way you would still need to calibrate to some degree, but if you look at the values as a ratio, then even temperature variations should track proportionally.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-10-22 21:03
    Beau,

    You're only getting by with because your ratios are greater than two. If they were equal to two (i.e. 10K, 20K, 40K, 80K, 160K), you would need 1% resistors. Consider the case where the 10K resistor is 5% too high, and the others are 5% too low. The resistance of the 10K, then, is 10.5K, and the paralleled resistance of the others is 10.133K. Since 10.1333 < 10.5, the output will not be monotonic going from %01111 to %10000.

    -Phil
  • bee_manbee_man Posts: 109
    edited 2013-10-22 21:16
    Beau has the best answer and that is how I would do it, or possibly a resistor ladder.

    If you limit it to one key press at a time you could wire each switch to more than one pin and read the value in binary;

    switch one - pin one

    switch two - pin two

    switch three - pin one and two (double pole)

    switch four - pin three

    switch five - pin one and three (double pole)

    that leaves your 4th pin free for feed back.

    Oh! And if you act now I'll throw in two more switches for free,

    switch six - pin two and three (double pole)

    switch seven - pin one, two, and three (triple pole?)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-10-22 21:22
    One problem with a solution that requires RC timing is, "What happens when a switch changes state during the timing sequence?" Do you just keep testing until you get two identical results in a row?

    -Phil
  • StephenMooreStephenMoore Posts: 188
    edited 2013-10-22 21:41
    6 pOINTS.JPG


    Regards,

    sm
    550 x 423 - 35K
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-10-22 21:47
    As Switch 1 and 3 can never be pressed at the same time (same with 2 and 4)
    5SwitchSolution.jpg?psid=1

    I think I can do it with two 4x resistor arrays.
    S1 = J2
    S2 = J1
    S3 = J3 (after J2 as output)
    S4 = J4 (after J1 as output)
    S5 = J3 (after J1 as output)

    So you start with 4 inputs, test states.
    Set J1 as output, test state on J2,J3,J4
    Set J2 as output, test state on J1,J3,J4

    No short circuit possible, as Setting for example J2 high while S1 is pressed just connects Vcc to Vcc


    May have to set J3 as ground to find some triple multi-press, not sure yet that is why I put 1K on S2 pull-up instead
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-22 22:23
    Since you have 4 pins, you could make all but one of them use 1-bit resolution (standard typical switch input) .... only one oddball would be 2- bit resolution. The odd-ball would follow the same rule mentioned above with the 5-bit resolution.
            4.7k       47k
    I/O >---/\/\---o---/\/\--------->GND
                   |
                   |   22k    SW1
                   o---/\/\---o o--->GND
                   | 
                   |   10k    SW2
                   o---/\/\---o o--->GND
                   |
                   |    1uF
                   o----||---------->GND
                       +
    
                  
            4.7k              SW3
    I/O >---/\/\---o----------o o--->+V
                   |   
                   |   10k
                   o---/\/\--------->GND
    
            4.7k              SW4
    I/O >---/\/\---o----------o o--->+V
                   |   
                   |   10k
                   o---/\/\--------->GND
    
            4.7k              SW5
    I/O >---/\/\---o----------o o--->+V
                   |   
                   |   10k
                   o---/\/\--------->GND
    
    
  • bee_manbee_man Posts: 109
    edited 2013-10-23 18:06
    6 switch 4 pins.bmp

    You could use 6 switches with no more pins or components ( except the extra switch).


    Set J1 HIGH test any combination of SW1, SW2, and SW3
    Set J4 HIGH test any combination of SW4, SW5, and SW6

    If you only want 5 switches just make SW3 and SW4 one DPST switch.
  • jmgjmg Posts: 15,173
    edited 2013-10-23 19:31
    tonyp12 wrote: »
    Puzzle: 5 Switches 4pins available.

    I had assumed 4pins available, meant connections, but I see you have added a Vcc label in there.

    If you are not worried about cables, but want to save Port Pins, then you can scan 3 pairs of Switches, on an OR basis, with just 3 port pins (5 wires), or 4 port pins(5 wires), if you want to go for lowest idle power, no matter what keys are pressed.

    Each pin has a Button+R to Vcc (or switched Vcc), and a Button+R to GND.
    Button scan is to Enable Vcc, Drive Hi and flip to In and check if still Hi - If LOW, GND sw is on.
    then Drive Low, and flip to In, and check if still Low. - If High, Vcc SW is on.
    4 Drive:read combinations exist, only 2 are active buttons.
    No pull downs are needed, but the scan relies on Rs * Cstray < ReadBack time, and Cstray * BoardLeakage > Readback time.

    In 4 port pin design (6 buttons), up to 3 LEDs can be added for Free, if you want to signal info to the user.
    During idle Scan, (> 99% of time) Pins drive to LEDs, During key-scan, LEDs are reverse biased

    No added debounce C is needed.
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-10-23 20:08
    bee_man is good, but there are some two and triple button problems.
    It works due to 1 & 3 can not be pressed at the same time,

    Problem1: Set J4 high Press S3 AND S1 it would confuse J2 thinking S5 was pressed. (but lucky 1<>3)
    Problem2: Set J1 high Press S4 AND S5 then J2 will think S1 was pressed, but xor it with J4 you know it was that and not S1+S3 (as that combo is not physical possible)
    Problem:3 Set J1 high Press S1 AND S4 AND S5 (valid key press) , how does it know it was not problem 2 above?

    Update:
    I changed it so S4 goes to J3 instead, should work better at S2 and S4 can not be pressed at the same time and power can not find a path from J1.
    5SwitchSolution3.jpg?psid=1

    P.S If I wanted to use a IC, this tiny QFN HC238 should fit in somewhere.
    http://www.mouser.com/ProductDetail/NXP/74HC238BQ115/?qs=%2fha2pyFadujxT%252bHyEDK6NmmiMIeuwAoc%252bmSnMJUCEVY%3d
  • kwinnkwinn Posts: 8,697
    edited 2013-10-24 20:04
    How about this. Requires 3 pins and 10 resistors, and provides binary number for the switch pressed. No way of sensing multiple button presses though.
    653 x 478 - 43K
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-24 23:15
    I know you didn't want an IC or diode solution, but here is something I dug up that would allow RS232 communication from up to 8-switches using only 1 I/O pin, or 2 I/O pins if your processor can provide the appropriate baud frequency at the same time your reading the I/O... not a problem for a Propeller though.

    http://forums.parallax.com/showthread.php/120970-Converting-serial-to-parallel?p=891726&viewfull=1#post891726
Sign In or Register to comment.