user65810: I am new to Arduino programming and I would like to ask a question.
Firstly apologies if this has been answered before and I understand if it gets locked if this has been answered elsewhere.
I am trying to get three LED's (red, amber, green) to loop 5 times (in a do loop).
for this I have created a integer variable (x) and initialised the variable to 0.
I then increase x by 1 each time.
However I cannot get them to stop looping when x is greater than 5. The program does work as expected for everything else (green LED lit, wait 1 sec, Yellow LED lit, wait 1 sec, red LED lit and then loop).
   //set LED pins
int greenPin = 13;
int yellowPin = 12;
int redPin = 11;

void setup()
{
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(yellowPin, OUTPUT);
  pinMode(redPin, OUTPUT);
}

void loop(){
int x = 0;

 do {
  digitalWrite(greenPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(greenPin, LOW); 
  digitalWrite(yellowPin, HIGH);    // sets the LED off
  delay(1000);
  digitalWrite(yellowPin, LOW);// waits for a second
  digitalWrite(redPin, HIGH);    // sets the LED off
  delay(1000);
  digitalWrite(redPin, LOW);
 }
   while(x<=5); {
    x = x + 1;
   }
}


user31105: Chrisl already gave the clue.
However, to add a comment with a code example I use the answer box.
In C you have more or less two kind of loops:

for
do / while

The first one is when you know beforehand how many iterations you have, the second when it depends on external input (e.g. when you expect a value acquired from within the loop).
In your case, you can better use the for loop, so you get:
for (x = 0; x < 6; x++)
{
   ...
}

What this does i:

Initializes x to 0 (first part of the for statement)
It checks the condition x < 5' (second part of theforstatement). If false, it continues with the first statement after thefor` body (after the } bracket).
It runs the body of the loop (inside the brackets)
It increases x (third part of the for statement)
It continues with 2

Also it is quite common for the condition to include the start element of the ragne (x = 0), but exclude the end element of the range (x < 6), thus it runs the last time with x == 5.

65810: <p>I am new to Arduino programming and I would like to ask a question.</p>

<p>Firstly apologies if this has been answered before and I understand if it gets locked if this has been answered elsewhere.</p>

<p>I am trying to get three LED's (red, amber, green) to loop 5 times (in a do loop).</p>

<p>for this I have created a integer variable (x) and initialised the variable to 0.</p>

<p>I then increase x by 1 each time.</p>

<p>However I cannot get them to stop looping when x is greater than 5. The program does work as expected for everything else (green LED lit, wait 1 sec, Yellow LED lit, wait 1 sec, red LED lit and then loop).</p>

<pre><code>   //set LED pins
int greenPin = 13;
int yellowPin = 12;
int redPin = 11;

void setup()
{
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(yellowPin, OUTPUT);
  pinMode(redPin, OUTPUT);
}

void loop(){
int x = 0;

 do {
  digitalWrite(greenPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(greenPin, LOW); 
  digitalWrite(yellowPin, HIGH);    // sets the LED off
  delay(1000);
  digitalWrite(yellowPin, LOW);// waits for a second
  digitalWrite(redPin, HIGH);    // sets the LED off
  delay(1000);
  digitalWrite(redPin, LOW);
 }
   while(x&lt;=5); {
    x = x + 1;
   }
}
</code></pre>
 31105: <p>Chrisl already gave the clue.</p>

<p>However, to add a comment with a code example I use the answer box.</p>

<p>In C you have more or less two kind of loops:</p>

<ul>
<li>for</li>
<li>do / while</li>
</ul>

<p>The first one is when you know beforehand how many iterations you have, the second when it depends on external input (e.g. when you expect a value acquired from within the loop).</p>

<p>In your case, you can better use the <code>for</code> loop, so you get:</p>

<pre><code>for (x = 0; x &lt; 6; x++)
{
   ...
}
</code></pre>

<p>What this does i:</p>

<ol>
<li>Initializes x to 0 (first part of the <code>for</code> statement)</li>
<li>It checks the condition <code>x &lt; 5' (second part of the</code>for<code>statement). If false, it continues with the first statement after the</code>for` body (after the } bracket).</li>
<li>It runs the body of the loop (inside the brackets)</li>
<li>It increases x (third part of the <code>for</code> statement)</li>
<li>It continues with 2</li>
</ol>

<p>Also it is quite common for the condition to include the start element of the ragne (x = 0), but exclude the end element of the range (x &lt; 6), thus it runs the last time with x == 5.</p>
