Go Down

Topic: codehalp: 4051 mux on input: tact switches? (Read 2682 times) previous topic - next topic

nym

Mar 15, 2011, 09:08 pm Last Edit: Mar 15, 2011, 09:20 pm by nym Reason: 1
howdy. trying to learn the 4051, but i'm having a bit of trouble.
i'm using 4 tact switches on a 4051, going into a digital pin on the arduino. i will have 8 tact switches eventually, but i'm starting with 4.

i adapted my code from midi footsteps.
here it is:

Code: [Select]
void doSensorScan() {  // look at all the sensors
for(int i=0; i<8; i++){
  // select multiplexer channel
  digitalWrite(s0Pin, i & 0x1);
  digitalWrite(s1Pin, (i>>1) & 0x1);
  digitalWrite(s2Pin, (i>>2) & 0x1);
  delay(1);            // delay to allow sample & hold capictor to charge
  currentState[i] = digitalRead(mux1); // read mux1 in first 8 array locations
      if(i == 0 && currentState[i] == HIGH)
   {    Serial.println(0);    }
       if(i == 1 && currentState[i] == HIGH)
   {    Serial.println(1);    }
           if(i == 2 && currentState[i] == HIGH)
   {    Serial.println(2);    }
               if(i == 3 && currentState[i] == HIGH)
   {    Serial.println(3);    }
}  
}


i know there's a better way to do the printing, but my main concern is why my buttons respond incorrectly.
i am getting values written to the serial monitors, but some buttons trigger multiple values and are unpredictable. not sure what i'm doing wrong.

my end goal is to have each input on the 4051 behave as would any of the digital inputs on arduino.

thanks for any help
nym

nym

looks like it's working now...i think my S0,S1,S2 lines to my arduino were poorly moored in their breadboard sockets...i need a bigger breadboard...

any tips as to cleanifying my code would be appreciated.
thanks -
nym

Grumpy_Mike

Make sure you have pull up resistors on the switches.

Code: [Select]
      if(i == 0 && currentState[i] == HIGH)
    {    Serial.println(0);    }
        if(i == 1 && currentState[i] == HIGH)
    {    Serial.println(1);    }
            if(i == 2 && currentState[i] == HIGH)
    {    Serial.println(2);    }
                if(i == 3 && currentState[i] == HIGH)
    {    Serial.println(3);    }


Can be replaced by:-
Code: [Select]

if(currentState[i] == HIGH)  Serial.println(i);


nym

#3
Mar 17, 2011, 09:31 pm Last Edit: Mar 17, 2011, 09:55 pm by nym Reason: 1
howdy.
need a little more help if you don't mind...
i changed it to print only if the data is LOW because for some reason the mux always reads HIGH (even when there is nothing on the inputs! perhaps it just works that way?)
here is my circuit:



my tact switches read, and everything looks good...except that the data flickers. for example, if i hit the switch on mux input 0, 0 will print - but so will 1, every now and then. code posted below

Code: [Select]
void doSensorScan() {  // look at all the sensors
for(int i=0; i<4; i++){
  // select multiplexer channel
  digitalWrite(s0Pin, i & 0x1);
  digitalWrite(s1Pin, (i>>1) & 0x1);
  digitalWrite(s2Pin, (i>>2) & 0x1);
  delay(1);            // delay to allow sample & hold capictor to charge
  currentState[i] = digitalRead(mux1); // read mux1 in first 8 array locations
      if(currentState[i] == LOW)
   {    Serial.print(i);}
 
}}


Grumpy_Mike

Quote
because for some reason the mux always reads HIGH (even when there is nothing on the inputs!


You need to read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
then wire it up correctly.

nym

#5
Mar 18, 2011, 04:11 pm Last Edit: Mar 18, 2011, 04:19 pm by nym Reason: 1
nice, thanks. i love your website, not the first time it's gotten me out of a jam.
edit - seriously, well written and kindly to the lower denominator electrician such as myself. this really helped, you were dead right that the new jack would assume that the pin should default to LOW instead of HIGH. the inverse does of course make more sense, but then again so does counting from 1 instead of 0 to the uninitiated.

as proof that i am in fact making progress with arduino and c++, please take a glance at this extremely short video showcasing my last project -

http://www.youtube.com/watch?v=_PcyuM1VGRk

graynomad

Rob Gray aka the GRAYnomad www.robgray.com

nym

thanks, rob.
ok, so an update.
i pullup-ed the 4051 inputs with 15 ohm resistors. i also pulled up the arduino input with a resistor of greater value.
my revised function looks like this:

Code: [Select]
void doSensorScan() {  // look at all the sensors
for(int i=0; i<4; i++){
   // select multiplexer channel
   digitalWrite(s0Pin, i & 0x1);
   digitalWrite(s1Pin, (i>>1) & 0x1);
   digitalWrite(s2Pin, (i>>2) & 0x1);
   delay(1);            // delay to allow sample & hold capictor to charge
   currentState[i] = digitalRead(mux1); // read mux1 in first 8 array locations
       if(currentState[i] == LOW)
    {    Serial.println(i);    }


}


now, i get proper data....that is, most the time....for instance, here is a sample of me pushing a button (no println for space purposes)

11111111111111111111111110111111212121101121111101011211111110121123123231
notice that the proper data appears at first, but then gets garbled.
especially on tact switch repress...
i am unsure whether this is an issue with my code or with my circuit. i can post another crude drawring if necessary.

MikMo


Grumpy_Mike

Quote
pullup-ed the 4051 inputs with 15 ohm resistors.

Way way too low. You are pulling too much current through your switch and you will damage it. Try 10K instead.

Go Up