user23860: I want to fade a red LED up and down, then a green LED up and down, and repeat the whole cycle indefinitely.
I've been trying to run multiple functions one after another. However my current code seems to have them both fading in parallel.
I tried using a switch case, do while and the while loop but the code runs the two functions at once. So what is wrong with the code below?
This is my code:
void setup() {
    Serial.begin(9600);
    DDRD |= (1<<DDD6)|(1<<DDD5);
    TCCR0A = 0xF1;
}

uint8_t fade_seq;

void loop() {
    loop1:
    fade_seq++;
    while (fade_seq==1)
    {
        fade_red(1);
    }
    Serial.println(fade_seq);
    while (fade_seq==2)
    {
        fade_green(1);
    }
    if (fade_seq==3)
    {
        fade_seq=0;
        goto loop1;
    }
}

void fade_red(uint8_t cnt)
{
    uint8_t i,k;
    for (k=0;k<cnt;k++)
    {
        OCR0A+=1;
        _delay_ms(10);
        if (OCR0A == 255)
        {
            for (i=0;i<255;i++)
            {
                OCR0A--;
                _delay_ms(10);
            }  
        }
    }
}

void fade_green(uint8_t cnt)
{
    uint8_t i,k,c;
    for (c=0;c<cnt;c++)
    {
        for (k=0;k<255;k++)
        {
            OCR0B+=1;
            _delay_ms(10);
            if (OCR0B == 255)
            {
                for (i=0;i<255;i++)
                {
                    OCR0B--;
                    _delay_ms(10);
                }  
            }
        }
    }
}


user27557: try this-and add to it calling a function in between-as discussed in comments that gives a visible delay-note I increased the delays as its probably going too fast
uint8_t fade_seq=1;
 void setup() {
DDRD |= (1<<DDD6)|(1<<DDD5);
TCCR0A = 0xF1;
}

void loop() {
fade_seq++;
if (fade_seq==1)
{
  fade_red(1);

}

if (fade_seq==2)
{
  fade_green(1);

}
if (fade_seq==3)
{
fade_seq=0;
}
}



void fade_red(uint8_t cnt)
{
  uint8_t i,k;
  for (k=0;k<cnt;k++)
  {
    OCR0A+=1;
    _delay_ms(100);
    if (OCR0A == 255)
    {
      for (i=0;i<255;i++)
      {
        OCR0A--;
        _delay_ms(100);
      }  
    }
  }
}

void fade_green(uint8_t cnt)
{
  uint8_t i,k;
  for (k=0;k<cnt;k++)
  {
    OCR0B+=1;
    _delay_ms(1-0);
    if (OCR0B == 255)
    {
      for (i=0;i<255;i++)
      {
        OCR0B--;
        _delay_ms(100);
      }  
    }
  }
}


user313: It simply is not possible for the processor to run both functions at once. What probably is happening is that the sequence of functions executes too fast to distinguish it as two sequential processes. You'll need to do something like call them with a delay in between. It could be as simple as:
void loop() {
   fade_red();
   delay(500);

   fade_green();
   delay(500);
}

The while loops surrounding your calls do nothing at all. The effect of each one is to call its respective function once. Your code is pretty much equivalent to a switch/case statement using fade_seq as the selector, but without any control over the timing of your calls, the functions will happen in rapid sequence.

user7508: I did not quite understand what you are actually trying to accomplish.
Neither you question nor your code is very clear in this respect. And
your code is probably buggy anyway, since it does not do what you
expect.
Then, I am just guessing: you may just want to fade a red LED up and
down, then a green LED up and down, and repeat the whole cycle
indefinitely. I am guessing right?
First of all, I would recommend you stick with the functions provided by
the Arduino core library, as they will make your life easier. The kind
of low level AVR programming you are attempting here is for advanced
users. You should learn the basics first before moving to that realm.
Here is how I would write that using the plain analogRead() function:
const uint8_t RED   = 6;  // pin connected to the red LED
const uint8_t GREEN = 5;  // pin connected to the green LED

void setup()
{
    Serial.begin(9600);
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
}

// Fade an analog pin up and down.
void fade(uint8_t pin)
{
    for (int i = 1; i <= 255; i++) {
        analogWrite(pin, i);
        delay(10);
    }
    for (int i = 254; i >= 0; i--) {
        analogWrite(pin, i);
        delay(10);
    }
}

void loop()
{
    Serial.println(F("Fading RED."));
    fade(RED);
    Serial.println(F("Fading GREEN."));
    fade(GREEN);
}

Each LED fading up and down takes roughly 5 seconds:
2 ramps × 255 steps/ramp × 10 ms/step.

user10794: 
Why my loops are very complicated?

OK, here is your loop:
uint8_t fade_seq;   // <--- initially zero

void loop() {
loop1:
fade_seq++;
while (fade_seq==1)
{
  fade_red(1);
}
Serial.println(fade_seq);
while (fade_seq==2)
{
  fade_green(1);
}
if (fade_seq==3)
{
  fade_seq=0;
  goto loop1;
}
}

So you add 1 to fade_seq and then:
while (fade_seq==1)
{
  fade_red(1);
}

So fade_seq is always 1 so that loops indefinitely. Therefore you can forget about everything else after it.
So you may as well write:
void loop() {
   while (true)
    {
    fade_red(1);
    }
 }

And since loop, well, loops, that simplifies to:
void loop() {
    fade_red(1);
   }


23860: <p>I want to fade a red LED up and down, then a green LED up and down, and repeat the whole cycle indefinitely.</p>

<p>I've been trying to run multiple functions one after another. However my current code seems to have them both fading in parallel.</p>

<p>I tried using a <code>switch case</code>, <code>do while</code> and the <code>while</code> loop but the code runs the two functions at once. So what is wrong with the code below?</p>

<p>This is my code:</p>

<pre><code>void setup() {
    Serial.begin(9600);
    DDRD |= (1&lt;&lt;DDD6)|(1&lt;&lt;DDD5);
    TCCR0A = 0xF1;
}

uint8_t fade_seq;

void loop() {
    loop1:
    fade_seq++;
    while (fade_seq==1)
    {
        fade_red(1);
    }
    Serial.println(fade_seq);
    while (fade_seq==2)
    {
        fade_green(1);
    }
    if (fade_seq==3)
    {
        fade_seq=0;
        goto loop1;
    }
}

void fade_red(uint8_t cnt)
{
    uint8_t i,k;
    for (k=0;k&lt;cnt;k++)
    {
        OCR0A+=1;
        _delay_ms(10);
        if (OCR0A == 255)
        {
            for (i=0;i&lt;255;i++)
            {
                OCR0A--;
                _delay_ms(10);
            }  
        }
    }
}

void fade_green(uint8_t cnt)
{
    uint8_t i,k,c;
    for (c=0;c&lt;cnt;c++)
    {
        for (k=0;k&lt;255;k++)
        {
            OCR0B+=1;
            _delay_ms(10);
            if (OCR0B == 255)
            {
                for (i=0;i&lt;255;i++)
                {
                    OCR0B--;
                    _delay_ms(10);
                }  
            }
        }
    }
}
</code></pre>
 27557: <p>try this-and add to it calling a function in between-as discussed in comments that gives a visible delay-note I increased the delays as its probably going too fast</p>

<pre><code>uint8_t fade_seq=1;
 void setup() {
DDRD |= (1&lt;&lt;DDD6)|(1&lt;&lt;DDD5);
TCCR0A = 0xF1;
}

void loop() {
fade_seq++;
if (fade_seq==1)
{
  fade_red(1);

}

if (fade_seq==2)
{
  fade_green(1);

}
if (fade_seq==3)
{
fade_seq=0;
}
}



void fade_red(uint8_t cnt)
{
  uint8_t i,k;
  for (k=0;k&lt;cnt;k++)
  {
    OCR0A+=1;
    _delay_ms(100);
    if (OCR0A == 255)
    {
      for (i=0;i&lt;255;i++)
      {
        OCR0A--;
        _delay_ms(100);
      }  
    }
  }
}

void fade_green(uint8_t cnt)
{
  uint8_t i,k;
  for (k=0;k&lt;cnt;k++)
  {
    OCR0B+=1;
    _delay_ms(1-0);
    if (OCR0B == 255)
    {
      for (i=0;i&lt;255;i++)
      {
        OCR0B--;
        _delay_ms(100);
      }  
    }
  }
}
</code></pre>
 313: <p>It simply is not possible for the processor to run both functions at once. What probably is happening is that the sequence of functions executes too fast to distinguish it as two sequential processes. You'll need to do something like call them with a delay in between. It could be as simple as:</p>

<pre><code>void loop() {
   fade_red();
   delay(500);

   fade_green();
   delay(500);
}
</code></pre>

<p>The while loops surrounding your calls do nothing at all. The effect of each one is to call its respective function once. Your code is pretty much equivalent to a switch/case statement using fade_seq as the selector, but without any control over the timing of your calls, the functions will happen in rapid sequence.</p>
 7508: <p>I did not quite understand what you are <em>actually</em> trying to accomplish.
Neither you question nor your code is very clear in this respect. And
your code is probably buggy anyway, since it does not do what you
expect.</p>

<p>Then, I am just guessing: you may just want to fade a red LED up and
down, then a green LED up and down, and repeat the whole cycle
indefinitely. I am guessing right?</p>

<p>First of all, I would recommend you stick with the functions provided by
the Arduino core library, as they will make your life easier. The kind
of low level AVR programming you are attempting here is for advanced
users. You should learn the basics first before moving to that realm.</p>

<p>Here is how I would write that using the plain <code>analogRead()</code> function:</p>

<pre><code>const uint8_t RED   = 6;  // pin connected to the red LED
const uint8_t GREEN = 5;  // pin connected to the green LED

void setup()
{
    Serial.begin(9600);
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
}

// Fade an analog pin up and down.
void fade(uint8_t pin)
{
    for (int i = 1; i &lt;= 255; i++) {
        analogWrite(pin, i);
        delay(10);
    }
    for (int i = 254; i &gt;= 0; i--) {
        analogWrite(pin, i);
        delay(10);
    }
}

void loop()
{
    Serial.println(F("Fading RED."));
    fade(RED);
    Serial.println(F("Fading GREEN."));
    fade(GREEN);
}
</code></pre>

<p>Each LED fading up and down takes roughly 5 seconds:
2 ramps × 255 steps/ramp × 10 ms/step.</p>
 10794: <blockquote>
  <p>Why my loops are very complicated?</p>
</blockquote>

<p>OK, here is your loop:</p>

<pre><code>uint8_t fade_seq;   // &lt;--- initially zero

void loop() {
loop1:
fade_seq++;
while (fade_seq==1)
{
  fade_red(1);
}
Serial.println(fade_seq);
while (fade_seq==2)
{
  fade_green(1);
}
if (fade_seq==3)
{
  fade_seq=0;
  goto loop1;
}
}
</code></pre>

<p>So you add 1 to <code>fade_seq</code> and then:</p>

<pre><code>while (fade_seq==1)
{
  fade_red(1);
}
</code></pre>

<p>So <code>fade_seq</code> is always 1 so that loops indefinitely. Therefore you can forget about everything else after it.</p>

<p>So you may as well write:</p>

<pre><code>void loop() {
   while (true)
    {
    fade_red(1);
    }
 }
</code></pre>

<p>And since <code>loop</code>, well, loops, that simplifies to:</p>

<pre><code>void loop() {
    fade_red(1);
   }
</code></pre>
