#define EN 3 #define S0 4 #define S1 5 #define S2 6 #define S3 7 #define SIG A0
void setup(){ Serial.begin(9600); pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT);
digitalWrite(S0, LOW); digitalWrite(S1, LOW); digitalWrite(S2, LOW); digitalWrite(S3, LOW);
pinMode(EN, OUTPUT); digitalWrite(EN, LOW);
}
void loop(){
//Loop through and read all 16 values //Reports back Value at channel 6 is: 346 for(int i = 0; i < 16; i ++){ // Serial.print("Value at channel "); // Serial.print(i); // Serial.print(": "); Serial.print(readMux(i)); Serial.print(". "); } Serial.println(); delay(1000); }
int readMux(int channel){ int controlPin[] = {S0, S1, S2, S3};
int muxChannel[16][4]={ {0,0,0,0}, //channel 0 {1,0,0,0}, //channel 1 {0,1,0,0}, //channel 2 {1,1,0,0}, //channel 3 {0,0,1,0}, //channel 4 {1,0,1,0}, //channel 5 {0,1,1,0}, //channel 6 {1,1,1,0}, //channel 7 {0,0,0,1}, //channel 8 {1,0,0,1}, //channel 9 {0,1,0,1}, //channel 10 {1,1,0,1}, //channel 11 {0,0,1,1}, //channel 12 {1,0,1,1}, //channel 13 {0,1,1,1}, //channel 14 {1,1,1,1} //channel 15 };
//loop through the 4 sig for(int i = 0; i < 4; i ++){ digitalWrite(controlPin[i], muxChannel[channel][i]); }
//read the value at the SIG pin int val = analogRead(SIG);
//return the value return val; }
|