Basic While loop for this Beginner

I’m sure this will be easy for you all but I can’t figure why this doesn’t work. My first While loop which is meant to simply light up separately in sequence 7 LED’s.
The first Led lights up, the second for a split second, then just reverts to the first pin again.
Any thoughts?

Do you see this "Copy For Forum (Markdown)" option on the edit menu?

It makes a much more readable and usable copy of the code for posting here.

The semicolon here is a problem:
image

It turns these whiles into infinite loops.

The above statements, in fact, stand as:

while(Pin < 8)
{
      ;   
}

Which means that the MCU will wait as long as the value of "Pin variable" remains less than 8.

Once you get the while() working, your output won’t look like it’s flashing, because there’s no interval after setting the pin low.

1 Like

Except for an empty or single line command, you don’t need the curly braces.

Thanks, that did the trick!

1 Like

Thanks!!

Some programmers say that putting the sub-ordinate clause within pair of braces is a part of literate programming.

1 Like

I suspect that that is a personal preference.

1 Like

{ braces }
[ brackets ]
( parentheses )

since we're being whatever.

I've seen "round brackets" for parentheses, and similar.

Now I will make a martini. It will have gin in it. :wink:

a7

To be precise, you don't need brackets if you have only one statement.
This statement can be written on multiple lines, it does not matter at all.

the curly braces create a compound statement, grouping everything together into one statement and this fitting the expectations of the compiler to get only one statement after the while (condition)

it's OK to have only one statement in a compound statement, and in case you add stuff later to do within the while() loop, having already the brackets helps avoid coding mistakes. that's why some companies recommend always having the compound statement form.

yeah - most languages do have specific names for those

{ accolades }
[ crochets ]
( parenthèses )
< chevrons > (also known as anti-lambda)

but you have others (not used in C++)

⟪⟫
〔〕
【】
〖〗
⎡⎤
⎣⎦
⎝⎠

do they have names?
➜ Lenticular Bracket, Double Angle Bracket, Tortoise Shell Bracket... so I think that's also why the other have the xxx bracket alternate name

The while loop works, but you should use a for loop here...

for (int i=0; i<8; i++){
    pinMode(i, OUTPUT);
}

thank you

Or if you have a less regular set of pins you can use an array:

int outPins[] = {2,3,4,6,8,10,13,A1,A5};
for (auto pin : outPins){
   pinMode(pin,OUTPUT);
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.