sinned6915

caliper sketch

Oct 14th, 2020 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. based on the original work done by
  3. (c)2018 Pawel A. Hernik
  4. YouTube video:
  5. https://youtu.be/RIt08GJH2IA
  6. https://www.instructables.com/Fun-With-OLED-Display-and-Arduino/
  7.  
  8. */
  9.  
  10. /*
  11. Cheap digital caliper pinout (from top left):
  12. LOOKING AT THE FACE OF THE DEVICE
  13. #1 - GND (Brown) | BLACK IN VIDEO
  14. #2 - DAT (Blue) | GREEN IN VIDEO
  15. #3 - CLK (WH/Blue) | BLUE IN VIDEO
  16. #4 - VCC 1.5-1.8V (WH/BRown) | RED IN VIDEO
  17. */
  18. #include <Wire.h>
  19. #include <Adafruit_GFX.h>
  20. #include <Adafruit_SSD1306.h>
  21.  
  22. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  23. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  24.  
  25. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  26. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  27.  
  28. #define CLOCK_PIN 2 // wh/blue CLK
  29. #define DATA_PIN 3 // blue DAT
  30. // These are pins A2 & A3
  31. // digitalWrite(pin, value)
  32. // for grabbing power and ground from other pins like A0 and A1
  33.  
  34. void setup()
  35. {
  36. Serial.begin(115200);
  37.  
  38. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  39. Serial.println(F("SSD1306 allocation failed"));
  40. for(;;);
  41. }
  42.  
  43. Serial.println("Ready");
  44.  
  45. pinMode(CLOCK_PIN, INPUT);
  46. pinMode(DATA_PIN, INPUT);
  47.  
  48. display.clearDisplay();
  49. display.setTextSize(1);
  50. display.setTextColor(WHITE);
  51. display.setCursor(10, 0);
  52. // Display static text
  53. Serial.println("Hello World! Setup");
  54. display.println("Hello World! Setup");
  55. display.display();
  56. delay(2500);
  57. display.clearDisplay();
  58. display.setCursor(10, 0);
  59. Serial.println("Caliper Setup");
  60. display.println("Caliper Setup");
  61. display.display();
  62. }
  63.  
  64. char buf[20];
  65. unsigned long tmpTime;
  66. int sign;
  67. int inches;
  68. int cm;
  69. long value;
  70. float result;
  71.  
  72. void loop()
  73. {
  74. while(digitalRead(CLOCK_PIN)==LOW) {}
  75. tmpTime=micros();
  76. // while(digitalRead(CLOCK_PIN)==HIGH) {}
  77. while(digitalRead(CLOCK_PIN)==HIGH) { Serial.println("Clock is still high."); }
  78. if((micros()-tmpTime)<500) return;
  79. readCaliper();
  80. Serial.println(result,2); Serial.println(inches);
  81. buf[0]=' ';
  82. dtostrf(result,6,3,buf+1); strcat(buf," in ");
  83. // display.setCursor(20, 0);
  84. // display.println(buf);
  85. Serial.println(result,2); Serial.println(inches);
  86. Serial.println("Inch Rreading");
  87. Serial.println(buf);
  88.  
  89. dtostrf(result*2.54,6,3,buf+1); strcat(buf," cm ");
  90. // display.setCursor(30, 0);
  91. // display.println(buf);
  92. Serial.println(result*2.54, 2); Serial.println(cm);
  93. Serial.println("cm Rreading");
  94. Serial.println(buf);
  95. delay(250);
  96. // display.display();
  97. }
  98.  
  99. void readCaliper()
  100. {
  101. sign=1;
  102. value=0;
  103. inches=0;
  104. for(int i=0;i<24;i++) {
  105. while(digitalRead(CLOCK_PIN)==LOW) {}
  106. while(digitalRead(CLOCK_PIN)==HIGH) {}
  107. if(digitalRead(DATA_PIN)==HIGH) {
  108. if(i<20) value|=(1<<i);
  109. if(i==20) sign=-1;
  110. if(i==23) inches=1; // doesn't work with my caliper, always returns inches
  111. }
  112. }
  113. result=(value*sign)/(inches?2000.0:100.0);
  114. }
Add Comment
Please, Sign In to add comment