Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
- //The code belongs to the following tutorial video: https://youtu.be/1_DzTDPmk3s
- //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.
- //Please visit this site where there is a very good explanation about everything.
- //http://wei48221.blogspot.com/2016/01/using-digital-caliper-for-digital-read_21.html?m=1
- /*
- If you want to support me, please buy the components using the following links:
- Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
- Arduino Nano with expansion board: https://www.banggood.com/custlink/vDGD9KnOHl
- Logic level shifter: https://www.banggood.com/custlink/KvmviZckv7
- LM7805 Voltage Regulator: https://www.banggood.com/custlink/GmmvSKQa9p
- Digital caliper ~0.03 mm accuracy: https://www.banggood.com/custlink/mKKD8ZWkpT
- */
- int bit_array[25]; // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
- unsigned long time_now; // For storing the time when the clock signal is changed from HIGH to LOW (falling edge trigger of data output).
- int CLOCK_PIN = 2;
- int DATA_PIN = 3;
- void setup() {
- Serial.begin(9600);
- pinMode(CLOCK_PIN, INPUT);
- pinMode(DATA_PIN, INPUT);
- }
- void loop() {
- while (digitalRead(CLOCK_PIN) == LOW) {} // If clock is LOW wait until it turns to HIGH
- time_now = micros();
- while (digitalRead(CLOCK_PIN) == HIGH) {} // Wait for the end of the HIGH pulse
- if ((micros() - time_now) > 500) { // If the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
- decode(); //decode the bit sequence
- }
- }
- void decode() {
- int sign = 1;
- int i = 0;
- float value = 0.0;
- float result = 0.0;
- bit_array[i] = digitalRead(DATA_PIN); // Store the 1st bit (start bit) which is always 1.
- while (digitalRead(CLOCK_PIN) == HIGH) {};
- for (i = 1; i <= 24; i++) {
- while (digitalRead(CLOCK_PIN) == LOW) { } // Wait until clock returns to HIGH
- bit_array[i] = digitalRead(DATA_PIN);
- while (digitalRead(CLOCK_PIN) == HIGH) {} // Wait until clock returns to LOW
- }
- for (i = 0; i <= 24; i++) { // Show the content of the bit array. This is for verification only.
- Serial.print(bit_array[i]);
- Serial.print(" ");
- }
- Serial.println();
- for (i = 1; i <= 20; i++) { // Turning the value in the bit array from binary to decimal.
- value = value + (pow(2, i-1) * bit_array[i]);
- }
- if (bit_array[21] == 1) sign = -1; // Bit 21 is the sign bit. 0 -> +, 1 => -
- if (bit_array[24] == 1) { // Bit 24 tells the measureing unit (1 -> in, 0 -> mm)
- result = (value*sign) / 2000.00;
- Serial.print(result,3); // Print result with 3 decimals
- Serial.println(" in");
- } else {
- result = (value*sign) / 100.00;
- Serial.print(result,2); // Print result with 2 decimals
- Serial.println(" mm");
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement