Skip to content

Instantly share code, notes, and snippets.

@DaelonSuzuka
Created May 10, 2023 22:49
Show Gist options
  • Save DaelonSuzuka/b949d750a5c21163701cefc309318393 to your computer and use it in GitHub Desktop.
Save DaelonSuzuka/b949d750a5c21163701cefc309318393 to your computer and use it in GitHub Desktop.
// update this section to reflect the pin numbers in the box
#define LED_GREEN_PIN 3
#define LED_RED_PIN 2
#define NUMBER_OF_PINS 4
int cableA[NUMBER_OF_PINS] = {6, 7, 8, 9};
int cableB[NUMBER_OF_PINS] = {15, 14, 16, 10};
void setup() {
// put your setup code here, to run once:
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
for (int i = 0; i < NUMBER_OF_PINS; i++) {
pinMode(cableA[i], OUTPUT);
pinMode(cableB[i], INPUT);
}
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(cableA[0], HIGH);
}
void reset_pins(void) {
for (int i = 0; i < NUMBER_OF_PINS; i++) {
digitalWrite(cableA[i], LOW);
}
}
bool scan_pins(void) {
for (int i = 0; i < NUMBER_OF_PINS; i++) {
reset_pins();
digitalWrite(cableA[i], HIGH);
for (int j = 0; j < NUMBER_OF_PINS; j++) {
if (i == j) {
if (!digitalRead(cableB[j])) {
return false;
}
} else {
if (digitalRead(cableB[j])) {
return false;
}
}
}
}
return true;
}
void loop() {
if (scan_pins()) {
digitalWrite(LED_GREEN_PIN, HIGH);
digitalWrite(LED_RED_PIN, LOW);
} else {
digitalWrite(LED_GREEN_PIN, LOW);
digitalWrite(LED_RED_PIN, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment