user35398: I have three funtion which describe the brightness of three LEDs. I want the seconde loop run 1 second after the first one and the third 1 second after the second one. One loop needs approximately 5 seconds. So basically I need to run three loops separated. How is this possible?
Update:
So I have this code running which I can compile but does not work:
int LED = 3;  
int LED2 =10;
int LED3 = 9;

bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms


void setup()
{
Serial.begin(9600);
}

void loop()
{
// Update loop enablers.
switch(periodIndex)
{
case 0:
 loop1state = true;
 loop1index = 0;
 break;

case 1000:
 loop2state = true;
 loop2index = 0;
 break;

case 1500:
 loop3state = true;
 loop3index = 0;
 break;

case 4000:
 periodIndex = 0;
 break;
}

if (loop1state)
{
 for (loop1index = 0; loop1index<255; loop1index++){
  analogWrite(LED,loop1index);
 }

 loop1state = loop1index < 256;
 }

 if (loop2state)
 {
 for(loop2index = 0; loop2index<255; loop2index++)
 {
  analogWrite(LED2,loop2index);
 }

 loop2state = loop2index < 256;
}

 if (loop3state)
 {
 for(loop3index = 0; loop3index<255; loop3index++)
 {
  analogWrite(LED3,loop3index);
 }
 loop3state = loop3index < 256;
 }
 delay(10);
 }


user31105: No, but there is a solution to put the action in one loop (main loop).
If I understand you well, you have the following scheme:
Time[s]
0..1     Loop 1
1..2     Nothing
2..3     Loop 2
3..4     Nothing
4..5     Loop 3
Define a variable that keeps track of the time.
Check against the current time (millis) and when within the correct 'range' perform an action.
After 5 seconds start over again.
uint32_t lastTime = -1;

void loop()
{
    if (lastTime == -1)
    {
        lastTime = currentTime; // Initialization, can be done in setup too
    }

    uint32_t diffTime = millis() - lastTime;

    if (diffTime < 1000) // 0..1 sec
    {
        DoLoop1();
    }
    else if (diffTime < 2000)
    {
         // Do nothing();
    }
    else if (diffTime < 3000)
    {
       DoLoop2();
    }
    else if (diffTime < 4000)
    {
        // Do nothing
    }
    else if (diffTime < 5000)
    {
       DoLoop3();
    }
    else // diffTime >= 5000
    {
         lastTime == currentTime; // Reset after 6 s
    }
}

Update
Since you need all three loops together (and mixed), it's easier to define some variables.
bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms

Too bad I don't know the conditions when a loop starts, but let's say each iteration is 10 ms, and loop 1 starts immediately, loop 2 after 1000 ms, loop 3 after 1500 ms. After 3000 ms it starts all over.
void loop()
{
  // Update loop enablers.
  switch(periodIndex)
  {
  case 0:
     loop1state = true;
     loop1index = 0;
     break;

  case 1000:
     loop2state = true;
     loop2index = 0;
     break;

  case 1500:
     loop3state = true;
     loop3index = 0;
     break;

  case 4000:
     periodIndex = 0;
     break;
  }

  if (loop1state)
  {
     DoLoop1(loop1index++);
     loop1state = loop1index < 256;
  }

  if (loop2state)
  {
     DoLoop2(loop2index++);
     loop2state = loop2index < 256;
  }

  if (loop3state)
  {
     DoLoop3(loop3index++);
     loop3state = loop3index < 256;
  }

  delay(10); // 10 ms
}

Note, not tested, but the idea shold be clear     
New update
You should not do a full loop during each switch, but only one iteration of the loop.
I did not run the sketch, just out of my head.
Some tips: print out the variables during each loop, so you get a clear idea what is happening (so all loop states and indices, and the periodIndex).
int LED = 3;  
int LED2 =10;
int LED3 = 9;

bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // Update loop enablers.
  switch(periodIndex)
  {
  case 0:
    loop1state = true;
    loop1index = 0;
    break;

  case 1000:
    loop2state = true;
    loop2index = 0;
    break;

  case 1500:
    loop3state = true;
    loop3index = 0;
    break;

  case 4000:
     periodIndex = 0;
     break;
  }

  if (loop1state)
  {
    analogWrite(LED,loop1index++);
    loop1state = loop1index < 256;
  }

  if (loop2state)
  {
    analogWrite(LED2,loop2index++);
    loop2state = loop2index < 256;
  }

  if (loop3state)
  {
    analogWrite(LED3,loop3index++);
    loop3state = loop3index < 256;
  }

  periodIndex++;
  delay(10);
}


user7508: Let me see if I understand correctly what you want... You want to ramp
up the brightnesses of 3 LEDs, one step every roughly 10 ms, and
you want the brightness ramps to start at different times. If this is
the case, I would start by writing a function that handles one LED,
increases its brightness by one step, and tells the caller whether the
ramp is done or not:
// Update the LED.
// Returns true if there are brightness steps left,
// false if we have finished the brightness ramp.
bool update_led_1()
{
    static uint8_t level = 0;  // brightness level
    analogWrite(LED1, level);
    level++;  // wraps to zero when we are done
    return level != 0;
}

This stores the brightness level as a single byte, rather than a
two-byte int, because I am cheap. ;-) Notice that on the last step of
the ramp, the value 255 is sent to the LED, then level++ wraps the
level to zero, then the function returns false to tell us the ramp is
finished. At this point, since level is zero, the function is ready
for the next ramp.
You can copy and paste this function three times, for each of the three
LEDs. Name them update_led_1(), update_led_2() and update_led_3().
The main loop can then go along these lines:
void loop()
{
    static int periodIndex = 0;  // increasing every 10 ms

    static bool led1active = false;
    if (periodIndex == 0)
        led1active = true;
    if (led1active)
        led1active = update_led_1();

    static bool led2active = false;
    if (periodIndex == 1000)
        led2active = true;
    if (led2active)
        led2active = update_led_2();

    static bool led3active = false;
    if (periodIndex == 1000)
        led3active = true;
    if (led3active)
        led3active = update_led_3();

    if (++periodIndex == 4000)
        periodIndex = 0;
    delay(10);
}

Notice the idiom if (led1active) led1active = update_led_1();. This
will be executed on every loop iteration, it will increase the LED
brightness if the LED is supposed to be active, and it will set
led1active to false when the brightness ramp is complete.
There is something I do not like in the solution I am suggesting here:
the copy-and-paste programming paradigm. If I am allowed to use C++
constructs, then I would encapsulate the ramp logic in a C++ class:
class RampingLed {
    const uint8_t pin;
    bool active;
    uint8_t level;
public:
    RampingLed(uint8_t pin)
        : pin(pin), active(false), level(0) {}
    void start() {
        active = true;
        level = 0;
    }
    void update() {
        if (!active) return;
        analogWrite(pin, level++);
        if (level == 0)
            active = false;
    }
};

Then the logic of the main program would appear more straightforward:
RampingLed led1(LED1), led2(LED2), led3(LED3);

void loop()
{
    static int periodIndex = 0;  // increasing every 10 ms

    // Start the LEDs at the appropriate times.
    if (periodIndex == 0)
        led1.start();
    if (periodIndex == 1000)
        led2.start();
    if (periodIndex == 1500)
        led3.start();

    // Update every LED.
    led1.update();
    led2.update();
    led3.update();

    if (++periodIndex == 4000)
        periodIndex = 0;
    delay(10);
}


35398: <p>I have three funtion which describe the brightness of three LEDs. I want the seconde loop run 1 second after the first one and the third 1 second after the second one. One loop needs approximately 5 seconds. So basically I need to run three loops separated. How is this possible?</p>

<p>Update:
So I have this code running which I can compile but does not work:</p>

<pre><code>int LED = 3;  
int LED2 =10;
int LED3 = 9;

bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms


void setup()
{
Serial.begin(9600);
}

void loop()
{
// Update loop enablers.
switch(periodIndex)
{
case 0:
 loop1state = true;
 loop1index = 0;
 break;

case 1000:
 loop2state = true;
 loop2index = 0;
 break;

case 1500:
 loop3state = true;
 loop3index = 0;
 break;

case 4000:
 periodIndex = 0;
 break;
}

if (loop1state)
{
 for (loop1index = 0; loop1index&lt;255; loop1index++){
  analogWrite(LED,loop1index);
 }

 loop1state = loop1index &lt; 256;
 }

 if (loop2state)
 {
 for(loop2index = 0; loop2index&lt;255; loop2index++)
 {
  analogWrite(LED2,loop2index);
 }

 loop2state = loop2index &lt; 256;
}

 if (loop3state)
 {
 for(loop3index = 0; loop3index&lt;255; loop3index++)
 {
  analogWrite(LED3,loop3index);
 }
 loop3state = loop3index &lt; 256;
 }
 delay(10);
 }
</code></pre>
 31105: <p>No, but there is a solution to put the action in one loop (main loop).</p>

<p>If I understand you well, you have the following scheme:</p>

<p>Time[s]
0..1     Loop 1
1..2     Nothing
2..3     Loop 2
3..4     Nothing
4..5     Loop 3</p>

<p>Define a variable that keeps track of the time.
Check against the current time (millis) and when within the correct 'range' perform an action.
After 5 seconds start over again.</p>

<pre><code>uint32_t lastTime = -1;

void loop()
{
    if (lastTime == -1)
    {
        lastTime = currentTime; // Initialization, can be done in setup too
    }

    uint32_t diffTime = millis() - lastTime;

    if (diffTime &lt; 1000) // 0..1 sec
    {
        DoLoop1();
    }
    else if (diffTime &lt; 2000)
    {
         // Do nothing();
    }
    else if (diffTime &lt; 3000)
    {
       DoLoop2();
    }
    else if (diffTime &lt; 4000)
    {
        // Do nothing
    }
    else if (diffTime &lt; 5000)
    {
       DoLoop3();
    }
    else // diffTime &gt;= 5000
    {
         lastTime == currentTime; // Reset after 6 s
    }
}
</code></pre>

<h2>Update</h2>

<p>Since you need all three loops together (and mixed), it's easier to define some variables.</p>

<pre><code>bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms
</code></pre>

<p>Too bad I don't know the conditions when a loop starts, but let's say each iteration is 10 ms, and loop 1 starts immediately, loop 2 after 1000 ms, loop 3 after 1500 ms. After 3000 ms it starts all over.</p>

<pre><code>void loop()
{
  // Update loop enablers.
  switch(periodIndex)
  {
  case 0:
     loop1state = true;
     loop1index = 0;
     break;

  case 1000:
     loop2state = true;
     loop2index = 0;
     break;

  case 1500:
     loop3state = true;
     loop3index = 0;
     break;

  case 4000:
     periodIndex = 0;
     break;
  }

  if (loop1state)
  {
     DoLoop1(loop1index++);
     loop1state = loop1index &lt; 256;
  }

  if (loop2state)
  {
     DoLoop2(loop2index++);
     loop2state = loop2index &lt; 256;
  }

  if (loop3state)
  {
     DoLoop3(loop3index++);
     loop3state = loop3index &lt; 256;
  }

  delay(10); // 10 ms
}
</code></pre>

<p>Note, not tested, but the idea shold be clear     </p>

<h1>New update</h1>

<p>You should not do a full loop during each switch, but only one iteration of the loop.</p>

<p>I did not run the sketch, just out of my head.</p>

<p>Some tips: print out the variables during each loop, so you get a clear idea what is happening (so all loop states and indices, and the periodIndex).</p>

<pre><code>int LED = 3;  
int LED2 =10;
int LED3 = 9;

bool loop1state, loop2state, loop3state; // True if running, false if off
int  loop1index, loop2index, loop3index; // Iterator values
int  periodIndex = 0; // starting with 0, increasing every 10 ms


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // Update loop enablers.
  switch(periodIndex)
  {
  case 0:
    loop1state = true;
    loop1index = 0;
    break;

  case 1000:
    loop2state = true;
    loop2index = 0;
    break;

  case 1500:
    loop3state = true;
    loop3index = 0;
    break;

  case 4000:
     periodIndex = 0;
     break;
  }

  if (loop1state)
  {
    analogWrite(LED,loop1index++);
    loop1state = loop1index &lt; 256;
  }

  if (loop2state)
  {
    analogWrite(LED2,loop2index++);
    loop2state = loop2index &lt; 256;
  }

  if (loop3state)
  {
    analogWrite(LED3,loop3index++);
    loop3state = loop3index &lt; 256;
  }

  periodIndex++;
  delay(10);
}
</code></pre>
 7508: <p>Let me see if I understand correctly what you want... You want to ramp
up the brightnesses of 3 LEDs, one step every roughly 10&nbsp;ms, and
you want the brightness ramps to start at different times. If this is
the case, I would start by writing a function that handles one LED,
increases its brightness by one step, and tells the caller whether the
ramp is done or not:</p>



<pre class="lang-cpp prettyprint-override"><code>// Update the LED.
// Returns true if there are brightness steps left,
// false if we have finished the brightness ramp.
bool update_led_1()
{
    static uint8_t level = 0;  // brightness level
    analogWrite(LED1, level);
    level++;  // wraps to zero when we are done
    return level != 0;
}
</code></pre>

<p>This stores the brightness level as a single byte, rather than a
two-byte <code>int</code>, because I am cheap. ;-) Notice that on the last step of
the ramp, the value 255 is sent to the LED, then <code>level++</code> wraps the
level to zero, then the function returns false to tell us the ramp is
finished. At this point, since <code>level</code> is zero, the function is ready
for the next ramp.</p>

<p>You can copy and paste this function three times, for each of the three
LEDs. Name them <code>update_led_1()</code>, <code>update_led_2()</code> and <code>update_led_3()</code>.
The main loop can then go along these lines:</p>

<pre class="lang-cpp prettyprint-override"><code>void loop()
{
    static int periodIndex = 0;  // increasing every 10 ms

    static bool led1active = false;
    if (periodIndex == 0)
        led1active = true;
    if (led1active)
        led1active = update_led_1();

    static bool led2active = false;
    if (periodIndex == 1000)
        led2active = true;
    if (led2active)
        led2active = update_led_2();

    static bool led3active = false;
    if (periodIndex == 1000)
        led3active = true;
    if (led3active)
        led3active = update_led_3();

    if (++periodIndex == 4000)
        periodIndex = 0;
    delay(10);
}
</code></pre>

<p>Notice the idiom <code>if (led1active) led1active = update_led_1();</code>. This
will be executed on every loop iteration, it will increase the LED
brightness if the LED is supposed to be active, and it will set
<code>led1active</code> to false when the brightness ramp is complete.</p>

<p>There is something I do not like in the solution I am suggesting here:
the copy-and-paste programming paradigm. If I am allowed to use C++
constructs, then I would encapsulate the ramp logic in a C++ class:</p>

<pre class="lang-cpp prettyprint-override"><code>class RampingLed {
    const uint8_t pin;
    bool active;
    uint8_t level;
public:
    RampingLed(uint8_t pin)
        : pin(pin), active(false), level(0) {}
    void start() {
        active = true;
        level = 0;
    }
    void update() {
        if (!active) return;
        analogWrite(pin, level++);
        if (level == 0)
            active = false;
    }
};
</code></pre>

<p>Then the logic of the main program would appear more straightforward:</p>

<pre class="lang-cpp prettyprint-override"><code>RampingLed led1(LED1), led2(LED2), led3(LED3);

void loop()
{
    static int periodIndex = 0;  // increasing every 10 ms

    // Start the LEDs at the appropriate times.
    if (periodIndex == 0)
        led1.start();
    if (periodIndex == 1000)
        led2.start();
    if (periodIndex == 1500)
        led3.start();

    // Update every LED.
    led1.update();
    led2.update();
    led3.update();

    if (++periodIndex == 4000)
        periodIndex = 0;
    delay(10);
}
</code></pre>
