const int s0Pin = 49; // S0 pin
const int s1Pin = 51; // S1 pin
const int s2Pin = 53; // S2 pin
const int s3Pin = 52; // S3 pin
const int numSwitches = 16; // Total number of switches
const int numMuxChannels = 16; // Number of multiplexer channels
void setup() {
pinMode(s0Pin, OUTPUT);
pinMode(s1Pin, OUTPUT);
pinMode(s2Pin, OUTPUT);
pinMode(s3Pin, OUTPUT);
// Set all the pins to LOW initially
digitalWrite(s0Pin, LOW);
digitalWrite(s1Pin, LOW);
digitalWrite(s2Pin, LOW);
digitalWrite(s3Pin, LOW);
Serial.begin(9600);
}
void loop() {
for (int channel = 0; channel < numMuxChannels; ++channel) {
// Set the multiplexer inputs using binary representation
digitalWrite(s0Pin, (channel & 0x01) ? HIGH : LOW);
digitalWrite(s1Pin, (channel & 0x02) ? HIGH : LOW);
digitalWrite(s2Pin, (channel & 0x04) ? HIGH : LOW);
digitalWrite(s3Pin, (channel & 0x08) ? HIGH : LOW);
// Read the state of the switch on the current channel
int switchState = digitalRead(48); // Assuming the switches are connected to analog pin A0
int test1 = digitalRead(49);
int test2 = digitalRead(51);
int test3 = digitalRead(53);
int test4 = digitalRead(52);
// Print the channel number and switch state
Serial.print("Channel: ");
Serial.print(channel);
Serial.print(" Switch State: ");
Serial.println(switchState);
Serial.print(test1);
Serial.print(test2);
Serial.print(test3);
Serial.print(test4);
delay(500); // Delay for stability
}
}
Loading
cd74hc4067
cd74hc4067