Go Down

### Topic: decoding pair-wise inputs (Read 215 times)previous topic - next topic

#### rubinj

##### May 16, 2019, 05:14 pm
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?

Jonathan

#### PixelDeath

#1
##### May 16, 2019, 07:54 pm
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.

Code: [Select]
`void setup() {}//defining pins, changes these to the pins you want to checkconst int A = 1;const int B = 2;const int C = 3;const int D = 4;//defining connectionsbool 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}`

#### PaulRB

#2
##### May 16, 2019, 08:26 pmLast Edit: May 16, 2019, 08:26 pm by PaulRB
Quote
If it works as I intended
With floating inputs? I doubt it.

#### larryd

#3
##### May 16, 2019, 08:30 pm
const int A = 1;

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

No technical PMs.
If you need clarification, ask for help.

#### cattledog

#4
##### May 16, 2019, 09:58 pmLast Edit: May 16, 2019, 10:00 pm by cattledog
Quote
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.

Code: [Select]
`void setup() {}//defining pins, changes these to the pins you want to checkconst int A = 5; //1;const int B = 2;const int C = 3;const int D = 4;//defining connectionsbool 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}`

#5
WORKS!!

thx!

#### rubinj

#6
##### May 17, 2019, 08:54 am
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...

?

#### Grumpy_Mike

#7
##### May 17, 2019, 09:21 am
Quote
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?

#### rubinj

#8
##### May 17, 2019, 10:16 am
if A-B and C-D are connected, i'd like to get only: 'AB' and 'CD' (as the rest are not connected..)

#### PaulRB

#9
##### May 17, 2019, 05:37 pmLast Edit: May 17, 2019, 05:41 pm by PaulRB
Try (untested):
Code: [Select]
`//defining pins, changes these to the pins you want to checkconst 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();  }`

#10
WORKS !!!

Go Up