dm41nes

2017-09-06 - Cable Tester with Port Expander

Sep 6th, 2017
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include "Wire.h"
  2. #include "Adafruit_MCP23017.h"
  3.  
  4. enum {PASS, FAIL_NOTCONNECTED, FAIL_WRONGCONNECTED, FAIL_SHORTENED };
  5.  
  6. // pin numbers for use at begin and end of cable
  7. const byte pinsCableBegin[]= { 2, 3, 4, 5, 6, 7};//, 8, 9, 10, 11, 12, 13};
  8. const byte pinsCableEnd[] = {A0,A1,A2,A3,A4,A5};//, 21, 22, 23, 24,25,26};
  9.  
  10. const byte NUMCABLES=sizeof(pinsCableBegin);
  11.  
  12. Adafruit_MCP23017 mcp;
  13.  
  14. void setup() {
  15.  
  16. Serial.begin(9600);
  17.  
  18. mcp.begin();
  19. Wire.begin();
  20. Wire.beginTransmission(0x20);
  21. Wire.write(0x00); //GPIOA
  22. Wire.write(0x00); //Sets all of bank A as outputs
  23.  
  24.  
  25. if (sizeof(pinsCableBegin)!=sizeof(pinsCableEnd))
  26. {
  27. Serial.println("Pin configuration Error.");
  28. Serial.println("Fix declaration of pinsCableBegin[] and pinsCableEnd[] arrays!");
  29. while(1); // error stop with endless loop
  30. }
  31. Serial.println();
  32. Serial.println("################################################");
  33. Serial.println("# CABLE TESTER #");
  34. Serial.println("################################################");
  35. Serial.println();
  36. }
  37.  
  38. void allPinsInputHigh()
  39. { // set all pins to INPUT_PULLUP in a for-loop
  40. for (byte i=0;i<NUMCABLES;i++)
  41. {
  42. pinMode(pinsCableBegin[i],INPUT_PULLUP); //enables internal 20k pullup resistors
  43. pinMode(pinsCableEnd[i],INPUT_PULLUP);
  44. //mcp.pinMode(pinsCableEnd[i], INPUT); //
  45. //mcp.pullUp(pinsCableEnd[i], HIGH); //
  46. }
  47. }
  48.  
  49.  
  50. void DoOneTest()
  51. {
  52. byte result;
  53. Serial.println();
  54. Serial.println("### TEST ###");
  55. for (byte i=0;i<NUMCABLES;i++) // test each pin
  56. {
  57. result= PASS; // initially there is no error found, assume PASS
  58. allPinsInputHigh();
  59. // first test is for continuity and OUTPUT/HIGH
  60. pinMode(pinsCableBegin[i], OUTPUT);
  61. if (digitalRead(pinsCableEnd[i])!=HIGH)
  62. {
  63. bitSet(result,FAIL_NOTCONNECTED);
  64. }
  65. // then check for continuity and OUTPUT/LOW
  66. digitalWrite(pinsCableBegin[i], LOW);
  67. if (digitalRead(pinsCableEnd[i])!=LOW)
  68. {
  69. bitSet(result,FAIL_NOTCONNECTED);
  70. }
  71.  
  72. // next test: check for wrong connections to other pins
  73. for (byte j=0;j<NUMCABLES;j++)
  74. {
  75. if (j!=i && digitalRead(pinsCableEnd[j])==LOW)
  76. {
  77. bitSet(result, FAIL_WRONGCONNECTED);
  78. }
  79. }
  80.  
  81. Serial.print("Line ");
  82. Serial.print(i+1);
  83. if (result== PASS) Serial.print(" PASS");
  84. else Serial.print(" FAIL");
  85. if (bitRead(result,FAIL_NOTCONNECTED)) Serial.print(" BREAK");
  86. if (bitRead(result,FAIL_WRONGCONNECTED)) Serial.print(" WRONG");
  87. if (bitRead(result,FAIL_SHORTENED)) Serial.print(" SHORT");
  88. Serial.println();
  89. }
  90. Serial.println("Test finished.");
  91. Serial.println();
  92. }
  93.  
  94. void loop() {
  95. if (Serial.available())
  96. {
  97. DoOneTest();
  98. delay(20);
  99. while (Serial.available()) Serial.read(); // clear Serial input buffer
  100. }
  101. }
Add Comment
Please, Sign In to add comment