DRO using Uno and HF(Pittsburh brand) digital caliper

I am working on a Digital Read Out(DRO) using a Uno and the sketch below. The sketch works with a logic converter (1.5v to 5v) between the Uno and the caliper data port. Bit 21 of the data sequence is the sign indicator, and data bit 24 is the units indicator. Can someone point me to a reference explaining this data manipulation, or give me the correct terminology to search with? I wish to modify the sketch so that depending on the unit indicator, the sketch converts the data correctly and prints the measurement and unit.

Chuck

//If readings are irratic values, change caliper battery
//DRO with sign inch

int i;
int sign;
long value;
float result;
int clockpin = 2;   
int datapin = 3;

unsigned long tempmicros;

 

 

void setup() {

  Serial.begin(115200);

  pinMode(clockpin, INPUT);

  pinMode(datapin, INPUT);

}




 void loop () {

  while (digitalRead(clockpin)==HIGH) {} //if clock is LOW wait until it turns to HIGH

  tempmicros=micros(); 

  while (digitalRead(clockpin)==LOW) {} //wait for the end of the HIGH pulse

  if ((micros()-tempmicros)>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() {

  sign=1;

  value=0;

  for (i=0;i<23;i++) {

    while (digitalRead(clockpin)==HIGH) { } //wait until clock returns to HIGH- the first bit is not needed

    while (digitalRead(clockpin)==LOW) {} //wait until clock returns to LOW

    if (digitalRead(datapin)==LOW) {

      if (i<20) {

        value|= 1<<i;

      }

      if (i==20) {

        sign=-1;

      }

    }

  }

  result=(value*sign)/2000.00;     

  Serial.print(result,4); //print result with 4 decimals
  Serial.println(" in");
  delay(500);

//Uncoment next lines 86-90 and comment out lines 79-82 to 
//dispay measurement in mm
//result=(value*sign)/100.00;     

//  Serial.print(result,3); //print result with 3 decimals
//  Serial.println(" mm");
//  delay(500);

}

you just have to clock in one more data cycle in your decode() function. Following the example of getting the sign bit, test which bit you are receiving (i) and if it is the last one, assign it to the units variable

//If readings are irratic values, change caliper battery
//DRO with sign inch

int i;
int sign;
int units;
long value;
float result;
int clockpin = 2;
int datapin = 3;

unsigned long tempmicros;

void setup() {

  Serial.begin(115200);
  pinMode(clockpin, INPUT);
  pinMode(datapin, INPUT);
}


void loop () {

  while (digitalRead(clockpin) == HIGH) {} //if clock is LOW wait until it turns to HIGH
  tempmicros = micros();
  while (digitalRead(clockpin) == LOW) {} //wait for the end of the HIGH pulse
  if ((micros() - tempmicros) > 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() {

  sign = 1;
  value = 0;
  units = 0;  // assume mm

  for (i = 0; i < 24; i++) {
    while (digitalRead(clockpin) == HIGH) { } //wait until clock returns to HIGH- the first bit is not needed
    while (digitalRead(clockpin) == LOW) {} //wait until clock returns to LOW
    if (digitalRead(datapin) == LOW) {
      if (i < 20) {
        value |= 1 << i;
      }
      if (i == 20) {
        sign = -1;
      }
      if (i == 23 ) {
        units = 1;  // thousands
      }
    }
  }
  if ( units ) {
    result = (value * sign) / 2000.00;
    Serial.print(result, 4); //print result with 4 decimals
    Serial.println(" in");
  } else {
    result = (value * sign) / 100.00;
    Serial.print(result, 3); //print result with 3 decimals
    Serial.println(" mm");
  }
  delay(500);
}

@OP
Hi!
May I step in and ask some questions?
I am using 3 DRO scales bought from Banggood. L
The link is https://www.banggood.com/150200300mm-Digital-Readout-DRO-Remote-LCD-Linear-Scale-for-Milling-Machines-or-Lathes-p-1228981.html?rmmds=myorder&ID=515969&cur_warehouse=CN.
That would be a different matter but maybe You have valuable knowledge.

I did mail the producer asking for a way to read the scales using an Arduino. They didn't answer. The scale is connected to the display unit using an USB C connector. That's for my mini mill in order to make a CNC machine.

For my mini lathe I have ordered 2 digital calipers from China. Whether they have an USB port I don't know. They likely don't have that.

Could You You attach a link to the company where You bought Your calipers? Reading Your code, and the help from blh64, I am really interested in making something for my lathe.

Try this site, lot's of useful information.

This site has a lot of information on Chinese scales.
http://www.shumatech.com
I have one of the Shumatech and one of the TouchDRO's and they both work great.
The Sumatech's are no longer available.
The ToucDRO had a program for the Arduino but went to the MSP430 Launchpad, which is pretty easy to work with if you have used an Arduino.

@detown
Lots of thanks! A real gold nugget for me!
Now I'm sure I'm using iGaging scales and readouts. Being quite comfortable with Arduino UNO I hope to build and program my own touch panel functions.
Attache a picture.

Well, I got a couple of sketches working that displays the Harbor Freight caliper reading on an LCD. Still cannot get the output to change based on the bit that reflects units. I had to write the sketches based on the caliper mode, which is controlled by the mm/in switch. Instead, I used the calculation method for mm with the mode in mm, and the regular way of converting mm to inch. It then prints both values. The other sketch calculates the reading for inch, and converts that to mm and prints both. Both versions checked with an Excell spread sheet that converts the units.
I really don't know enough C++ to do it differently.
Thanks for all the suggestions and help.

Greate news! Any code that does the job is a good code. Smart and malfunctioning code makes nnobody happy. It's the same here. I know some about C but C++ I never practised but quuite a few projects "have landed". Keep on, have fun!

Hi!
I'm new here and in the world of Arduino, but however.
I started my litle project with this caliper stuff and it is working.

So, what I wish to achieve is:
I'm not sure what this line means in the above code:

" value|= 1<<i; "

and I'm searching for a solution to transfer the bit's to the terminal windows,
not the decoded data.

I was trying several thinks what was on my mind but it was not working for me.

Can somebody pls help me to mod the code so I can transfer the bit sequence to the terminal?

Thank you very much.

Use Arduino reference! Look at this: Arduino Reference - Arduino Reference
Bit 0 of value is set to one and value is shifted left one step.

Jodank:
Hi!
I'm new here and in the world of Arduino, but however.
I started my litle project with this caliper stuff and it is working.

So, what I wish to achieve is:
I'm not sure what this line means in the above code:

" value|= 1<<i; "

and I'm searching for a solution to transfer the bit's to the terminal windows,
not the decoded data.

I was trying several thinks what was on my mind but it was not working for me.

Can somebody pls help me to mod the code so I can transfer the bit sequence to the terminal?

Thank you very much.

Why not start a new topic of Your own? That would be better.

Thank you for your reply, I really appreciate it.

I found the answer from the link you shared:

x |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.

I didn't started a new topic cos I won't hijack this from the original author.
I feel somehow does my question is related to this topic.

But I will open a new later when I run in some trouble.
For now I'm playing with bit-shifting... :slight_smile: