I'm having a problem when trying to read 3 calipers simultaneously using one arduino.
I hook up the caliper into arduino and use the code that is described in the following tutorial:
In this tutorial j44 explain how to build a circuit to feed the calipers with the right voltage for the arduino can read the calipers.
The circuit is described below:
The LED had a 1.8v drop, the capacitor is 10 uF and the resistor is 200 ohm, j44 explain this in the middle of tutorial.
Then, he upload the following in arduino so he can read the same.
//Simple Digital Calliper Reader
//See http://j44industries.blogspot.com/
// Pin Declarations
int dataIn = 11;
int clockIn = 12;
// Variables
int clock = 1;
int lastClock = 1;
unsigned long time = 0;
unsigned long timeStart = 0;
int out = 0;
void setup() {
// Pin Set Up
pinMode(dataIn, INPUT);
pinMode(clockIn, INPUT);
Serial.begin(115200);
Serial.println("Ready: ");
}
void loop(){
lastClock = clock;
clock = digitalRead(clockIn);
if (lastClock == 1 && clock == 0){
out = digitalRead(dataIn)+digitalRead(dataIn)+digitalRead(dataIn); // Tripple sampling to remove glitches
if((micros() - time) > 800){
Serial.println(" ");
}
else if((micros() - time) > 400){
Serial.print(" ");
}
if (out > 1){
Serial.print("1");
}
else{
Serial.print("0");
}
Serial.print(",");
time = micros();
}
}
In my code I remove the print of every bit read, expand the code to read 3 calipers at the same time and chance the ADC prescaler to get more speed with arduino.
My code is described below:
//Simple Digital Calliper Reader
//See http://j44industries.blogspot.com/
// Pin Declarations
int dataIn1 = 10;
int clockIn1 = 11;
int dataIn2 = 8;
int clockIn2 = 9;
int dataIn3 = 6;
int clockIn3 = 7;
// Variables
int clock1 = 1;
int lastClock1 = 1;
int clock2 = 1;
int lastClock2 = 1;
int clock3 = 1;
int lastClock3 = 1;
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long timeStart = 0;
int out1 = 0;
int out2 = 0;
int out3 = 0;
int i;
int x1 = 0;
int x2 = 0;
int x3 = 0;
int cont1 = -1;
int cont2 = -1;
int cont3 = -1;
// Define various ADC prescaler
const unsigned char PS_16 = (1 << ADPS2);
const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0);
const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
void setup() {
// Pin Set Up
pinMode(dataIn1, INPUT);
pinMode(clockIn1, INPUT);
pinMode(dataIn2, INPUT);
pinMode(clockIn2, INPUT);
pinMode(dataIn3, INPUT);
pinMode(clockIn3, INPUT);
Serial.begin(115200);
ADCSRA &= ~PS_128; // remove bits set by Arduino library
// you can choose a prescaler from above.
// PS_16, PS_32, PS_64 or PS_128
ADCSRA |= PS_16; // set our own prescaler to 64
}
void loop()
{
lastClock1 = clock1;
clock1 = digitalRead(clockIn1);
lastClock2 = clock2;
clock2 = digitalRead(clockIn2);
lastClock3 = clock3;
clock3 = digitalRead(clockIn3);
//------------------------------------------paquimetro 1
if (lastClock1 == 1 && clock1 == 0)
{
out1 = digitalRead(dataIn1)+digitalRead(dataIn1); // Tripple sampling to remove glitches
if((micros() - time1) > 800)
{
if (cont1 == 23)
{
Serial.write(65); //letter A to indicate that is caliper 1
Serial.println(x1);
}
//leituras[0] = x1;
x1=0;
delayMicroseconds(1);
cont1=-1;
}
if (cont1 < 15)
{
if (out1 >= 1)
{
bitSet(x1,cont1);
}
else
{
bitClear(x1,cont1);
}
}
else if (cont1 == 20)
{
if (out1 >= 1)
{
x1=(-1)*x1;
}
}
cont1 = cont1 + 1;
time1 = micros();
}
//------------------------------------------paquimetro 2
if (lastClock2 == 1 && clock2 == 0)
{
out2 = digitalRead(dataIn2)+digitalRead(dataIn2); // Tripple sampling to remove glitches
if((micros() - time2) > 800)
{
if (cont2 == 23)
{
Serial.write(66); //letter B to indicate that is caliper 2
Serial.println(x2);
}
//leituras[1] = x2;
x2=0;
delayMicroseconds(1);
cont2=-1;
}
if (cont2 < 15)
{
if (out2 >= 1)
{
bitSet(x2,cont2);
}
else
{
bitClear(x2,cont2);
}
}
else if(cont2 == 20)
{
if (out2 >= 1)
{
x2=(-1)*x2;
}
}
cont2 = cont2 + 1;
time2 = micros();
}
//------------------------------------------paquimetro 3
if (lastClock3 == 1 && clock3 == 0)
{
out3 = digitalRead(dataIn3)+digitalRead(dataIn3); // Tripple sampling to remove glitches
if((micros() - time3) > 800)
{
if (cont3 == 23)
{
Serial.write(67); //letter C to indicate that is caliper 3
Serial.println(x3);
}
//leituras[2] = x3;
x3=0;
delayMicroseconds(1);
cont3=-1;
//delayMicroseconds(1);
}
if(cont3 < 15)
{
if (out3 >= 1)
{
bitSet(x3,cont3);
}
else
{
bitClear(x3,cont3);
}
}
else if (cont3 == 20)
{
if (out3 >= 1)
{
x3=(-1)*x3;
}
}
cont3 = cont3 + 1;
time3 = micros();
}
}
However, if I try to read 3 calipers at the same time it returns me some wrong measures, like in images below:
calipers:
Error in caliper A:
Error in caliper C:
If I put only 1 or 2 calipers, I don't get any wrong measure.
What I wanna know is what I can do to stop this wrong measures, because it is not the calipers that is giving these oscillations.