#include <Arduino.h>

#define COM_PIN 2

const uint8_t CONTROL_PINS[] = {3, 4, 5, 6};
const int LED_PIN = A0;

char readButton(int channel)
{
  for (int i = 0; i < 4; i++)
  {
    digitalWrite(CONTROL_PINS[i], (channel >> i) & 1);
  }
  delay(10);
  int buttonState = digitalRead(COM_PIN);
  char buttonChar = 'A' + channel;
  return buttonState == HIGH ? buttonChar : '\0';
}

void setup()
{
  Serial.begin(9600);
  pinMode(COM_PIN, INPUT);
  for (int i = 0; i < 4; ++i)
  {
    pinMode(CONTROL_PINS[i], OUTPUT);
  }
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 16; ++i)
  {
    char buttonPressed = readButton(i);
    if (buttonPressed != '\0')
    {
      Serial.println(buttonPressed);
      digitalWrite(LED_PIN, HIGH); // Turn on LED when a button is pressed
      delay(100);
    }
    digitalWrite(LED_PIN, LOW); // Turn off LED after delay
  }
}