dm41nes

Cable Tester Arduino

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