You are on page 1of 2

//

// Arduino LEONARDO Digital Caliper reader by Barry Smalldon 18th July 2013.
// Intended for use as an X-Y mouse movement device with 2 x Digital Calipers,
this program reads
// the first Digital Caliper. An application of moving the Mouse in the X (hori
zontal) dimension with 1x
// Digital Caliper and use of Mouse Libraries from Arduino LEONDARD was impleme
nted, to prove the concept.
// This version reads Digital Caliper output from external pins, displays on se
rial port.
// Works with the Digital Calipers clock signal connected to interrupt D2 on Ar
duino LEONARDO board (D3 on Arduino UNO board).
// And Data from Calipers connected to Arduino shield pin D5 (CLOCK and DATA bo
th need voltage correction circuit).
// This Code Prints with a delay time 'delayTime' to the serial output.
//
int datapin = 5; // the Arduino D
ata input from Digital Caliper Output.
float oldValue = 0;
float newValue = 0;
int i;
int delayTime = 50; // this is the delay in
milliseconds to print the output to the screen.
long value;
void setup()
{
pinMode(datapin, INPUT); // D5 = DATA line from Digital Caliper go
es to Arduino digital pin 5.
Serial.begin(9600); // Serial Port BAUD Rate = 9600.
delay(500); // 500 Millisecond delay (this can be var
ied,works down to 1ms).
attachInterrupt(0,getBit,FALLING); // D0 = CLOCK Pin from Digital Caliper go
es to Arduino LEONARDO interrupt digital pin 2.
} // NOTE:- DATA and CLOCK both connected v
ia voltage correction cct (1.5v input to 3.3v output).
void loop()
{
if (i>=23)
{
//newValue = value/100.00;
newValue = value/100.00;
// Serial.print("new value before = ");
// Serial.println(newValue,2);
if (newValue != oldValue)
{
Serial.print("new value = ");
Mouse.move(newValue,Y);
Serial.println(newValue,2);
Serial.print("old value = ");
Serial.println(oldValue,2);
oldValue = newValue;
Serial.print("old value part 2 = ");
Serial.println(oldValue,2);
}
delay(delayTime);
newValue = 0;
i = 0;
value=0;
}
}
//
// Interrupt method.Found to be more reliable than reading 'datapin' constantly
in loop() method.
// getBit reads data from the caliper 'datapin' = a 24 bit number corresponding
to caliper reading.
// 24 bit reading = caliper ouptut value(bits 1 TO 20. e.g. 367 = 3.67mm) + 1 si
gn bit(bit 21) + 3 zero bits(bits 22 to 24).
//
void getBit()
{
if (digitalRead(datapin)==LOW) // High to Low transition is a digital '1'
on caliper Data line.
{
if (i<20) // only read the first 20 bits of data.
{
value |= 1 << i; // compound bitwise 'value' with a '1' in bit positio
n 'i'.
}
if (i==20)
{
value = -value; // if bit 21 is negative , the ouptut 'value' is negat
ed.
}
}
i++; // increment counter,regardless of datapin value.
}

You might also like