Go Down

Topic: CD4051 Multiplexor input test sketch (Read 1 time) previous topic - next topic

Pancra85

I made a little sketch to test my MUX shield and thought it may be useful to someone else
It's for testing INPUT potentiometers

Sorry for spanish comments, but you just need to copy, paste and change multSelPins[] to the inputs (S0,S1,S2) of the MUX and muxPin to the Pin of "COM OUT/IN" (Pin 3 of IC)

Code: [Select]
//------------------------
//change multSelPins[] to the inputs (S0,S1,S2) of the MUX and muxPin to the Pin of "COM OUT/IN" (Pin 3 of IC)
//------------------------

const int multSelPins[]={2,3,4}; //selectores del multiplex (S2,S1,S0);
const int muxPin=A5; //salida del mux

void setup() {
  Serial.begin(9600);
  pinMode (muxPin,INPUT);
  for(int x=0;x<3;x++){
    pinMode(multSelPins[x],OUTPUT);
  }
}

void loop() {
  int channel[]={0,0,0};
  multSelectInputFun(channel);
  int value=analogRead(muxPin);
  Serial.println(value);
  int channel1[]={0,0,1};
  multSelectInputFun(channel1);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel2[]={0,1,0};
  multSelectInputFun(channel2);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel3[]={0,1,1};
  multSelectInputFun(channel3);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel4[]={1,0,0};
  multSelectInputFun(channel4);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel5[]={1,0,1};
  multSelectInputFun(channel5);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel6[]={1,1,0};
  multSelectInputFun(channel6);
  value=analogRead(muxPin);
  Serial.println(value);
  int channel7[]={1,1,1};
  multSelectInputFun(channel7);
  value=analogRead(muxPin);
  Serial.println(value);
 
  Serial.println("-----------------------");
  delay(500);
}

void multSelectInputFun(int inputSel[]){
  //Serial.print("multInput :");
  for (int x=0;x<3;x++){
    //digitalWriteFast2(multSelPins[x],0); //prueba
    digitalWrite(multSelPins[x],inputSel[x]); //escribe el selector del multiplex con el valor que le pasamos
    //Serial.print(inputSel[x]);
  }
  //Serial.println("");
}

dhenry

1. That's very inefficient;
2. You may spuriously select two wrong channels before settling on  your target channel.

Pancra85

Yes, I know it's not efficient but it's just for testing a connection to a MUX, works for me.
Code: [Select]
2. You may spuriously select two wrong channels before settling on  your target channel.
Sorry but I don't understand, what do you mean with this?

dhenry

Think about your multSelectInputFun() call. As x goes through 0..2, you have selected multSelectPin[] will select two combinations before settling on the final combination.

MarkT


1. That's very inefficient;
2. You may spuriously select two wrong channels before settling on  your target channel.


Just how efficient do you want it to be given that there's a call to analogRead() which takes 110us????
[ I won't respond to messages, use the forum please ]

dhenry

inefficiency is a concept that applies not just to time.

Go Up