I added a connector to my cheap Chinese Mastercraft calipers. The data looks good when I view it on my scope and I can understand it just fine. There's a Ground, Clock, Data and 1.5V pin.
It sends out data 4 bits at a time with a small gap between each 4 bit chunk (nibble) 6 times so it sends out 24 bits then a long 120ms gap then another 24 bits, all LSB first. bit 21 defines whether it's a negative number or not.
Lots of people have written about this and this site has images that match what mine sends out.
http://nut-bolt.nl/2012/reading-digital-calipers-with-an-arduino/I like that the code on this site uses interrupts because it seems logical for this type of application. I've never used interrupts before and this looked like a good way to learn about the topic.
I built a simple circuit with two NPNs to shift the 1.5V signal closer to 5V so an Arduino can read it but then realized that this code looks like it was written for a signal that's NOT inverted, unlike what I've seen from other websites. I simply used a 40106 to invert the already level shifted Clock and Data and now it looks exactly like the screenshots on that website.
QUESTIONS:
The code on the site doesn't declare any variables so it's up to you to decide where to put them (the
scope), which
types are appropriate, which ones need to be
volatile and which should be
global.
Can anyone give me advice on this?
millis() is supposedly an unsigned long so it seems that
lastInterrupt and
now should also be?
Also... on arduino.cc it says
Note
Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
This code DOES use milis(),
isn't that a problem?
I've tried different stuff after declaring all the variables as best as I could figure out just so it compiles and see that the arduino is reading the data off the data pin correctly. By doing a Serial.print (data); the ones and zeros in Serial Monitor match what's on my scope but the rest of the code isn't doing what it should because I'm getting weird results for finalValue.
I've tried it on a Leonardo with the interrupt set to 1 instead of 0 because the values for 0 and 1 are backwards on a 32U4 compared to a regular UNO/328 arduino and all I get is stuff like -9718 0 -1 -2 -1 -4 -2 -4 -1 -2 -64 -1 -8 -1 -32 -16 -1 -2 -1 -2 -1
On an Uno with the same code (but the interrupt set back to 0) I get strange numbers like
4832 0 3 24832 4 24832 2 24832 2 -12798 206 24832 0
Here's a screenshot of the data a clock displaying 0.02mm
If you read the bits on the falling edge of the clock you always get an extra 1 at the start but the code seems to take care of that.