Test which pin is connected - Works but can't get right output

Hello,

I need your help with my first real project.

Goal : connect 8 pins two by two, and have a message each time a connection is made or removed.

Here is my code (modified from one of Alastair Aitchison) :

#define DEBUG

// define pins of the cable
byte cablePins[] = {6, 7, 8, 9, 10, 11, 12, 13};

const byte connections[8][7][2] = {
  { {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7} },
  { {1, 0}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7} },
  { {2, 0}, {2, 1}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7} },
  { {3, 0}, {3, 1}, {3, 2}, {3, 4}, {3, 5}, {3, 6}, {3, 7} },
  { {4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 5}, {4, 6}, {4, 7} },
  { {5, 0}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 6}, {5, 7} },
  { {6, 0}, {6, 1}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 7} },
  { {7, 0}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6} }
};

#define NUMCONNECTIONS (sizeof(cablePins))


// GLOBAL VARIABLES
// Track state of which output pins are connected
bool lastState[7] = { false, false, false, false, false, false, false };

void setup() {

  Serial.begin(9600);
  Serial.println();
  Serial.println("################################################");
  Serial.println("#                   CABLE TEST                 #");
  Serial.println("################################################");
  Serial.println();

}

void loop() {
 
    // Assume that the state has not changed since last reading
    bool stateChanged = false;


    // Check each connection in turn
    for (int i = 0; i < NUMCONNECTIONS; i++) {

      for (int j = 0; j < (NUMCONNECTIONS - 1); j++) {

        // Get the pin numbers
        byte pin1 = cablePins[connections[i][j][0]];
        byte pin2 = cablePins[connections[i][j][1]];

        bool currentState = isConnected(pin1, pin2);

        if (currentState != lastState[j]) {

          // Set the flag to show the state has changed
          stateChanged = true;

          // Update the stored connection state
          lastState[j] = currentState;
        }
      }
      // If a connection has been made/broken since last time we checked
      if (stateChanged) {
    #ifdef DEBUG
        // Dump to serial the current state of all connections
        for (uint8_t j = 0; j < NUMCONNECTIONS - 1; j++) {
          Serial.print(F("Pin#"));
          Serial.print(cablePins[connections[i][j][0]]);
          Serial.print(F(" - Pin#"));
          Serial.print(cablePins[connections[i][j][1]]);
          Serial.print(F(" : "));
          Serial.println(lastState[j] ? "CONNECTED" : "NOT CONNECTED");
        }
        Serial.println(F("---"));
    #endif
      }
    }
  */
}

/**
   Tests whether an output pin is connected to a given INPUT_PULLUP pin
*/
bool isConnected(byte OutputPin, byte InputPin) {

  // To test whether the pins are connected, set the first as output and the second as input
  pinMode(OutputPin, OUTPUT);
  pinMode(InputPin, INPUT_PULLUP);

  // Set the output pin LOW
  digitalWrite (OutputPin, LOW);

  // If connected, the LOW signal should be detected on the input pin
  // (Remember, we're using LOW not HIGH, because an INPUT_PULLUP will read HIGH by default)
  bool isConnected = !digitalRead(InputPin);

  // Set the output pin back to its default state
  pinMode(OutputPin, INPUT_PULLUP);

  return isConnected;
}

My problem is what I get on the monitor :

Résumé

Pin#7 - Pin#6 : NOT CONNECTED
Pin#7 - Pin#8 : NOT CONNECTED
Pin#7 - Pin#9 : CONNECTED
Pin#7 - Pin#10 : NOT CONNECTED
Pin#7 - Pin#11 : NOT CONNECTED
Pin#7 - Pin#12 : NOT CONNECTED
Pin#7 - Pin#13 : NOT CONNECTED

Pin#8 - Pin#6 : NOT CONNECTED
Pin#8 - Pin#7 : NOT CONNECTED
Pin#8 - Pin#9 : NOT CONNECTED
Pin#8 - Pin#10 : NOT CONNECTED
Pin#8 - Pin#11 : NOT CONNECTED
Pin#8 - Pin#12 : NOT CONNECTED
Pin#8 - Pin#13 : NOT CONNECTED

Pin#9 - Pin#6 : NOT CONNECTED
Pin#9 - Pin#7 : CONNECTED
Pin#9 - Pin#8 : NOT CONNECTED
Pin#9 - Pin#10 : NOT CONNECTED
Pin#9 - Pin#11 : NOT CONNECTED
Pin#9 - Pin#12 : NOT CONNECTED
Pin#9 - Pin#13 : NOT CONNECTED

Pin#10 - Pin#6 : NOT CONNECTED
Pin#10 - Pin#7 : NOT CONNECTED
Pin#10 - Pin#8 : NOT CONNECTED
Pin#10 - Pin#9 : NOT CONNECTED
Pin#10 - Pin#11 : NOT CONNECTED
Pin#10 - Pin#12 : NOT CONNECTED
Pin#10 - Pin#13 : NOT CONNECTED

Pin#11 - Pin#6 : NOT CONNECTED
Pin#11 - Pin#7 : NOT CONNECTED
Pin#11 - Pin#8 : NOT CONNECTED
Pin#11 - Pin#9 : NOT CONNECTED
Pin#11 - Pin#10 : NOT CONNECTED
Pin#11 - Pin#12 : NOT CONNECTED
Pin#11 - Pin#13 : NOT CONNECTED

Pin#12 - Pin#6 : NOT CONNECTED
Pin#12 - Pin#7 : NOT CONNECTED
Pin#12 - Pin#8 : NOT CONNECTED
Pin#12 - Pin#9 : NOT CONNECTED
Pin#12 - Pin#10 : NOT CONNECTED
Pin#12 - Pin#11 : NOT CONNECTED
Pin#12 - Pin#13 : NOT CONNECTED

Pin#13 - Pin#6 : NOT CONNECTED
Pin#13 - Pin#7 : NOT CONNECTED
Pin#13 - Pin#8 : NOT CONNECTED
Pin#13 - Pin#9 : NOT CONNECTED
Pin#13 - Pin#10 : NOT CONNECTED
Pin#13 - Pin#11 : NOT CONNECTED
Pin#13 - Pin#12 : NOT CONNECTED

And it loops.

How can I change the code so it shows only what connection changed ?

why not sequentially test each combination


const byte cablePins [] = {6, 7, 8, 9, 10, 11, 12, 13};
const byte Npin = sizeof(cablePins);

void
loop (void)
{
    for (int i = 0; i < Npin-1; i++)  {
        pinMode (cablePins [i], OUTPUT);
        for (int j = i+1; j < Npin; j++)  {
            digitalWrite (cablePins [i], HIGH);
            if (HIGH == digitalRead (cablePins [j])) {
                digitalWrite (cablePins [i], LOW);
                if (LOW == digitalRead (cablePins [j]))  {
                    Serial.print ("   ");
                    Serial.print (cablePins [i]);
                    Serial.print ("   ");
                    Serial.print (cablePins [j]);
                    Serial.print ("   match");
                    Serial.println ();
                }
            }
        }
        pinMode (cablePins [i], INPUT_PULLUP);
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

Thanks ! Way better than what I did.

But it is not what I expect. Sorry, i did not express my need clearly.

I would like something like this in the monitor:

If I connect pins 6 and 8, nothing else :

6-8 connected

Then I connect 7-9 :

6-8 connected
7-9 connected

If I remove 6-8 :

7-9 connected

And if possible without looping messages.

byte cablePins[] = {6, 7, 8, 9, 10, 11, 12, 13};
#define NUMCONNECTIONS (sizeof(cablePins)-1)
bool lastState[NUMCONNECTIONS * NUMCONNECTIONS] = { false };

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("################################################");
  Serial.println("#                   CABLE TEST                 #");
  Serial.println("################################################");
  Serial.println();
}

void loop() {
  for (byte i = 0; i < NUMCONNECTIONS; i++) {
    for (byte j = i ; j < NUMCONNECTIONS ; j++) { // Check each connection in turn

      byte pin1 = cablePins[i];
      byte pin2 = cablePins[j + 1];

      bool currentState = isConnected(pin1, pin2);

      if (currentState != lastState[i * NUMCONNECTIONS + j]) {
        Serial.print(F("Pin#"));
        Serial.print(pin1);
        Serial.print(F(" - Pin#"));
        Serial.print(pin2);
        Serial.println(currentState ? " : CONNECTED" : " : NOT CONNECTED");
        lastState[i * NUMCONNECTIONS + j] = currentState;
      }
    }
  }
}


// Tests whether an output pin is connected to a given INPUT_PULLUP pin
bool isConnected(byte OutputPin, byte InputPin) {

  // To test whether the pins are connected, set the first as output and the second as input
  pinMode(OutputPin, OUTPUT);
  pinMode(InputPin, INPUT_PULLUP);

  // Set the output pin LOW
  digitalWrite (OutputPin, LOW);

  // If connected, the LOW signal should be detected on the input pin
  // (Remember, we're using LOW not HIGH, because an INPUT_PULLUP will read HIGH by default)
  bool isConnected = !digitalRead(InputPin);

  // Set the output pin back to its default state
  pinMode(OutputPin, INPUT);
  pinMode(InputPin, INPUT);

  return isConnected;
}
1 Like

I have two questions please :

  • Would it be possible to change the message output ? Each time state changes, have a list of which pins are connected (if any) ?
    For example, if 7-10 are already connected, when connecting 8-12 :
    Pin#7 - Pin#10 : CONNECTED
    Pin#8 - Pin#12 : CONNECTED

And if 7-10 are deconnected :
Pin#8 - Pin#12 : CONNECTED

  • Second question : could you please explain me these or point me to the right direction :

bool lastState[NUMCONNECTIONS * NUMCONNECTIONS] = { false };
Why using * ?

lastState[i * NUMCONNECTIONS + j] = currentState;
What does it do ?

mark as solution

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.