escape loop after x number of blinking Led

I have searched the web for example sketches that will stop flashing an LED after x number of times. None seem to work. Here is an example:
int ledPin = 13; // select the pin for the LED
int val = 6; // variable to store the data upper limit
void setup() {
pinMode(ledPin,OUTPUT); // declare the LED's pin as output
}
void loop () {
// blink the LED that number
for(int i=0; i<val; i++) {
digitalWrite(ledPin,HIGH);
delay(150);
digitalWrite(ledPin, LOW);
delay(150);
}
}

Where does this go wrong? Thank you.

Every time loop runs, i gets intialized to 0 and so six iterations of the loop are performed. Make i a global.

edit: more precisely - take int i=0 out of the for loop & put it above setup.

the main is

main(){
  setup();
  for(;;)
    loop();
}

so loop is ever call... you can do:

int ledPin = 13;   // select the pin for the LED
int val = 6;    // variable to store the data upper limit

void setup() {
  pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
  // blink the LED that number
    for(int i=0; i<val; i++) {
     digitalWrite(ledPin,HIGH);
     delay(150);
     digitalWrite(ledPin, LOW);
     delay(150);
  }
}
void loop(){
  ;
}

or

void setup() {
  pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
}

void loop () {
  static bool end = false;
  if(!end){
     // blink the LED that number
     for(int i=0; i<val; i++) {
       digitalWrite(ledPin,HIGH);
       delay(150);
       digitalWrite(ledPin, LOW);
       delay(150);
    }
    end = true;
  }
}

or

void setup() {
  pinMode(ledPin,OUTPUT);    // declare the LED's pin as output
}

void loop () {
  static int i = 0;
  // blink the LED that number
  for(; i<val; i++) {
     digitalWrite(ledPin,HIGH);
     delay(150);
     digitalWrite(ledPin, LOW);
     delay(150);
  }
}

and simlilar

Where does this go wrong?

Nowhere; it's doing exactly what you tell it to :slight_smile:

It's flashing the LED six times, then it's finished with the code in the loop() function, which then returns. The thing about loop() is that it's called repeatedly, since it's designed to be the body of a loop.

So you could do this:

void loop () {
  // blink the LED that number
    for(int i=0; i<val; i++) {
     digitalWrite(ledPin,HIGH);
     delay(150);
     digitalWrite(ledPin, LOW);
     delay(150);
  }

  // all done; do nothing now
  while ( true )
  {}
}

which just feels wrong somehow,
or you could do this

bool flashedLED = false;
void loop () {
  if ( ! flashedLED )
  {
     // blink the LED that number
      for(int i=0; i<val; i++) {
       digitalWrite(ledPin,HIGH);
       delay(150);
       digitalWrite(ledPin, LOW);
       delay(150);
    }

    flashedLED = true;
  }
}

which is probably closer to what you'll do when you evolve the code to do something more.

D'oh. Must learn to type faster.

I have a strong preference for the static local variable as in Brig's suggestion (static bool end = false;) because scopes should be as narrow as possible to reduce conflicts and the temptations of excessive coupling. I was under the impression that compiling this in the Arduino environment would result in linker errors because of missing cxa_guard functions. I was wrong. I recommend his solution.

D'oh indeed. Brig nailed it with solution one, just put the for loop in setup.

I still predict the bool guarded loop will be closer to what's needed for whatever the code does next.

BTW, the cxa_guard stuff puzzled me, so I repeated my earliest Arduino experiments. This

void setup()
{
}

class tmp
{
  public:
    tmp() {}
    ~tmp() {}
};

void loop()
{
  static tmp a;
}

requires those functions (because of the ctor.) The dtor also requires atexit. None of these are needed for native types.

THANKS all! I am sculptor with zero intuitive programming skills. This problem occurs when I want sensors/motors to start when the viewer approaches and turn off after a certain count or time. I gave up several years ago but am anxious to begin anew. Thank you so much for the timely responses. Works great!