const uint8_t COM_PIN = A0;
const uint8_t CONTROL_PINS[] = {3, 4, 5, 6};
const uint8_t LENGTH_OF_CONTROL_PINS = sizeof(CONTROL_PINS) / sizeof(CONTROL_PINS[0]);
const uint8_t NUM_OF_INPUTS = 16;
void setup() {
Serial.begin(9600);
for(uint8_t i = 0; i < LENGTH_OF_CONTROL_PINS; i++) {
pinMode(CONTROL_PINS[i], OUTPUT);
}
}
uint16_t readMux(int channel) {
for(uint8_t i = 0; i < LENGTH_OF_CONTROL_PINS; i++) {
// 2 進数表記の channel の i 桁目を取り出し,
// CONTROL_PINS[i] に出力
digitalWrite(CONTROL_PINS[i], (channel >> i & 1));
}
delay(10);
return analogRead(COM_PIN);
}
void loop() {
// 各入力値を半角スペース区切りで出力
for(uint8_t i = 0; i < (NUM_OF_INPUTS - 1); i++) {
Serial.print(readMux(i));
Serial.print(" ");
}
Serial.println(readMux(NUM_OF_INPUTS - 1));
delay(1000);
}
Loading
cd74hc4067
cd74hc4067
0