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/
}
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
}
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).
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
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?
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; }