Won't exit 'for' loop?

I wrote a program based off of the first blinking-LED example (the one that uses delay() ) that essentially flashes "SOS" in Morse code in an attempt to help me better understand "for" loops in the Arduino language (I wrote the program using what I can remember of plain-Jane C), and to help me learn the . The original intention was for the message to flash 3 times and then stop, but while it does flash the intended message, it won't exit the loop after the third time the message is displayed.

Is there something I'm missing? I apologize if it's something that's blaringly obvious (or something that has been asked before, but I searched and didn't find anything), but if someone could point me in the right direction I'd be most appreciative.

Thanks in advance for your help!

My code:

void setup()
{
pinMode(13,OUTPUT);
}
void loop()
{
int i=0;
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
}
for(int j=0;j<3;j++)
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(500);
}
for(int j=0;j<3;j++)
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}
i=i+1;
}
digitalWrite(13,HIGH); /I tried to visually signal the end of the loop by leaving the LED on/
}

Sincle loop well... loops I think you want:

void setup()
{
  pinMode(13,OUTPUT);
  
  int i=0;
  for(i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      {
        digitalWrite(13,HIGH);
        delay(500);
        digitalWrite(13,LOW);
        delay(500);
      }
    }
    for(int j=0;j<3;j++)
    {
      digitalWrite(13,HIGH);
      delay(1000);
      digitalWrite(13,LOW);
      delay(500);
    }
    for(int j=0;j<3;j++)
    {
      digitalWrite(13,HIGH);
      delay(500);
      digitalWrite(13,LOW);
      delay(500);
    }
    i=i+1;
  }
  digitalWrite(13,HIGH);  /*I tried to visually signal the end of the loop by leaving the LED on*/
}
void loop()
{
  //nothing to see here, move on
}

Well, don't I feel like an idiot...

Thanks for the assist. Didn't even think to try throwing it all in setup(). In fact, I didn't even know it was possible (just kind of assumed setup was for, well, setting it all up for loop to do all the heavy lifting).

No need to feel like an idiot! No wonder why you thought setup was for exactly that.

For education, you should try to implement it as part of the loop. You can implement that with one single boolean variable, and an additional if :slight_smile:

I'm trying to understand the code for myself... how come when I look at the code, I feel like its going to flash 27 times, 3 per (j) for loop and those 3 looped 3 times by the (i) for loop.

Sorry I am new to using the arduino and c programming as a whole. More of an expert on assembly XD

You're incrementing i by two each loop. One time in the for loop and one time within the body of the for loop.

Okay so lets see if i get it so far.

-The loop will go twice because 'i' because it will require two increments to be greater than 3.

Doesn't this result in your loop still going twice, meaning it will blink 18 times?

for(int j=0;j<3;j++)
{
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}

This section of code tells me the LED is blinking 3 times in this one loop. Theres 3 of these 'for' loops reinitializing the j integer. meaning 9 blinks total?

ahhh sorry for my ignorance ahead of time :-/

Yes, thee for loop constructs that loop from j=0 to j<3 no longer is true will loop nine times total
BUT
as the code is written now, the series of nine loop only run two times, so I suspect you see a total number of blinks of 18 (2*9) because the value of i is incremented by two each iteration so i<3 is only true for two passes through the loop.

Remove the i = i +1 statement.

Also one of the for loops has a superfluous scope resolution operator. You never need to write things like {{ code; }} it is exactly the same as { code; }

:slight_smile:

ok thanks, good to know i'm thinking in the right track because the OP said 3 blinks.