Variable value unexpected changed without touching.

Hi there, I'm programmer but never write arduino.
I try to use fingerprint module with UNO board.
and download adafruit library and example to use
everything seem go fine.

but the problem is enrolling my fingerprint.
I input number ID of my fingerprint to the board via serial
at first I check the value of variable that can receive number correctly
but everytime the ID variable turned to zero before store to the module

let check with source code
or you can download adafruit to check it on your own
I have not change anything.

void loop()                     // run over and over again
{
  Serial.println("Type in the ID # you want to save this finger as...");
  uint8_t id = 0;
  while (true) {
    while (! Serial.available());
    char c = Serial.read();
    if (! isdigit(c)) break;
    id *= 10;
    id += c - '0';
  }
  Serial.print("Enrolling ID #");
  Serial.println(id);
  
  while (!  getFingerprintEnroll(id) );
}

uint8_t getFingerprintEnroll(uint8_t id) {
  uint8_t p = -1;
  Serial.println("Waiting for valid finger to enroll");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

in function loop everything ok.
but function getFingerprintEnroll that have
parameter uint8_t id
that have value from calling from loop function
"never" write anything on it.
but the value in it was changed to zero before it store to memory

I have check by function Serial.println(id);
and found the value has been changed at the point

p = finger.getImage();

I try input number 7 it turn to 2 at first this function call.
and turn to zero at another call.

but hey,.. another surprise to me..
I try to make another variable inside the function

uint8_t id_temp = id;

for making backup the value;
before it's changed.

... but this variable has been changed too T_T
both id and id_temp turn to zero value in function

so I try another way..
I change the value to fix number

uint8_t id_temp = 10;

this time.. it's ok
the value unchange it still be "10" till the end of function.

and finally, I fluky found solution..

adding this lines will make the variable value not changed till the end.

Serial.println(id);
Serial.println(id);
Serial.println(id);
Serial.println(id);
Serial.println(id);

it has to be 5 lines,... not less.
4 line not help, just 5 line.
even added to the end of function, it still work.

this make me sick.
I don't know why.

Debug by printing variables you want to trace and which paths your conditionals took.

I don't have your fingerprint module to check anything with.

-nevermind-

GoForSmoke:
Debug by printing variables you want to trace and which paths your conditionals took.

I don't have your fingerprint module to check anything with.

Not a fingerprint module problem.......

while (1) {
    while (!(Serial.available());
    c = Serial.read();
    if (isdigit (c)) { // act only for 0 thru 9
        c -= '0'; // convert ascii to numeric
        break; // break out with your parsed value
    }
}

That will work for one char and it blocks.

The OP still needs to debug just to see what the OP's code is actually doing. Best lesson around.

What you are describing are the classic symptoms of a memory overflow problem. So that might be the problem and they are difficult to debug and difficult to advise on. You did not post your full code; you might have other variables that take memory and as a result you have the problem. Please post the full code.

but the value in it was changed to zero before it store to memory
...
I try input number 7 it turn to 2 at first this function call. and turn to zero at another call.
...
it has to be 5 lines,... not less. 4 line not help, just 5 line.
...

First thing to try

uint8_t getFingerprintEnroll(uint8_t id) {
  Serial.println(id);
  return 0;

  // existing code here
  ...
  ...
  ...
}

Your code now will only print the id#. Is it stable? You can remove the existing code but as none of the library functions are called I suspect it ill not make much of a difference if you remove it or not.

Next I suggest that you slowly build your function again. Do a getImage and nothing more and print the result.

Can you provide a link to the library?

I use Library Manager to search this library.

/*************************************************** 
  This is an example sketch for our optical Fingerprint sensor

  Designed specifically to work with the Adafruit BMP085 Breakout 
  ----> http://www.adafruit.com/products/751

  These displays use TTL Serial to communicate, 2 pins are required to 
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

uint8_t getFingerprintEnroll(uint8_t id);


// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
SoftwareSerial mySerial(2, 3);

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()  
{
  Serial.begin(9600);
  Serial.println("fingertest");

  // set the data rate for the sensor serial port
  finger.begin(57600);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
}

void loop()                     // run over and over again
{
  Serial.println("Type in the ID # you want to save this finger as...");
  uint8_t id = 0;
  while (true) {
    while (! Serial.available());
    char c = Serial.read();
    if (! isdigit(c)) break;
    id *= 10;
    id += c - '0';
  }
  Serial.print("Enrolling ID #");
  Serial.println(id);
  
  while (!  getFingerprintEnroll(id) );
}

uint8_t getFingerprintEnroll(uint8_t id) {
  uint8_t p = -1;
  Serial.println("Waiting for valid finger to enroll");
  Serial.println(id);
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  Serial.println("Remove finger");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }

  p = -1;
  Serial.println("Place same finger again");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.print(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      break;
    default:
      Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  
  // OK converted!
  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    Serial.println("Prints matched!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    Serial.println("Fingerprints did not match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    Serial.println("Stored!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    Serial.println("Could not store in that location");
    return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    Serial.println("Error writing to flash");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  Serial.println(id);
  Serial.println(id);
  Serial.println(id);
  Serial.println(id);
  Serial.println(id);
}

sterretje:
What you are describing are the classic symptoms of a memory overflow problem. So that might be the problem and they are difficult to debug and difficult to advise on. You did not post your full code; you might have other variables that take memory and as a result you have the problem. Please post the full code.

First thing to try

uint8_t getFingerprintEnroll(uint8_t id) {

Serial.println(id);
  return 0;

// existing code here
  ...
  ...
  ...
}



Your code now will only print the id#. Is it stable? You can remove the existing code but as none of the library functions are called I suspect it ill not make much of a difference if you remove it or not.

Next I suggest that you slowly build your function again. Do a getImage and nothing more and print the result.

Can you provide a link to the library?

the value passed via parameter at begin of the function was stable until calling function getImage();

One way to save RAM:

Serial.print( "all the text in this string gets loaded to RAM at startup" );

Serial.print( F( "all the text in this string stays in flash, prints from flash" ));

now I found the problem, I think.
the problem cause from calling member function of "Adafruit_Fingerprint" in the function.
there's some problem about their function that make value in stack variable change.

I found 2 solution now.

first, I don't know why but I added "Serial.println(id);" 5 times in function.. that make value in "id" variable stable. it's not changed.

second, I make new function that calling adafruit_fingerprint member function inside.. (wrap function). that make my variable safe.

You're just hiding the symptoms but don't cure the disease. If you add a new function or change something, it might fail again. Or it will fail in the long run.

Maybe contacting Adafruit is an option; adafruit customer support forums. No experience with them.

indeed