// Define button pins and BCD output pins
const int buttonPins[] = {A0, A1, A2, A3, A4, A5, A7, A8, A9, A10, A11, 48};
const int bcdPins[] = {A12, A13, A14, A15};
const int outputPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int selectPin = 14;
// Variables to store the selected button and BCD value
int selectedButton = -1;
int bcdValue = 0;
void setup() {
// Set button pins as inputs and enable internal pull-up resistors
for (int i = 0; i < 12; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Set selectPin as an input
pinMode(selectPin, INPUT);
// Set BCD output pins and output pins as outputs and initialize them to LOW
for (int i = 0; i < 4; i++) {
pinMode(bcdPins[i], OUTPUT);
digitalWrite(bcdPins[i], LOW);
}
for (int i = 0; i < 12; i++) {
pinMode(outputPins[i], OUTPUT);
digitalWrite(outputPins[i], LOW);
}
}
void updateOutputs() {
// Display BCD value on the BCD output pins
for (int j = 0; j < 4; j++) {
digitalWrite(bcdPins[j], bcdValue & 0x01);
bcdValue >>= 1;
}
// Set the corresponding output pin to HIGH
digitalWrite(outputPins[selectedButton], HIGH);
}
void loop() {
int buttonState;
int selectState = digitalRead(selectPin);
if (selectState == LOW) {
// If the selectPin is LOW, check for button presses
for (int i = 0; i < 12; i++) {
buttonState = digitalRead(buttonPins[i]);
if (buttonState == LOW && selectedButton == -1) {
// Button is pressed and no button has been selected before
selectedButton = i;
bcdValue = i + 1;
// Update outputs
updateOutputs();
}
}
} else {
// If the selectPin is HIGH, reset the selectedButton and BCD values
if (selectedButton != -1) {
// Turn off BCD output pins and output pins
digitalWrite(bcdPins[0], LOW);
digitalWrite(bcdPins[1], LOW);
digitalWrite(bcdPins[2], LOW);
digitalWrite(bcdPins[3], LOW);
digitalWrite(outputPins[selectedButton], LOW);
// Reset selectedButton and BCD values
selectedButton = -1;
bcdValue = 0;
}
}
}