const uint8_t SPinB[] = {6, 5, 4, 3}; // S0 ... S3
const uint8_t SPinL[] = {11, 10, 9, 8}; // S0 ... S3
const int SIGB = 2; // SIG pin input for buttons
const int SIGL = 7; // SIG pin output for leds
int readMux(int channel)
{
for (int i = 0; i < 4; i++)
digitalWrite(SPinB[i], channel >> i & 1);
delay(10);
return digitalRead(SIGB);
}
void setup()
{
Serial.begin(115200);
pinMode(SIGB, INPUT);
pinMode(SIGL, OUTPUT);
for (int i = 0; i < 4; i++)
{
pinMode(SPinB[i], OUTPUT);
pinMode(SPinL[i], OUTPUT);
}
}
void loop()
{
// Print it to the Serial Monitor
for (int i = 0; i < 16; i++)
Serial.print(readMux(i));
Serial.println();
// Find the first pressed button
bool found = false;
int index = 0;
for (int i = 0; i < 16 && !found; i++)
{
if(readMux(i) == LOW)
{
index = i;
found = true;
}
}
if(found)
{
// Found a pressed button ?
// The diagram has the buttons in a different order,
// therefor it is 15-index
writeMux(15-index);
digitalWrite(SIGL,HIGH);
}
else
{
// Nothing found? Then turn off all leds
// The Enable is not used, so just set the SIG to LOW.
digitalWrite(SIGL,LOW);
}
delay(50);
}
void writeMux(int channel)
{
for (int i = 0; i < 4; i++)
digitalWrite(SPinL[i], channel >> i & 1);
}
Loading
cd74hc4067
cd74hc4067
Loading
cd74hc4067
cd74hc4067