digitalRead and wrong output over serial

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

inside which function? and what, exactly, do you try to print?

You're not trying to print local variables, are you?

Maybe the variables need to be declared volatile ?

If i use

Serial.println(Wegmm);

inside the function Wegmessung() it works fine.
But i want to print (and use) Wegmm in the loop (ouside the function, actually commented out). This doesnt works.
Wegmm is a global variable
Volatile doesnt solve the problem

Kind regards,
Andreas

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.