user30261: I'm trying to control the speed of a DC motor using PWM. Here is my code so far:
int motor = 9;        
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);
}

void loop() {
  for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
  if (fadeValue==245) {
    fadeValue=255;
    break;
  }
}

I want the motor to slowly increase it speed then stay high. However, the loop keep repeating.
Thanks in advance guys.

user-1: If you dont want the code to repeat at the maximum, just add a condition to the loop. Your solution could look like this:
int fadeValue = 5;

void loop()
{
    if (fadeValue < 255)
    {
        if (fadeValue >= 245)
        {
            fadeValue = 255;
        }
        else
        {
            fadeValue++;
        }
        analogWrite(motor, fadeValue);
        delay(100); 
    }

    //Other code here
}

By setting FadeValue to another value, the fade will start again from the new value until the maximum.

user30320: Could you also change the order the loop is in? So the code would look like this instead:
void loop() {
  if (fadeValue == 245) {
    fadeValue=255;
    analogWrite(motor, fadeValue);  // motor will stay at highest value
    }
  else {
      for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
        analogWrite(motor, fadeValue);
        delay(100);
        }
  }
}

So now, the loop will run until fadeValue == 245, at which point the loop will not advance to the for loop because the "if" in the if/else statement is satisfied. I'm not actually sure if this will work, but it's an idea.

30261: <p>I'm trying to control the speed of a DC motor using PWM. Here is my code so far:</p>

<pre><code>int motor = 9;        
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);
}

void loop() {
  for (int fadeValue = 5 ; fadeValue &lt;= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
  if (fadeValue==245) {
    fadeValue=255;
    break;
  }
}
</code></pre>

<p>I want the motor to slowly increase it speed then stay high. However, the loop keep repeating.</p>

<p>Thanks in advance guys.</p>
 -1: <p>If you dont want the code to repeat at the maximum, just add a condition to the loop. Your solution could look like this:</p>

<pre><code>int fadeValue = 5;

void loop()
{
    if (fadeValue &lt; 255)
    {
        if (fadeValue &gt;= 245)
        {
            fadeValue = 255;
        }
        else
        {
            fadeValue++;
        }
        analogWrite(motor, fadeValue);
        delay(100); 
    }

    //Other code here
}
</code></pre>

<p>By setting <code>FadeValue</code> to another value, the fade will start again from the new value until the maximum.</p>
 30320: <p>Could you also change the order the loop is in? So the code would look like this instead:</p>

<pre><code>void loop() {
  if (fadeValue == 245) {
    fadeValue=255;
    analogWrite(motor, fadeValue);  // motor will stay at highest value
    }
  else {
      for (int fadeValue = 5 ; fadeValue &lt;= 245; fadeValue += 10) {
        analogWrite(motor, fadeValue);
        delay(100);
        }
  }
}
</code></pre>

<p>So now, the loop will run until fadeValue == 245, at which point the loop will not advance to the for loop because the "if" in the if/else statement is satisfied. I'm not actually sure if this will work, but it's an idea.</p>
