Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Wire.h"
- #include "Adafruit_MCP23017.h"
- enum {PASS, FAIL_NOTCONNECTED, FAIL_WRONGCONNECTED, FAIL_SHORTENED };
- // pin numbers for use at begin and end of cable
- const byte pinsCableBegin[]= { 2, 3, 4, 5, 6, 7};//, 8, 9, 10, 11, 12, 13};
- const byte pinsCableEnd[] = {A0,A1,A2,A3,A4,A5};//, 21, 22, 23, 24,25,26};
- const byte NUMCABLES=sizeof(pinsCableBegin);
- Adafruit_MCP23017 mcp;
- void setup() {
- Serial.begin(9600);
- mcp.begin();
- Wire.begin();
- Wire.beginTransmission(0x20);
- Wire.write(0x00); //GPIOA
- Wire.write(0x00); //Sets all of bank A as outputs
- if (sizeof(pinsCableBegin)!=sizeof(pinsCableEnd))
- {
- Serial.println("Pin configuration Error.");
- Serial.println("Fix declaration of pinsCableBegin[] and pinsCableEnd[] arrays!");
- while(1); // error stop with endless loop
- }
- Serial.println();
- Serial.println("################################################");
- Serial.println("# CABLE TESTER #");
- Serial.println("################################################");
- Serial.println();
- }
- void allPinsInputHigh()
- { // set all pins to INPUT_PULLUP in a for-loop
- for (byte i=0;i<NUMCABLES;i++)
- {
- pinMode(pinsCableBegin[i],INPUT_PULLUP); //enables internal 20k pullup resistors
- pinMode(pinsCableEnd[i],INPUT_PULLUP);
- //mcp.pinMode(pinsCableEnd[i], INPUT); //
- //mcp.pullUp(pinsCableEnd[i], HIGH); //
- }
- }
- void DoOneTest()
- {
- byte result;
- Serial.println();
- Serial.println("### TEST ###");
- for (byte i=0;i<NUMCABLES;i++) // test each pin
- {
- result= PASS; // initially there is no error found, assume PASS
- allPinsInputHigh();
- // first test is for continuity and OUTPUT/HIGH
- pinMode(pinsCableBegin[i], OUTPUT);
- if (digitalRead(pinsCableEnd[i])!=HIGH)
- {
- bitSet(result,FAIL_NOTCONNECTED);
- }
- // then check for continuity and OUTPUT/LOW
- digitalWrite(pinsCableBegin[i], LOW);
- if (digitalRead(pinsCableEnd[i])!=LOW)
- {
- bitSet(result,FAIL_NOTCONNECTED);
- }
- // next test: check for wrong connections to other pins
- for (byte j=0;j<NUMCABLES;j++)
- {
- if (j!=i && digitalRead(pinsCableEnd[j])==LOW)
- {
- bitSet(result, FAIL_WRONGCONNECTED);
- }
- }
- Serial.print("Line ");
- Serial.print(i+1);
- if (result== PASS) Serial.print(" PASS");
- else Serial.print(" FAIL");
- if (bitRead(result,FAIL_NOTCONNECTED)) Serial.print(" BREAK");
- if (bitRead(result,FAIL_WRONGCONNECTED)) Serial.print(" WRONG");
- if (bitRead(result,FAIL_SHORTENED)) Serial.print(" SHORT");
- Serial.println();
- }
- Serial.println("Test finished.");
- Serial.println();
- }
- void loop() {
- if (Serial.available())
- {
- DoOneTest();
- delay(20);
- while (Serial.available()) Serial.read(); // clear Serial input buffer
- }
- }
Add Comment
Please, Sign In to add comment