// Column 1 (left)

// C1 = Pin 2
// C2 = Pin 3
// C3 = Pin 4
// C4 = Pin 5

// Column 4 (right)

// Row 1 (top row)

// R1 = Pin 6
// R2 = Pin 7
// R3 = Pin 8
// R4 = Pin 9

// Row 4 (bottom row)


long Time = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(2, INPUT_PULLUP); // HIGH
  pinMode(3, INPUT_PULLUP); // HIGH
  pinMode(4, INPUT_PULLUP); // HIGH
  pinMode(5, INPUT_PULLUP); // HIGH
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{

  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  digitalWrite(6, LOW);

  if (digitalRead(2) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('1');
      Time = millis();
    }
  }
  if (digitalRead(3) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('2');
      Time = millis();
    }
  }
  if (digitalRead(4) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('3');
      Time = millis();
    }
  }
  if (digitalRead(5) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('A');
      Time = millis();
    }
  }

  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  digitalWrite(7, LOW);

  if (digitalRead(2) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('4');
      Time = millis();
    }
  }
  if (digitalRead(3) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('5');
      Time = millis();
    }
  }
  if (digitalRead(4) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('6');
      Time = millis();
    }
  }
  if (digitalRead(5) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('B');
      Time = millis();
    }
  }

  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  digitalWrite(8, LOW);

  if (digitalRead(2) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('7');
      Time = millis();
    }
  }
  if (digitalRead(3) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('8');
      Time = millis();
    }
  }
  if (digitalRead(4) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('9');
      Time = millis();
    }
  }
  if (digitalRead(5) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('C');
      Time = millis();
    }
  }

  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);

  digitalWrite(9, LOW);

  if (digitalRead(2) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('*');
      Time = millis();
    }
  }
  if (digitalRead(3) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('0');
      Time = millis();
    }
  }
  if (digitalRead(4) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('#');
      Time = millis();
    }
  }
  if (digitalRead(5) == 0)
  {
    if (millis() - Time > 500 )
    {
      Serial.print('D');
      Time = millis();
    }
  }
}