Pages: [1]   Go Down
Author Topic: CD4051 Multiplexor input test sketch  (Read 1180 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//------------------------
//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("");
}
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I know it's not efficient but it's just for testing a connection to a MUX, works for me.
Code:
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?
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Online Online
Shannon Member
****
Karma: 162
Posts: 10478
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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????
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

inefficiency is a concept that applies not just to time.
Logged

Pages: [1]   Go Up
Jump to: