loop help needed

Hello, i am beginner and that’s why i need help from you guys.
i am running a program for fingerprint reader and i want it to run for only 5 times then i want it to end, i used for loops but it still run forever, please help
here is the code

#include <Adafruit_Fingerprint.h>
#if ARDUINO >= 100
 #include <SoftwareSerial.h>
#else
 #include <NewSoftSerial.h>
#endif

int getFingerprintIDez();

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
#if ARDUINO >= 100
SoftwareSerial mySerial(2, 3);
#else
NewSoftSerial mySerial(2, 3);
#endif

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

int red = 12;
int green = 11;

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

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

void loop()                     // run over and over again
{
  getFingerprintIDez();
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  digitalWrite(red, HIGH);

   
}

// returns -1 if failed, otherwise returns ID #
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;
  
  // found a match!
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  delay(1000);
  digitalWrite(green, LOW);
  delay(1000);
  digitalWrite(red, HIGH);
  Serial.print("Found ID #"); Serial.println(finger.fingerID);
  return finger.fingerID; 
}

If you don’t want it to carry on, put all your code in “setup()”, not “loop()”

AWOL: If you don't want it to carry on, put all your code in "setup()", not "loop()"

i want it to run for 10 times...

You wrote five earlier. The answer is the same.

AWOL: You wrote five earlier. The answer is the same.

when i do as you told me it shows error... a function definition is not allowed here before '{' token

I can't see your sketch.

AWOL:
I can’t see your sketch.

if there is an error please edit it for me a little, i really need it, thanks
#include <Adafruit_Fingerprint.h>
#if ARDUINO >= 100
#include <SoftwareSerial.h>
#else
#include <NewSoftSerial.h>
#endif

int getFingerprintIDez();

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
#if ARDUINO >= 100
SoftwareSerial mySerial(2, 3);
#else
NewSoftSerial mySerial(2, 3);
#endif

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

int red = 12;
int green = 11;

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

// set the data rate for the sensor serial port
finger.begin(9600);

if (finger.verifyPassword()) {
Serial.println(“Found fingerprint sensor!”);
} else {
Serial.println(“Did not find fingerprint sensor :(”);
while (1);
}
Serial.println(“Waiting for valid finger…”);

getFingerprintIDez();
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
digitalWrite(red, HIGH);

// returns -1 if failed, otherwise returns ID #
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;

// found a match!
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(1000);
digitalWrite(green, LOW);
delay(1000);
digitalWrite(red, HIGH);
Serial.print(“Found ID #”); Serial.println(finger.fingerID);
return finger.fingerID;
}
}

void loop() // run over and over again
{

}

You can't define a function inside another function. Please use code tags when posting code.

AWOL: You can't define a function inside another function. Please use code tags when posting code.

so how am i gonna fix it? if you make a little change in the code for me, it will be better... plz

If you edit your code to put it inside code tags, you'll see the problem. Please.

AWOL:
If you edit your code to put it inside code tags, you’ll see the problem.
Please.

#include <Adafruit_Fingerprint.h>
#if ARDUINO >= 100
 #include <SoftwareSerial.h>
#else
 #include <NewSoftSerial.h>
#endif

int getFingerprintIDez();

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
#if ARDUINO >= 100
SoftwareSerial mySerial(2, 3);
#else
NewSoftSerial mySerial(2, 3);
#endif

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

int red = 12;
int green = 11;

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

  // set the data rate for the sensor serial port
  finger.begin(9600);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor smiley-sad");
    while (1);
  }
  Serial.println("Waiting for valid finger...");
  
  
  
  
  getFingerprintIDez();
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  digitalWrite(red, HIGH);
  
  // returns -1 if failed, otherwise returns ID #
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;
  
  // found a match!
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  delay(1000);
  digitalWrite(green, LOW);
  delay(1000);
  digitalWrite(red, HIGH);
  Serial.print("Found ID #"); Serial.println(finger.fingerID);
  return finger.fingerID; 
}
}

void loop()                     // run over and over again
{
  
}

You’re still defining your function “getFingerprintIDez” inside the function “setup”, which isn’t allowed

(You could have just edited your earlier post to add the code tags)

I guess you could run it in the setup, then you have to copy and paste it four more times. (or nine, whatever)

What I would do is (and this is bad practice, but if you really want to put it in the void loop(){} )

try putting it in a for loop, and once that for loop is done, call a function that is blank

void blank(){}

then Arduino will be looping... nothing until it is reset.

you can also look into putting the arduino into sleep mode to save energy. as that is AVR's pride.

try putting it in a for loop, and once that for loop is done, call a function that is blank

void blank(){}

That won’t work. The blank() function will return immediately, the loop() function will start again and, therefore, the for loop will run again. This is an example of it not working

void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  for (byte i = 0;i < 10;i++)
  {
    Serial.print("Counted ");
    Serial.println(i); 
  }
  blank();
  Serial.println("Back from blank");
}

void blank()
{
}

The best solution is, as has been suggested, to put the for loop in the setup() function where it is guaranteed to only run once on startup or reset.

By the way, there is no such thing as “the void loop()”. There is a loop() function that is void, ie it does not return a value.

Maybe I worded it wrong....

I did something like this, which I admit is terrible but it works:

void loop(){
  //your code here to run five times, maybe a for loop
  blank();
}
void blank(){
  otherblank();
}

void otherblank(){
  blank();
}

You just have it calling functions that call function that call function that ...................

Abubakar, try that if it really matters to you to have the main code in the loop.

That’s not just wrong it’s VERY, VERY WRONG, and totally unnecessary.

If you really want to stop the loop() function executing again you can insert

while(true)
{}

at the place you want it to stop. It would probably be regarded as better practice though to set a boolean variable to true when the required actions in loop() were complete and to surround the action code with a test of the variable such that it only executes when the variable is false. Giving the variable a meaningful name also helps to make it clear what is going on and that the code is not a mistake.

And another way

when you want your code to stop, just do

while (1){}

basically does nothing.

I would look into sleeping though