sinned6915

caliper sketch

Oct 21st, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 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.  
  29.  
  30. #define DATA_PIN 1 // green
  31. #define CLOCK_PIN 2 // white-green
  32.  
  33. // These are pins A2 & A3
  34. // digitalWrite(pin, value)
  35. // for grabbing power and ground from other pins like A0 and A1
  36.  
  37. void setup()
  38. {
  39. Serial.begin(9600);
  40.  
  41. pinMode(0, OUTPUT); // sets the digital pin as output
  42. pinMode(3, OUTPUT); // sets the digital pin as output
  43.  
  44. digitalWrite(0, LOW); // GROUND white-orange
  45. digitalWrite(3, HIGH); // +5V orange
  46.  
  47. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  48. Serial.println(F("SSD1306 allocation failed"));
  49. for(;;);
  50. }
  51.  
  52. Serial.println("Ready");
  53.  
  54. pinMode(CLOCK_PIN, INPUT);
  55. pinMode(DATA_PIN, INPUT);
  56.  
  57. display.clearDisplay();
  58. display.setTextSize(1);
  59. display.setTextColor(WHITE);
  60. display.setCursor(10, 0);
  61. // Display static text
  62. Serial.println("Hello World! Setup");
  63. // display.println("Hello World! Setup");
  64. // display.display();
  65. delay(500);
  66. // display.clearDisplay();
  67. // display.setCursor(10, 0);
  68. Serial.println("Caliper Setup");
  69. // display.println("Caliper Setup");
  70. // display.display();
  71. }
  72.  
  73. char buf[20];
  74. unsigned long tmpTime;
  75. int sign;
  76. int inches;
  77. int cm;
  78. long value;
  79. float result;
  80.  
  81. void loop()
  82. {
  83. while(digitalRead(CLOCK_PIN)==LOW) {}
  84. tmpTime=micros();
  85. while(digitalRead(CLOCK_PIN)==HIGH) {}
  86. // while(digitalRead(CLOCK_PIN)==HIGH) { Serial.println("Clock is still high."); }
  87. if((micros()-tmpTime)<500) return;
  88. readCaliper();
  89. Serial.println(result,2); Serial.println(inches);
  90. buf[0]=' ';
  91. dtostrf(result,6,3,buf+1); strcat(buf," in ");
  92. // display.setCursor(20, 0);
  93. // display.println(buf);
  94. // Serial.println(result,2); Serial.println(inches);
  95. // Serial.println("Inch Rreading");
  96. Serial.println(buf);
  97.  
  98. dtostrf(result*2.54,6,3,buf+1); strcat(buf," cm ");
  99. // display.setCursor(30, 0);
  100. // display.println(buf);
  101. // Serial.println(result*2.54, 2); Serial.println(cm);
  102. // Serial.println("cm Rreading");
  103. Serial.println(buf);
  104. delay(250);
  105. // display.display();
  106. }
  107.  
  108. void readCaliper()
  109. {
  110. sign=1;
  111. value=0;
  112. inches=0;
  113. for(int i=0;i<24;i++) {
  114. while(digitalRead(CLOCK_PIN)==LOW) {}
  115. while(digitalRead(CLOCK_PIN)==HIGH) {}
  116. if(digitalRead(DATA_PIN)==HIGH) {
  117. if(i<20) value|=(1<<i);
  118. if(i==20) sign=-1;
  119. if(i==23) inches=1; // doesn't work with my caliper, always returns inches
  120. }
  121. }
  122. result=(value*sign)/(inches?2000.0:100.0);
  123. }
Add Comment
Please, Sign In to add comment