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