How to run multiple functions one after another? 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&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);
                }  
            }
        }
    }
}
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 &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);
}


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