want to break a loop

Hello! Brand new user here! Annoying novice, I know. I would like to have this loop run 10 times then stop ("break"). can anybody add the code I need to make that happen? Thank you!

//Stoplight

int delay1 = 1000; //Set delay value
int red = 11; //Set up red as a constant
int yellow = 10; //Set up yellow as a constant
int green = 9; //Set up green as a constant

void setup() {
pinMode(red, OUTPUT); //Set digital pin red as an output
pinMode(green, OUTPUT); //Set digital pin green as an output
pinMode(yellow, OUTPUT); //Set digital pin yellow as an output
}

void loop() {

digitalWrite(green, HIGH); //Turn the green light on
delay(delay1); //Wait 1000 ms

digitalWrite(green, LOW); //Turn the green light off
digitalWrite(yellow, HIGH); //Turn the yellow light on
delay(delay1 / 2); //Wait 500 ms

digitalWrite(yellow, LOW); //Turn the yellow light off
digitalWrite(red, HIGH); //Turn the red light on
delay(delay1); //Wait 1000 ms

digitalWrite(red, LOW); //Turn the red light off

}

for(int i=0;i<10;i++) {

do things..

}

-jim lee

Use a 'for' loop like jimLee suggests, but put it at the bottom of setup() and move all of the code from inside loop() to the new 'for' loop. If you put the 'for' loop in loop() it would just run ten times again each time loop() repeated. And loop() repeats forever... that's what it's for.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

thank you so much everybody for taking the time to respond!

Alternative method

void loop()
{
    static uint8_t counter = 0;
    if ( counter < 10 )
    {
        counter++;
        
        digitalWrite(green, HIGH);  //Turn the green light on
        delay(delay1);              //Wait 1000 ms

        digitalWrite(green, LOW);   //Turn the green light off
        digitalWrite(yellow, HIGH); //Turn the yellow light on
        delay(delay1 / 2);          //Wait 500 ms

        digitalWrite(yellow, LOW);  //Turn the yellow light off
        digitalWrite(red, HIGH);    //Turn the red light on
        delay(delay1);              //Wait 1000 ms

        digitalWrite(red, LOW);     //Turn the red light off
    }
}