"for","if","break" Loop

I am a Beginner.
I successfully created/saved a very simple sketch, where the purpose was to make the builtin LED in my Adruino Uno board to blink 10 times and then exit the Loop/Stop blinking. I used "for", "if" and "break" in this sketch.
As mentioned, I was able to successfully verify and save this sketch (see attached screen capture), however when I uploaded and ran the sketch, the builtin LED continued to blink past 10 times.

Adruino Capture.JPG

Please post your code in [code] [/code] tags, not pictures of your code.

Your for statement looks really strange.

You don't need to manually break a for loop.

Everything is inside loop() so once you've flashed your LED 10 times, then loop will restart and flash it another 10 times, and another, and another...

I take a look at your code, and in future please use tags. First of all you dont need for loop or break. You only need if condition. Do it this way:

void loop{
a++;
if(a<=10){
//What you want to do with LED
}}
[\code]

Everything is inside loop() so once you've flashed your LED 10 times, then loop will restart and flash it another 10 times, and another, and another...

If you want the 10x blinks to happen only once, either:

  • Put the for in setup(), since setup() itself runs only once
  • Use a boolean flag, called say "done10Blinks", intitialsed false. Then put your for in an if which checks the flag is false. When the for is done, set the flag to true, so next time through loop() the if will fail and the for won't run again

Here's OP's pic btw:

98083ac076c17fa9552b3e53d7a970718bf86a74.jpg

1 Like

Thanks for your replies. Sorry for not using Tags (this is my first post in this forum). I edited my sketch using the suggestion of ogmjen3 as follows

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

void loop(){ 
a++;
if(a<=10)
{
digitalWrite(LED_BUILTIN,LOW);
delay(250);
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);}
}

Still, the loop does not stop after 10 blinks/flashes.

You increment a on every iteration of loop. It will very quickly overflow and be less than 10 again, hence the flashing. Move the increment inside the braces for the if.

Yea sorry for that, I was writing code on my phone so that increment slip away