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);
}
}
}
}
}
first_function(); second_function();
loop
function looks to me the same as:while (true) fade_red(1);