10 Blinks

Hello,

I am try to get the pin 13 led to flash ten times and then stop. Its off the shelf code, but it does not seem to work in my uno. It just keeps flashing indefinitely, I can however change the delay.

Thanks

// sketch 01_01_blink_10
int ledPin = 13;
int delayPeriod = 200;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < 10; i++)
{
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
}
}

The loop function gets repeated once it is finished. So it blinks 10 times and then the loop function repeats and it blinks 10 more. If you want something to happen only once and never again then you should put that code into the setup function.

The loop function repeats infinitely.

Move all your code into the setup function and leave the loop function blank.

Why do you think the loop() function has that name ?

Either move the for loop into setup() where it will run once or set a variable to false before the 10 blinks, then to true afterwards and don't execute the for loop if the variable is true.