Advertisement
CuriousScientist

Arduino code for digital caliper

Aug 3rd, 2019
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial video: https://youtu.be/1_DzTDPmk3s
  3.  
  4. //The code is a copy of the following code. I did not modify it and I do not own this code. I just save it as a backup copy.
  5. //Please visit this site where there is a very good explanation about everything.
  6. //http://wei48221.blogspot.com/2016/01/using-digital-caliper-for-digital-read_21.html?m=1
  7.  
  8. /*
  9. If you want to support me, please buy the components using the following links:
  10. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  11. Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
  12. Logic level shifter: https://www.banggood.com/custlink/KvmviZckv7
  13. LM7805 Voltage Regulator: https://www.banggood.com/custlink/GmmvSKQa9p
  14. Digital caliper ~0.03 mm accuracy: https://www.banggood.com/custlink/mKKD8ZWkpT
  15. */
  16.  
  17.  
  18. int bit_array[25];        // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
  19. unsigned long time_now;   // For storing the time when the clock signal is changed from HIGH to LOW (falling edge trigger of data output).
  20.  
  21. int CLOCK_PIN = 2;
  22. int DATA_PIN = 3;
  23.  
  24. void setup() {
  25.   Serial.begin(9600);
  26.   pinMode(CLOCK_PIN, INPUT);
  27.   pinMode(DATA_PIN, INPUT);
  28. }
  29.  
  30. void loop() {
  31.   while (digitalRead(CLOCK_PIN) == LOW) {}  // If clock is LOW wait until it turns to HIGH
  32.   time_now = micros();
  33.   while (digitalRead(CLOCK_PIN) == HIGH) {} // Wait for the end of the HIGH pulse
  34.   if ((micros() - time_now) > 500) {        // If the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
  35.     decode(); //decode the bit sequence
  36.   }
  37. }
  38.  
  39. void decode() {
  40.   int sign = 1;
  41.   int i = 0;
  42.   float value = 0.0;
  43.   float result = 0.0;
  44.  
  45.   bit_array[i] = digitalRead(DATA_PIN);       // Store the 1st bit (start bit) which is always 1.
  46.   while (digitalRead(CLOCK_PIN) == HIGH) {};
  47.  
  48.   for (i = 1; i <= 24; i++) {
  49.     while (digitalRead(CLOCK_PIN) == LOW) { } // Wait until clock returns to HIGH
  50.     bit_array[i] = digitalRead(DATA_PIN);  
  51.     while (digitalRead(CLOCK_PIN) == HIGH) {} // Wait until clock returns to LOW
  52.   }
  53.  
  54.   for (i = 0; i <= 24; i++) {                 // Show the content of the bit array. This is for verification only.
  55.     Serial.print(bit_array[i]);
  56.     Serial.print(" ");
  57.   }
  58.     Serial.println();
  59.  
  60.   for (i = 1; i <= 20; i++) {                 // Turning the value in the bit array from binary to decimal.
  61.       value = value + (pow(2, i-1) * bit_array[i]);
  62.   }
  63.  
  64.   if (bit_array[21] == 1) sign = -1;          // Bit 21 is the sign bit. 0 -> +, 1 => -
  65.  
  66.   if (bit_array[24] == 1) {                   // Bit 24 tells the measureing unit (1 -> in, 0 -> mm)
  67.      result = (value*sign) / 2000.00;
  68.      Serial.print(result,3);                   // Print result with 3 decimals
  69.      Serial.println(" in");
  70.   } else {
  71.      result = (value*sign) / 100.00;  
  72.      Serial.print(result,2);                   // Print result with 2 decimals
  73.      Serial.println(" mm");  
  74.   }
  75.   delay(1000);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement