interfacing between while Statement and some arduino functions (strange thing)

hi

im trying to connect push button and Fingerprint to Arduino MEGA 2560
to do this

first push the button

second the fingerprint scanner will run to scan my fingerprint

after the fingerprint scan my finger it will pass the code and run function to receive string from serial port from arduino

between these step i make checkpoint to find and detect any error

when the arduino run it will print "Please Push button"

after i push the button the fingerprint scanner will run to scan my finger

after that arduion will print "Fingerprint Work Fine"

and the do serial receiving function

but thus never happen
and when i compile that after push the button fingerprint just pulse red light and never run again

if i edit the code and make this like commend "in the receiving from serial"

the code will work and run fine and the fingerprint scanner will run and scan my finger
and after that arduino will print "Fingerprint Work Fine"

char *token = strtok(inData, ","); // Get the first token
////////while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}

if i disable while and make it comment the code will work fine :fearful:

can someone tell me what the hell is happen in this code :angry:

there is no link between while in IF statement not activated to executed and the function of fingerprint
getFingerprintIDez();

also if i remove the comment and remove the finger function getFingerprintIDez();

the code will work fine when i push the button and arduino stop print "Please Push buttion"

is there bugs in Arduino IDE compiler

i try version 1.6 and i have the same problem

this is the code

#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char dataPacket[64];
char inData[64];
byte index;



int d=0;
int ss=0;


#include <Adafruit_Fingerprint_MEGA.h>
int getFingerprintIDez();
#define mySerial Serial1
Adafruit_Fingerprint_MEGA finger = Adafruit_Fingerprint_MEGA(&mySerial);

int buttonState = 0;
const int buttonPin = 2;   

void setup(void)
{
  Serial.begin(9600);
  finger.begin(57600);   
}

void loop(void)
{
   buttonState = digitalRead(buttonPin);
 
  if (d==0){
    Serial.println("Please Push Button");
    if (buttonState == HIGH) {         
   d=1;  
  } 
  } 
  
  if(d==1){                             
 getFingerprintIDez();
  }
  

  if(ss==1){
    Serial.println("System Work Fine");
   // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
 int values[10]; // Array to hold the values
byte index = 0; // index into array

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
Serial.println("---------");

sprintf(dataPacket,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9]);
      Serial.println(dataPacket);
Serial.println("---------");
Serial.flush();
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
}
  
}



////########################################################################################
    

int getFingerprintIDez()
{
 uint8_t p = finger.getImage();
 if (p != FINGERPRINT_OK)  return -1;

 p = finger.image2Tz();
if (p != FINGERPRINT_OK)  return -1;

p = finger.fingerFastSearch();
 if (p != FINGERPRINT_OK)  return -1;
  
  switch(finger.fingerID){
      case 0:  
   Serial.println("Finger Work Fine "); 
   mySerial.end();
   Serial1.end();
  } 
  d=2;
  ss=1;
  return finger.fingerID; 
  
}

Just modified your code slightly to turn it into something to debug.

int values[30];
char inData[]={"12,13,14"};

void setup()
{
Serial.begin(57600);
int index=0;

char *token = strtok(inData, ","); // Get the first token
while(token)
{
   values[index] = atoi(token); // convert the token to an int and store it in the array
   Serial.println(values[index]);
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
}

void loop(){}

Here's my output

12
13
14

Seems your problem is not in the snippet you posted.

i didn't find any reason for what happened

why when i disable while the fingerprint run

while located in if statement and its not activated till i scan my finger ? :confused:

also i connect an xbee to arduino and send string to another xbee connected to atduino and i received it with out any problem using the code i posted it
but with out putting in in if and fingerprint :grin:

so the problem is not in code of receiving

also when i remove finger print from code the code work very well

It would appear that you have a problem building the inData string or initialising index or maybe your values array has a problem. But I'm just shooting in the dark.

im trying to convert while in function out of the loop then call it but i got this error

#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char dataPacket[64];
char inData[64];
byte index;


#include <Adafruit_Fingerprint_MEGA.h>
int d=0;
int s=0;
int ss=0;
char n=0;


int getFingerprintIDez();
#define mySerial Serial1




Adafruit_Fingerprint_MEGA finger = Adafruit_Fingerprint_MEGA(&mySerial);

int buttonState = 0;
const int buttonPin = 2;     // the number of the pushbutton pin

void setup(void)
{


  
   Serial.begin(9600);
  finger.begin(57600);

   
}

void loop(void)
{
   buttonState = digitalRead(buttonPin);
 
  if (d==0){
    Serial.println("---------");
    if (buttonState == HIGH) {         
   d=1;  
  } 
  } 
  
  if(d==1){                             
 getFingerprintIDez();
  }
  

  if(ss==1){
    Serial.println("x");
   // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet


char *token = strtok(inData, ","); // Get the first token
tokken();
Serial.println("---------");

sprintf(dataPacket,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], n);
      Serial.println(dataPacket);
Serial.println("---------");
Serial.flush();
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
}
  
}



////########################################################################################
    
byte tokken(int values[10], byte index = 0)
{
  while (token)
{
 //  int values[10]; // Array to hold the values
  // byte index = 0; // index into array
   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
}
}
int getFingerprintIDez()
{
 uint8_t p = finger.getImage();
 if (p != FINGERPRINT_OK)  return -1;

 p = finger.image2Tz();
if (p != FINGERPRINT_OK)  return -1;

p = finger.fingerFastSearch();
 if (p != FINGERPRINT_OK)  return -1;
  
  switch(finger.fingerID){
      case 0:  
   Serial.println("Finger Work Fine "); 
   mySerial.end();
   Serial1.end();
  } 
  d=2;
  ss=1;
  n=1;
  return finger.fingerID; 
  
}

but i got this error

That isn't an error. That is code. Try again.

hi paul

you remember when i ask for help to send string and split it into array after receiving it via xbee

in this code i put security level fingerprint with button

but there is problem in execution as i explain in the posts upper and i can't solve it because i don't understand why it happen

In your function byte tokken(int values[10], byte index = 0) you make reference to a variable called token But the only place you define this variable is within your loop function.

So token is a local variable to your loop function. You don't have access to it within your tokken function. This is what "not declared within this scope" means.

The other errors mentioned seem pretty similar.

thank you KenF for explain that
i edit the code again and put all the xbee code in function and put this function out of the loop and call it from the loop and this time the code work fine without any problem.

fingerprint work fine and xbee receive the string from another xbee and adrduino split it and show it in serial monitor

this is the new code

#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char dataPacket[64];
char inData[64];
byte index;


#include <Adafruit_Fingerprint_MEGA.h>
int d=0;
int s=0;
int ss=0;
char n=0;


int getFingerprintIDez();
#define mySerial Serial1




Adafruit_Fingerprint_MEGA finger = Adafruit_Fingerprint_MEGA(&mySerial);

int buttonState = 0;
const int buttonPin = 2;     // the number of the pushbutton pin

void setup(void)
{


  
   Serial.begin(9600);
  finger.begin(57600);

   
}

void loop(void)
{
  
 
  if (d==0){
     buttonState = digitalRead(buttonPin);
    Serial.println("---------");
    if (buttonState == HIGH) {         
   d=1;  
  } 
  } 
  
  if(d==1){                             
 getFingerprintIDez();
  }
  

  if(ss==1){
    //Serial.println("x");
    xbe();
   // Read all serial data available, as fast as possible

  
}
  
}



////########################################################################################
    
int xbe(){
  
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 63)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet


char *token = strtok(inData, ","); // Get the first token
 int values[10]; // Array to hold the values
  byte index = 0; // index into array
while (token)
{

   values[index] = atoi(token); // convert the token to an int and store it in the array
   index++; // Increment the index

   token = strtok(NULL, ","); // Keep parsing the same string
   
}
Serial.println("---------");

sprintf(dataPacket,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], n);
      Serial.println(dataPacket);
Serial.println("---------");
Serial.flush();
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }  
  
  
}



int getFingerprintIDez()
{
 uint8_t p = finger.getImage();
 if (p != FINGERPRINT_OK)  return -1;

 p = finger.image2Tz();
if (p != FINGERPRINT_OK)  return -1;

p = finger.fingerFastSearch();
 if (p != FINGERPRINT_OK)  return -1;
  
  switch(finger.fingerID){
      case 0:  
   Serial.println("Finger Work Fine "); 
   mySerial.end();
   Serial1.end();
  } 
  d=2;
  ss=1;
  n=1;
  return finger.fingerID; 
  
}

can someone tell me please why the code didn't execution before i put it into a function

it execute only when i remove fingerprint function
or make while(token) comment

i want to know if someone have background in programming please give us some explain and info to understand

don't tell me there is no one know why this error happen :fearful:

i just want to learn more, i hope someone come from those who a good understand in programming to tell me the reason :grin:

don't tell me there is no one know why this error happen

If you send me your Arduino and fingerprint scanner, I'll take the time and trouble to investigate the two codes to see what the real differences are. Without them, and knowing that you have code that works, the motivation (and ability) just isn't there.

If you're willing to buy another finger print scanner I'll make you the same offer as PaulS. I suspect that, all you'll get from our labours is a forehead slapping "doh" moment.

these days i cant buy the fingerprint with Arduino

but i do that soon and I'm really interested to learn from you Pauls and KenF

Haider92:
but i do that soon and I'm really interested to learn from you Pauls and KenF

Seriously. Reverse engineering somebody else's code is work. If they have a real problem I'll sometimes do it to help a fellow man out. More often than not, once I find the error it's some silly detail that the original poster has overlooked. But to find that requires me to work out how the heck the sketch is supposed to work in the first place. To do that I would have to download the library files you're using and go through them to understand them. When the nett result is just you getting to say. "Oh yeah, I should have seen that!" It's just not worth it.

Just be happy that what you have now, does what you want. You also have the treasure of a little mystery that you can look at, whenever you're lost for something better to do.

Thank you KenF for details and motivational words :slight_smile: