I don’t know how your button is wired, I enable INPUT_PULLUP and close the button to GND,
resulting in this code, that I can not bring to display anything unexpected (on my I2C variant, different pins, on a Nano).
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const byte drivePlusPin = 22;
const byte driveMinusPin = 24;
const byte driveGNDPin = 26;
const byte driveCasingPin = 28;
const byte sensePlusPin = 23;
const byte senseMinusPin = 25;
const byte senseGNDPin = 27;
const byte senseCasingPin = 29;
const unsigned int testAllMillis = 1000;
unsigned long lastTest;
const byte buttonPin = 36;
void setup()
{
lcd.begin(16, 2);
lcd.print(F("XLR Test"));
Serial.begin(115200);
pinMode(drivePlusPin, OUTPUT);
pinMode(driveMinusPin, OUTPUT);
pinMode(driveGNDPin, OUTPUT);
pinMode(driveCasingPin, OUTPUT);
pinMode(sensePlusPin, INPUT_PULLUP);
pinMode(senseMinusPin, INPUT_PULLUP);
pinMode(senseGNDPin, INPUT_PULLUP);
pinMode(senseCasingPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
delay(1000);
lcd.clear();
}
void loop()
{
unsigned long topLoop = millis();
bool buttonDown = !digitalRead(buttonPin);
if (buttonDown) {
if (topLoop - lastTest >= testAllMillis) {
lastTest = topLoop;
oneTest(PSTR("POS"), drivePlusPin, sensePlusPin, 0, 0);
oneTest(PSTR("NEG"), driveMinusPin, senseMinusPin, 0, 8);
oneTest(PSTR("GND"), driveGNDPin, senseGNDPin, 1, 0);
oneTest(PSTR("CAS"), driveCasingPin, senseCasingPin, 1, 8);
Serial.println("");
}
}
}
void oneTest(const char* name, byte outPin, byte inPin, byte row, byte col)
{
digitalWrite(outPin, LOW); // sense is pulled high, so try to drive it low
Serial.print(F("XLR "));
Serial.print((__FlashStringHelper*)name);
lcd.setCursor(col, row);
lcd.print((__FlashStringHelper*)name);
lcd.setCursor(col + 4, row);
if (digitalRead(inPin)) { // if it stays high, there is no connection
Serial.println(F(" FAIL"));
lcd.print(F("err"));
} else {
Serial.println(F(" PASS"));
lcd.print(F("ok "));
}
digitalWrite(outPin, HIGH); // release pin
}
A debouncing of the button is not neccessary, because the triggered action can only be started again after a second.
Normally you would have to use debouncing code and more key-was-pressed vs key-is-pressed.
For the debouncing you can use a library, I like Bounce2.