How to determine number of 'TRUE' pin connections

So I have a 2 x 4 hardware port matrix. 8 total ports (1/4" jacks). Any 2 can be connected together (ex: 1 +4, 2+8, etc..)

I have written a code which will go through each port and identify the other port it is connected to, and it will read through all eight ports one at a time from 1 through 8. But this reads each connection twice. Example: It will read port 1 + port 7, and then when code gets to port 7, it will read port 7 + port 1.

I am using :

// boolean T/F : is pin connected? False means NOT connected
boolean pin1 = false;
boolean pin2 = false;
boolean pin3 = false;
boolean pin4 = false;
boolean pin5 = false;
boolean pin6 = false;
boolean pin7 = false;
boolean pin8 = false;

to log which port has changed to low via the cable connection to another port. There will always be TWO true cases for 2 connected ports. 4 TRUE cases for 4 connected ports, etc....

I have a flashing LED which tells you that you have connected a set of ports. However , it flashes twice for each TRUE pair of port connections (ex: 1 +2 == 2+1). I only want it to flash ONCE for one connection, TWICE for two connections, etc...

So, is there a way to divide boolean logic /2 to get half of the true values? or go back and address the issue somewhere else?

Instead of checking each one against each other one, only compare them against the higher numbered ones.

So do 1-2 1-3 1-4 1-5 1-6 1-7 and then 2-3 2-4 2-5 2-6 2-7 and then 3-4 3-5 3-6 3-7 etc etc.

Only look for connections to higher numbered pins. Check Pin 1 against 2 through 8 but check 2 against 3 through 8 and pin 4 against 5 through 8. Once you have checked Pin 7 against Pin 8 you are done.

for (byte firstPin = 0; firstPin < PinCount-1; firstPin++)

  for (byte secondPin = firstPin+1; secondPin < PinCount; secondPin++)
    blinkIfConnected(firstPin, secondPin);

Hi John, thx for reply! My code includes a pin check similar to what you suggested. I will recode trying your +1 example.

// define pins 1-8 on teensy 3.2, and which func to point to...
PINPORTS ports[] =
{
  {1, portScan0},
  {2, portScan1},
  {3, portScan2},
  {4, portScan3},
  {5, portScan4},
  {6, portScan5},
  {7, portScan6},
  {8, portScan7},
};





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


  pinMode(LED, OUTPUT);



  for (uint8_t portCount = 0; portCount < sizeof(ports[0])  ; portCount++)
  {
    pinMode(ports[portCount].pin, OUTPUT);
    digitalWrite(ports[portCount].pin, HIGH);// set sll to HIGH to begin
  }



}

void loop() {


  //bool done = false;

  for (uint8_t portCount = 0; portCount < sizeof(ports[0]) ; portCount++)
  {
    digitalWrite(ports[portCount].pin, LOW);


    ports[portCount].func();//run through each port function

    PinTF ();// for serial monitor of each pin and converts bool false to true if connected

    delay(1000);

    if (pin1 == true && pin2 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin3 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin4 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin5 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin6 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin1 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin3 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin4 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin5 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin6 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin2 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin3 == true && pin4 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin3 == true && pin5 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin3 == true && pin6 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin3 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin3 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin4 == true && pin5 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin4 == true && pin6 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin4 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin4 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin5 == true && pin6 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin5 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin5 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin6 == true && pin7 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin6 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
    if (pin7 == true && pin8 == true) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }



    pin1 = false;
    pin2 = false;
    pin3 = false;
    pin4 = false;
    pin5 = false;
    pin6 = false;
    pin7 = false;
    pin8 = false;

    digitalWrite(LED, LOW);



    delay(500);

    digitalWrite(ports[portCount].pin, HIGH);//set all to high 


    ports[portCount].func();//run through each port function


    delay(500);// slow so i can see the pin change

  }


  // default if no ports connected
  //if (done == false)
  // {

  // }
portCount < sizeof(ports[0])

WARNING! The ‘sizeof’ operator gives the size of a data type IN BYTES. If you want the NUMBER OF ENTRIES in the array (and you DO), use:

portCount < (sizeof ports / sizeof ports[0])

XRAD:
But this reads each connection twice.

So count them all and divide the answer by 2?

...R

THX Guys!

Thx John. Fine point I did not know about byte size (). Although it runs fine, I will adjust to better tune the code . AND I learned something!

Robin, Thx for reply. That brings me back to my original post. I am not sure how to count TRUE connections. Should I create another variable that stores 'number of TRUE's' or a variable that stores 'number of connections'? and I am not sure exactly how to do this.

Also, since the total connections(TRUE values) are always an EVEN number (2,4,6,8), I could appoach this with '/2' or 'even -1' , etc... Just still not sure how to 'count' the TRUE's or the connections.....

THX for help!

XRAD:
Robin, Thx for reply. That brings me back to my original post. I am not sure how to count TRUE connections.

What do you mean by "TRUE"?

I have been assuming that you have things arranged so that if two pins are connected then reading either end will produce a known value using digitalRead(). For example set all except one pin to INPUT_PULLUP and then set the other pin to OUTPUT and LOW and check each input pin. Record the number of the INPUT pin that registers LOW. Just increment the count when you get a LOW. You could also record the pair that gave the LOW.

When you have tried all the INPUT pins with (say) pin2 as OUTPUT then repeat the same for pin3 as OUTPUT. Note that putting most of the pins to INPUT avoids the risk of a short circuit.

However I have to say I don't understand why you would need to do any of this. Maybe if you could explain that someone would have a simple solution.

...R

Robin, Thx for reply.

Reason for doing this is a challenge game. one person chooses 1 to 4 port connections (in the 2x4 1/4" hardware jack matrix), and the other players have to solve the 'port connections' in a certain time limit.

I can read the ports and see all connections without issue. I do need a way to record the port connections and I have tried using :

// boolean T/F : is pin connected? False means NOT connected
boolean pin1 = false;
boolean pin2 = false;
boolean pin3 = false;
boolean pin4 = false;
boolean pin5 = false;
boolean pin6 = false;
boolean pin7 = false;
boolean pin8 = false;

TRUE means the port is connected. But I do not think this is the best way to record whether a port is connected or not as I am confused on how to use this boolean data to show that say 1 set of ports connected, 2 sets connected, 3 sets connected, etc.....

I can get an LED to blink on and off when any pair of ports connected, but cannot figure a way to get boolean 'TRUE'/2 (as described above). Because currently, my code reads port 1+4 the same as 4+1. So LED blinks twice in the loop....

The reason I need to be able to determine if any correct ports are connected is so that when a players hits a button, an SD player will play 'one wire correct' 'two wires correct' etc....

Does this make sense?

I will continue working on code variants this evening......

I do not think creating 28 boolean statements (# of 1-8 port variants) is the way to go.....

THX

Because currently, my code reads port 1+4 the same as 4+1.

Well that shouldn’t be a problem anymore. You’ve been given a couple of good solutions to that. You should only compare pins with ones that have a higher pin number. If you do that then you just need to add one to a count variable when you find one.

Also please note you are talking about pins, not ports. A port is a whole collection of pins that share a register.

Yes, good suggestions. will re-code tonight as stated above...

But question about how to count boolean T/F's still out there( wondering if it can be done.... probably could put them into an array of variables....)

1/4" stereo jacks = mechanical 'ports'

'pins' for board connections...

will reserve future 'port' for register....

THX

But question about how to count boolean T/F's still out there( wondering if it can be done.... probably could put them into an array of variables....)

int count = 0;

///. Then in loop

if(the pins are connected){
   count = count + 1;
}

At the end count will be how many were connected.

Thx everyone! working now. :slight_smile: