Created
May 10, 2023 22:49
-
-
Save DaelonSuzuka/b949d750a5c21163701cefc309318393 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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