#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

// Continuity Tester for 10-in, 10-out Cable Configuration using Arduino

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'X', '0', 'E', 'D'}
};

byte rowPins[ROWS] = {51, 50, 49, 48};
byte colPins[COLS] = {47, 46, 45, 44};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Pins for incoming cables
int inPins[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

// Pins for outgoing cables
int outPins[10] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41};

// Push button pin
const int buttonPin = 1;

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows

void setup() {
  // Set up incoming pins as OUTPUT
  for (int i = 0; i < 10; i++) {
    pinMode(inPins[i], OUTPUT);
    digitalWrite(inPins[i], LOW); // Ensure all output pins start LOW
  }

  // Set up outgoing pins as INPUT
  for (int i = 0; i < 10; i++) {
    pinMode(outPins[i], INPUT);
  }

  // Set up push button pin as INPUT
  pinMode(buttonPin, INPUT);

  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Initialize serial communication
  Serial.begin(9600);

  lcd.setCursor(0, 0);
  lcd.print("  Cable Tester");
  lcd.setCursor(0, 1);
  lcd.print("Press X to Start");
}

void loop() {
  // Read the keypad
  char key = customKeypad.getKey();

  // If 'X' key is pressed, start the test
  if (key == 'X') {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Testing...");
    delay(500);

    lcd.clear();

    for (int i = 0; i < 10; i++) {
      // Set current output pin HIGH
      digitalWrite(inPins[i], HIGH);
      delay(10); // Ensure the signal is stable

      bool continuityFound = false;

      for (int j = 0; j < 10; j++) {
        int state = digitalRead(outPins[j]);
        if (state == HIGH) {
          continuityFound = true;
          lcd.setCursor(0, 1);
          lcd.print("Cont: In ");
          lcd.print(i + 1);
          lcd.print(" Out ");
          lcd.print(j + 1);
          Serial.print("Continuity detected between input pin ");
          Serial.print(inPins[i]);
          Serial.print(" and output pin ");
          Serial.println(outPins[j]);
          delay(1000); // Display result for a short time
        }
      }

      if (!continuityFound) {
        lcd.setCursor(0, 1);
        lcd.print("No Cont: In ");
        lcd.print(i + 1);
        lcd.print(" Out ");
        lcd.print("All");
        Serial.print("No continuity detected for input pin ");
        Serial.print(inPins[i]);
        Serial.println(" to any output pin");
        delay(1000); // Display result for a short time
      }

      // Set current output pin LOW
      digitalWrite(inPins[i], LOW);
      delay(10);
    }

    lcd.setCursor(0, 0);
    lcd.print("Test Complete");
    lcd.setCursor(0, 1);
    lcd.print("Press X to Start");
  }
}
$abcdeabcde151015202530354045505560fghijfghij