Looping

Total noob here so sorry for the stupid question...i'm just trying to get an LED to blink 3 times then stop.

This is my code:

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
for (int count = 0; count <= 3; count++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}

}

Can someone please explain what i'm doing wrong? I did try googling it & tried a bunch of different things but it just keeps flashing!!!

Thanks!

Mel :slight_smile:

After you are finished the for(), your loop() function restarts, thus you restart the for() over and over and over . . . Ad infinitum.

The for() loop is inside the main loop(), so it repeats after it has finished.

You must use extra code to run that for loop only once.

boolean runOnce;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (runOnce == false) {
    for (int count = 0; count <= 3; count++) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(200);
      digitalWrite(LED_BUILTIN, LOW);
      delay(200);
    }
  }
  runOnce = true;
}

Or... move that for loop to setup().
Leo..

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  for (int count = 0; count <= 3; count++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    delay(200);
  }
}

void loop() {}

oh! ok thanks!!!