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...
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
}
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
}