🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

what is the simplest way to read 7 switches w/4 wires
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Mon Feb 15, 2016 3:07 pm

Hi There,

Dammit Jim! I'm a programmer, not an Electrical Engineer!!

Logically speaking I wanted to use 3 lines to read as many switches as possible (knowing I had to save one for power.) So I thought of the 3 lines as binary counting and decided I could read 7 switches -- 001, 010, 011, etc...

So I wired up 7 switches with each one connecting to the proper wires using the binary 'directions' -- and Hey Presto, all three lines light up no matter which switch I press.

Which gets me to thinking about using transistors in some reverse-cascade arrangement (I'd have to figure that out) so that only the lines I want energized become so when I press each switch.

Thinking about transistors leads me to thinking that there may be a chip to do this job, and multiplexing comes to mind -- but everything I can find seems to need some computing on both ends and I want only switches on the far end.

So after trying to read a bunch of bunch of EE greek-to-me stuff I bail out on that.

So I think about the mess for a while longer and realize that the current is flowing 'backwards' and cross connecting the lines -- and a quick google search tells me that I could use diodes to control the directional flow of current and that would probably work.

Is there a better way to put 7 switches out at the end of a 4 conductor wire and be able to tell which one is being held down? I'm willing to put a chip out there, but I don't want to put a computer out there.
ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Re: what is the simplest way to read 7 switches w/4 wires

by paulf8080 on Mon Feb 15, 2016 9:31 pm

Google 8 to 3 encoder

paulf8080
 
Posts: 207
Joined: Sat Jan 18, 2014 10:25 pm

Re: what is the simplest way to read 7 switches w/4 wires

by paulf8080 on Mon Feb 15, 2016 9:32 pm

Google 8 to 3 encoder

paulf8080
 
Posts: 207
Joined: Sat Jan 18, 2014 10:25 pm

Re: what is the simplest way to read 7 switches w/4 wires

by franklin97355 on Mon Feb 15, 2016 9:35 pm

Something like this should work http://www.onsemi.com/pub_link/Collateral/MC14028B-D.PDF This is not the only chip out there, Google "binary to decimal" for more

franklin97355
 
Posts: 21650
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Tue Feb 16, 2016 9:28 am

thanks for these great answers -- I've ordered some chips
ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Re: what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Wed Feb 17, 2016 7:21 pm

ok, so the diodes arrived -- and it works as I had predicted !! a small test with 4 switches and 3 wires works !!

logic wins !!

the chips should show up Friday, and I can try all 7 switches without a [small] spiders nest of diodes :)
ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Re: what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Wed Feb 17, 2016 7:31 pm

sample code for a Espruino

Code: Select all | TOGGLE FULL SIZE
// standard options for pins
var stdOpts = { repeat: true, edge:'rising', debounce:25 };

// when a pin changes call the fn with everything we know
function onChange ( pin, fn ) {
  return function changed ( evt ) { // rather than the little bit they give us
    fn.call(null,pin,evt);
  };
}

// setup the watch on the pin to call the changeFn
function setupInput ( pin, changeFn, opts ) {
  digitalRead(pin);
  pinMode(pin, "input_pulldown");
  setWatch(onChange(pin,changeFn),pin,opts);
}

// the pins we'll use
var pins = [ 'B3','B4','B5' ];

// our binary source
var binary = [0,0,0];

// when a pin is on set it's associated bit
function pinIsOn (ndx) {
  return function (name,evt) {
    binary[ndx] = 1;
  };
}

// initialize the pins
pins.forEach(function(e,x){ setupInput(e, pinIsOn(x), stdOpts); });

// every 1/2 second output the last button pushed
setInterval(function(){
  var binStr = binary.reverse().join('');
  if (binStr) {
    var btn = parseInt(binStr,2);
    if (btn) {
      console.log('switch ',btn);
    }
  }
  binary = [0,0,0]; // and reset
},500);

ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Re: what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Fri Feb 26, 2016 7:34 pm

I should have been looking for Encoders as suggested by others

The MC14028B is a Decoder

Will advise on success...
ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Re: what is the simplest way to read 7 switches w/4 wires

by ajoslin103 on Fri Feb 26, 2016 7:58 pm

wait a minute - using an encoder is going to require me to supply it with not only +3v but also a gnd -- right?

I only have 4 wires -- if I use one for +3 and one for gnd then I only have 2 wires left to read 7 switches !!

My diode solution handled the problem by only completing the circuit when a switch was pressed...

Hrmmmm......

Is there a chip that does 'cascading' diodes that doesn't need + and gnd ?
ajoslin103
 
Posts: 8
Joined: Mon Jun 11, 2012 10:21 pm

Please be positive and constructive with your questions and comments.