Reading data from a generic digital caliper with atmega328p standalone and display the readings on 16X2 lcd

Good day everyone, please i'm having a hard time getting the correct reading from a generic caliper using atmega standalone and non I2C lcd. i have tried some codes i found from youtube videos, but not getting it right. some of the readings are out of range and it keeps changing even without sliding the digital caliper. I will really appreciate if someone can help me out. Thank you.

https://sites.google.com/site/marthalprojects/home/arduino/arduino-reads-digital-caliper?pli=1&authuser=0

#include <LiquidCrystal.h>

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;

// Initialize the LCD with standard interface pins: RS, EN, D4, D5, D6, D7
// Adjust these pin numbers if your physical wiring differs!

LiquidCrystal lcd(6, 5, 7, 8, 9, 10);

void setup() {
  Serial.begin(9600);
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);

  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  lcd.print("Digital Caliper");
  delay(1500);
  lcd.clear();
}

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 => -

  // Prepare LCD for the new reading
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Measured Value:");
  lcd.setCursor(0, 1);

  if (bit_array[24] == 1) {                   // Bit 24 tells the measuring unit (1 -> in, 0 -> mm)
     result = (value * sign) / 2000.00;
     Serial.print(result, 3);                 // Print result with 3 decimals
     Serial.println(" in");
     
     // Display on LCD
     lcd.print(result, 3);
     lcd.print(" in");
  } else {
     result = (value * sign) / 100.00;  
     Serial.print(result, 2);                 // Print result with 2 decimals
     Serial.println(" mm");  
     
     // Display on LCD
     lcd.print(result, 2);
     lcd.print(" mm");
  }
  delay(1000);
}

Please post code and schematics here.

Are you using the transistor level translator or the crazy resistive divider set-up

That "novel approach to the logic level problem." looks silly at best.

I've updated the post with the code and circuit diagram

Where is the level translator?
See post #3 and answer the question

This isn't connected to anything.
Draw the actual wires, label everything, now take a picture of the drawing. Do two drawings, one for just the power the other your actual project.

Thank You but the request was for schematics, not a Fritzing picture.

Transistor level translator

Try this code. It will check the clock pin. If the count is less than 24 then there is no clock.

#include <LiquidCrystal.h>

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).
unsigned long start;

int CLOCK_PIN = 2;
int DATA_PIN = 3;

// Initialize the LCD with standard interface pins: RS, EN, D4, D5, D6, D7
// Adjust these pin numbers if your physical wiring differs!

LiquidCrystal lcd(6, 5, 7, 8, 9, 10);

void setup()
{
  Serial.begin(9600);
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);

  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  lcd.print("Digital Caliper");
  delay(1500);
  lcd.clear();
}

void loop()
{
  int a = 0, b = 0;
  unsigned long count = 0;
  start = millis();

  while ((millis() - start) < 1000)  // Check CLK for 1 second
  {
    if (a != b)
    {
      count++;
      b = a;
    }
    a = digitalRead(CLOCK_PIN);
  }
  Serial.print("Count = ");
  Serial.println(count);
  delay(2000);
}

ok sir, let me try it. thank you

this is the output

Count = 252
Count = 252
Count = 248
Count = 252
Count = 288
Count = 252
Count = 296
Count = 238
Count = 278
Count = 256
Count = 252
Count = 252
Count = 242
Count = 254
Count = 252
Count = 252
Count = 254

You are just picking up stray 60Hz mains voltage. It means either that your clock wires are not connected or your caliper clock pin is not connected to anything

this is the setup and below is the second output.

Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 21
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1
Count = 1

Too many long wires that can pick up stray signals. Also don't put your breadboard anywhere near the laptop, they generate a lot of noise.

If you still can't get it to work, then you will need an oscilloscope to look at the clock signal.

A 9.6k "clock" registers both transitions (near 19.2k).

No clock registers zero... or the odd pulse of RFI or wiggly connections.

I suggest if you need accurate help you post an annotated schematic. Your squares, lines, etc go to empty boxes, what are they. Your links end in a dead end.

Any progress?

sorry for the late reply, my network went bad for the past fe days. i tested it oscilloscope but there was no data from it.

Well, that's the end of that project.