Hi,
i use the code from http://j44industries.blogspot.com/ for reading out a Digital Calliper:
If i use serial.println inside of the function all works fine. But if i use serial.println outside of the function i get only wrong values.
Why? How can i solve this?
The goal is: Reading values (one time!) from the caliper via function and then working with this value in the loop.
the code:
//Simple Digital Calliper Reader
//See http://j44industries.blogspot.com/
// Berechnung nur mit int, Umrechnung in float erst nach der function
// Konstanten
int dataIn = 4; //Weiß;
int clockIn = 9; //Gelb;
int clock = 1;
int lastClock = 1;
int out = 0;
int digits[28]; //byte
int counter;
int flag;
double Wegmm;
double Wert1vonRechts = 0;
double Wert2vonRechts = 0;
double Wert3vonRechts = 0;
double Wert4vonRechts = 0;
double Wert5vonRechts = 0;
double Wert6vonRechts = 0;
double Resultat = 0;
void setup() {
// Pin Set Up
pinMode(dataIn, INPUT);
pinMode(clockIn, INPUT);
Serial.begin(115200);
Serial.println("Ready: ");
delay(100);
}
void Wegmessung() {
lastClock = clock;
clock = digitalRead(clockIn);
flag = 0;
if ((lastClock == 1) && (clock == 0))
{
out = digitalRead(dataIn) + digitalRead(dataIn) + digitalRead(dataIn); // Tripple sampling to remove glitches
flag = 0;
if (out > 1)
{
flag = 1;
}
digits[counter] = flag;
counter = counter + 1;
if (counter >= 25) {
Wert1vonRechts = (double)digits[1] * 1 + (double)digits[2] * 2 + (double)digits[3] * 4 + (double)digits[4] * 8;
Wert2vonRechts = (double)digits[5] * 1 + (double)digits[6] * 2 + (double)digits[7] * 4 + (double)digits[8] * 8;
Wert3vonRechts = (double)digits[9] * 1 + (double)digits[10] * 2 + (double)digits[11] * 4 + (double)digits[12] * 8;
Wert4vonRechts = (double)digits[13] * 1 + (double)digits[14] * 2 + (double)digits[15] * 4 + (double)digits[16] * 8;
Wert5vonRechts = (double)digits[17] * 1 + (double)digits[18] * 2 + (double)digits[19] * 4 + (double)digits[20] * 8;
Wert6vonRechts = (double)digits[21] * 1 + (double)digits[22] * 2 + (double)digits[23] * 4 + (double)digits[24] * 8;
Wegmm = Wert1vonRechts + Wert2vonRechts * 10 + Wert3vonRechts * 100 + Wert4vonRechts * 1000 + Wert5vonRechts * 10000 + Wert6vonRechts * 100000;
counter = 0;
Serial.println(Wegmm);
}
}
}
void loop()
{
Wegmessung();
//Serial.println(Wegmm);
}