Electronic – arduino – Detecting paired node combinations

algorithmarduinocircuit analysis

My 2 year-old nephew likes nothing more than to plug/unplug RCA jacks. Whenever he's around, tv inputs get switched, audio plugs are swapped, etc.

So… For his birthday, I decided to build him a little box with a panel full of RCA jacks and some buttons and nice blinky leds and beepy noises. The idea is to have an array of about 12 RCA jacks, into which he can plug cables to his heart's content. To give him some feedback, an arduino will generate different blinky led patterns and wacky noises (using the Mozzi library) depending on which pairs of RCA jacks are connected.

My question is… What's an efficient way to detect which pairs of RCA jacks are shorted via a cable? Ideally, for 12 RCA jacks, I would want to uniquely detect up to 6 connected pairs.

I have a few ideas (below), but I can't help but think there must be a more elegant solution to the problem.

Possible combinations
Some combinatorics tells me that the number of ways I can choose p pairs out of n jacks is:

C(n,p) = n!/((n-2p)!*p!*2^p)

That's basically the number of permutations of 2p jacks out of n (=n!/((n-2p)! ), divided by number of pair reversal combinations 2^p (i.e. reversing a cable), and divided by the number of pair permutations p! (i.e. the order of plugging in the cables)

So, for 12 jacks and using anywhere from 0 to 6 cables, there are:
sum(C(12,p) for p=0..6) = 140152 possible pairings (I think…)

Some ideas I came up with:

1) connect each of 12 jacks to an arduino digital IO pin with a pullup resistor. Scan for pairings by setting one pin to OUTPUT LOW and checking for a low signal on the other eleven (configured as INPUT pins). Repeat 11x (each time, setting another one of the twelve to OUTPUT LOW)

Advantage: uniquely detect any of the 140152 possible pairings

Disadvantage: Lots of pins in use

2) Create a string of resistors from 5V to ground, via the 12 jacks, like this:
GND – R – p1 – R – p2 – R – p3 – R – p4 – R – … – p12 – 5V

Connect pin p1 to an analog input as well. When a cable is plugged in to two jacks, some of the resistors are bypassed, changing the voltage at pin p1. For equal values of R, we can determine only the total number of resistors bypassed. For sequentially doubling values of R (1,2,4,8,…), we can deduce from the voltage at p1 exactly which resistors have been bypassed (sort of… we'll be limited by noise and the arduino's 10 bit ADC)

Advantage: Simple, only 1 analog pin used

Disadvantage: This method can't uniquely identify all 140152 pairings (notably when plugging in multiple cables), but I don't think my 2-year old nephew will notice the shortcoming.

3)
Something something RLC filter… I have no idea what I'm talking about.

Best Answer

Connect all the shields to ground, so the only thing to detect is which center pins are connected to which other center pins.

This is pretty easy to do with scanning. Set up the electronics so that each pin can be driven one way but passively pulled the other. Since built-in pullups are more common in microcontrollers, I'll assume each line has a passive pullup and can be actively driven low.

Now you simply go thru the possible combinations. Drive pin 1 low (all others passively pulled high), and read all the pins. The one that is low is connected to pin 1. Now drive pin 2 low and release all others and read all the pins, etc.

Let's say you do this slowly, like every 100 µs. Each 100 µs interrupt you read the pins from the previous output state, then switch to the new output state. That requires 12 interrupts or 1.2 ms to get the complete picture of what is connected to what. That's way faster than humans can notice, so even at this slow scanning rate the processor will know what it connected to what instantaneously in human time.