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 ?