decoding pair-wise inputs

Hi all,

I'm looking for an idea to solve the following: given 4 input terminals - A, B, C and D (simple electrical wires), the controller needs to decode which of pairs are connected. For example: A-C, or B-D, etc...

Any ideas?

Thanks in advance,
Jonathan

I haven't tested it but this should give you a good jumping off point. If it works as I intended the bools AB, AC, AD... should be contently set to what pins are connected.

void setup() {}

//defining pins, changes these to the pins you want to check
const int A = 1;
const int B = 2;
const int C = 3;
const int D = 4;

//defining connections
bool AB = false;
bool AC = false;
bool AD = false;
bool BC = false;
bool BD = false;
bool CD = false;

void loop() {
  
  pinMode(A,OUTPUT);//set one pin to output 
  pinMode(B,INPUT);  //set the rest to input
  pinMode(C,INPUT);
  pinMode(D,INPUT);
  digitalWrite(A, HIGH);
  AB = digitalRead(B); //if the pin is high(true) it means it's connected to the only pin outputting
  AC = digitalRead(C);
  AD = digitalRead(D);

  pinMode(A,INPUT);
  pinMode(B,OUTPUT);
  pinMode(C,INPUT);
  pinMode(D,INPUT);
  digitalWrite(B, HIGH);
  BC = digitalRead(C); //we don't need to check as many because they have already been looked at, eg A connected to B is the same ans B connected to A
  BD = digitalRead(D);

  pinMode(A,INPUT);
  pinMode(B,INPUT);
  pinMode(C,OUTPUT);
  pinMode(D,INPUT);
  digitalWrite(C, HIGH);
  CD = digitalRead(C);

//D never needs to be an ouput because it is checked by the rest
}

If it works as I intended

With floating inputs? I doubt it.

const int A = 1;

Suggest you not use pin 1 as it is the serial TX pin.

With floating inputs? I doubt it.

Change the INPUT to INPUT_PULLUP.

DigitalWrite() the OUTPUT pin LOW instead of HIGH. An output pin written LOW is connected to ground and can sink current.

The digitalRead() if the INPUT_PULLUP pin will be LOW if it is connected to the pin which is output and low.

void setup() {}

//defining pins, changes these to the pins you want to check
const int A = 5; //1;
const int B = 2;
const int C = 3;
const int D = 4;

//defining connections
bool AB = false;
bool AC = false;
bool AD = false;
bool BC = false;
bool BD = false;
bool CD = false;

void loop() {
  
  pinMode(A,OUTPUT);//set one pin to output 
  pinMode(B,INPUT_PULLUP);  //set the rest to INPUT_PULLUP
  pinMode(C,INPUT_PULLUP);
  pinMode(D,INPUT_PULLUP);
  digitalWrite(A, LOW);
  AB = digitalRead(B); //if the pin is LOW(false) it means it's connected to the only pin outputting
  AC = digitalRead(C);
  AD = digitalRead(D);

  pinMode(A,INPUT_PULLUP);
  pinMode(B,OUTPUT);
  pinMode(C,INPUT_PULLUP);
  pinMode(D,INPUT_PULLUP);
  digitalWrite(B, LOW);
  BC = digitalRead(C); //we don't need to check as many because they have already been looked at, eg A connected to B is the same ans B connected to A
  BD = digitalRead(D);

  pinMode(A,INPUT_PULLUP);
  pinMode(B,INPUT_PULLUP);
  pinMode(C,OUTPUT);
  pinMode(D,INPUT_PULLUP);
  digitalWrite(C, LOW);
  //CD = digitalRead(C);
  CD = digitalRead(D);
  

//D never needs to be an ouput because it is checked by the rest
}

WORKS!!

thx!

one thing:

when A-B is connected, I get 'AB'
when C-D is connected, I get 'CD'

but, when both A-B and C-D are connected,
I get: AB, AC, AD, BC and CD...

?

but, when both A-B and C-D are connected,
I get: AB, AC, AD, BC and CD...

Yes you will. Why would you expect anything else?

if A-B and C-D are connected, i'd like to get only: 'AB' and 'CD' (as the rest are not connected..)

Try (untested):

//defining pins, changes these to the pins you want to check
const byte pins[4] = {5, 2, 3, 4};
const char names[4] = {'A', 'B', 'C', 'D'};

void setup() {
  for (byte i=0; i<4; i++) pinMode(pins[i], INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {

  for (byte i=0; i<3; i++) {
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], LOW);
    for (byte j=i+1; j<4; j++) {
      if (digitalRead(pins[j]) == LOW) {
        Serial.print(names[i]);
        Serial.print("-");
        Serial.print(names[j]);
        Serial.print(" ");
      }
    }
    pinMode(pins[i], INPUT_PULLUP);
  }
  Serial.println();
  
}

WORKS !!! :slight_smile: