How to make Loop only run a certain number of times

Just like it sounds above. I want the loop to go only a pre-determined amount of times (say 10 times) instead of it running over and over again until I turn the power off.

Put a for loop in void setup. Nothing in void loop.

If by "loop" you mean "the" loop, as in void loop() {} then you can't afaik- its job is to loop for ever.

But you could put a for in setup().

EDIT.... like steinie44 said :slight_smile:

Alright! Perfect, I'll try that.

JimboZA:
If by "loop" you mean "the" loop, as in void loop() {} then you can't afaik- its job is to loop for ever.

But you could put a for in setup().

EDIT.... like steinie44 said :slight_smile:

Or count the required number of times through the loop() function then enter a while loop with an exit condition that will always be true.

Alright I'm super lost again.

And it seems that for () isn't going to be able to work for me. I literally just want an LED to blink a certain number of times. Or run a photodiode a certain number of times. If that makes any sense?

I literally just want an LED to blink a certain number of times. Or run a photodiode a certain number of times. If that makes any sense?

Blinking an LED does. "Running a photodiode" does not.

bool beenThereDoneThat = false;

void loop()
{
   if(!beenThereDoneThat)
   {
       blinkLED();
       beenThereDoneThat = true;
   }

   // The rest of the code

   // Perhaps something causes beenThereDoneThat to revert to false?
}

void blinkLED()
{
    // Do whatever needs to be done to blink the LED
}

I can tell that there must be more that you want to do. Explain Everything you want to do. Also any code you have.

#define inPin0 0
int led = 13;

void setup(void) {

Serial.begin(9600);
Serial.println(); //get three fig. of precision
pinMode(led, OUTPUT); //calibrate with LED to get rid of background noise

}

void loop(void) {

int pinRead0 = analogRead(inPin0);
float pVolt0 = pinRead0 / 1024.0 * 5.0;
Serial.print(pVolt0); //take off to decrease time of printing no.
Serial.println();
delay(10);

//int input = digitalRead (led = 13);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
// delay(100);
}

Sorry about the messy code. It's a work in progress, and I don't want to delete anything just yet.
Anyway, this makes the LED turn on and off, and it makes a photodiode turn on and off. But I want to make the photodiode only run 10 times or so.

The entire purpose of this is to get a phosphor (a luminescent powder) and excite it with the LED so that it absorbs the light. Then the LED will turn off and the photodiode will read the light that the phosphor is giving off.

I'm basically experimenting what the phosphor emits, but I only want the loop to run about ten times or so, so I don't get excessive amounts of data.

So use a for loop in your setup() function.

It sounds like you need to define your requirements better. When do you want it to blink 10 times? On startup? When you reach a threshold on your analog pin? When you tell it to?

#define inPin0 0
 int led = 13;
 
void setup(void) {
 
  Serial.begin(9600);
  Serial.println();             //get three fig. of precision
   pinMode(led, OUTPUT);        //calibrate with LED to get rid of background noise
     
}
 
void loop(void) {
                                  // get a number from Serial <--------------------------------
                                  // for loop here per that number <-------------------------------
  int pinRead0 = analogRead(inPin0);
  float pVolt0 = pinRead0 / 1024.0 * 5.0;
  Serial.print(pVolt0); //take off to decrease time of printing no.
  Serial.println();
   delay(10);
   
  //int input = digitalRead (led = 13);
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 // delay(100);     
}
// end of for loop

Assuming I've understood the problem, I'd do something crude and simple.

int loop_finished=0;

loop()
{
if (!loop_finished)
{
for (count=0;count<max;count++)
{
// your loop-limited stuff here
}
loop_finished=1;//
}
}