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.
#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);
}
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.
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);
}
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
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.