Realization of colorful gradient and SOS effect of single-chip RGB three-color lamp

First we need to use a hardware timer to implement something similar to software PWM, open a timer and run the following code to achieve what we want. Then we only need to change the r_duty, g_duty, b_duty duty cycles in order to achieve the desired effect. .

 if(r_duty == 0)
{
BSP_LED_Off(LEDR);
}
else if(r_duty == 255)
{
BSP_LED_On(LEDR);
}
else
{
if(now_duty < r_duty)
{
BSP_LED_On(LEDR);
}
else
{
BSP_LED_Off(LEDR);
}
}
if(g_duty == 0)
{
BSP_LED_Off(LEDG);
}
else if(g_duty == 255)
{
BSP_LED_On(LEDG);
}
else
{
if(now_duty < g_duty)
{
BSP_LED_On(LEDG);
}
else
{
BSP_LED_Off(LEDG);
}
}
if(b_duty == 0)
{
BSP_LED_Off(LEDB);
}
else if(b_duty == 255)
{
BSP_LED_On(LEDB);
}
else
{
if(now_duty < b_duty)
{
BSP_LED_On(LEDB);
}
else
{
BSP_LED_Off(LEDB);
}
}
}
now_duty + + ;

The seven-color gradient effect is realized. In this example, the seven-color gradient lights are preceded by seven monochromatic lights of red, green, blue, yellow, purple, cyan and white. The seven colors are realized with these seven colors. The monochromatic lights only need to be given an initial The duty cycle can be achieved and will not be discussed in this example, so now_rgb starts from 8. The following code is for learning reference only.

if(now_rgb == 8)//Colorful light mode
{
if(now_duty == 255)
{
switch(now_Seven_and_SOS_Step)
{
case 0 :
if(now_seven_and_SOS_Cnt == 0)
{
if(r_duty == 255)
{
now_seven_and_SOS_Cnt = 1;
}
else
r_duty + + ;
}
else
{
if(r_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
r_duty --;
}
break;
case 1:
if(now_seven_and_SOS_Cnt == 0)
{
if(g_duty == 255)
{
now_seven_and_SOS_Cnt = 1;
}
else
g_duty + + ;
}
else
{
if(g_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
g_duty --;
}
break;
case 2:
if(now_seven_and_SOS_Cnt == 0)
{
if(b_duty == 255)
{
now_seven_and_SOS_Cnt = 1;
}
else
b_duty + + ;
}
else
{
if(b_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
b_duty --;
}
break;
case 3:
if(now_seven_and_SOS_Cnt == 0)
{
if(g_duty == 255)
{
now_seven_and_SOS_Cnt = 1;
}
else
{
r_duty + + ;
g_duty + + ;
}
}
else
{
if(g_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
{
r_duty --;
g_duty --;
}
}
break;
case 4:
if(now_seven_and_SOS_Cnt == 0)
{
if(r_duty == 128)
{
now_seven_and_SOS_Cnt = 2;
}
else
{
now_seven_and_SOS_Cnt = 1;
}
}
else if(now_seven_and_SOS_Cnt == 1)
{
r_duty + + ;
b_duty + + ;
now_seven_and_SOS_Cnt = 0;
}
else if(now_seven_and_SOS_Cnt == 2)
{
if(r_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
{
now_seven_and_SOS_Cnt = 3;
}
}
else if(now_seven_and_SOS_Cnt == 3)
{
r_duty --;
b_duty --;
now_seven_and_SOS_Cnt = 2;
}
break;
case 5:
if(now_seven_and_SOS_Cnt == 0)
{
if(b_duty == 128)
{
now_seven_and_SOS_Cnt = 2;
}
else
{
now_seven_and_SOS_Cnt = 1;
}
}
else if(now_seven_and_SOS_Cnt == 1)
{
g_duty + + ;
b_duty + + ;
now_seven_and_SOS_Cnt = 0;
}
else if(now_seven_and_SOS_Cnt == 2)
{
if(b_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step + + ;
}
else
{
now_seven_and_SOS_Cnt = 3;
}
}
else if(now_seven_and_SOS_Cnt == 3)
{
g_duty --;
b_duty --;
now_seven_and_SOS_Cnt = 2;
}
break;
case 6:
if(now_seven_and_SOS_Cnt == 0)
{
if(g_duty == 255)
{
now_seven_and_SOS_Cnt = 1;
}
else
{
r_duty + + ;
g_duty + + ;
b_duty + + ;
}
}
else
{
if(g_duty == 0)
{
now_seven_and_SOS_Cnt = 0;
now_Seven_and_SOS_Step = 0;
}
else
{
r_duty --;
g_duty --;
b_duty --;
}
}
break;
}
}
}

SOS function realization: the red light flashes 3 times quickly, then flashes slowly 3 times, then flashes quickly 3 times, then pauses for 8s, and then continues the above cycle of flashing (fast flashes 1s three times, slow flashes 2s once)

else if(now_rgb == 9)//SOS mode
{
if(now_duty == 255)
{
switch(now_Seven_and_SOS_Step)
{
case 0 :
case 1:
case 2:
if(now_seven_and_SOS_Cnt < 30)
{
r_duty = 255;
g_duty = 0;
b_duty = 0;
}
else if(now_seven_and_SOS_Cnt < 60)
{
r_duty = 0;
}
else
{
now_Seven_and_SOS_Step + + ;
now_seven_and_SOS_Cnt = 255;
}
now_seven_and_SOS_Cnt + + ;
break;
case 3:
case 4:
case 5:
if(now_seven_and_SOS_Cnt < 180)
{
r_duty = 255;
g_duty = 0;
b_duty = 0;
}
else if(now_seven_and_SOS_Cnt < 240)
{
r_duty = 0;
}
else
{
now_Seven_and_SOS_Step + + ;
now_seven_and_SOS_Cnt = 255;
}
now_seven_and_SOS_Cnt + + ;
break;
case 6:
case 7:
case 8:
if(now_seven_and_SOS_Cnt < 30)
{
r_duty = 255;
g_duty = 0;
b_duty = 0;
}
else if(now_seven_and_SOS_Cnt < 60)
{
r_duty = 0;
}
else
{
now_Seven_and_SOS_Step + + ;
now_seven_and_SOS_Cnt = 255;
}
now_seven_and_SOS_Cnt + + ;
break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
if(now_seven_and_SOS_Cnt >= 240)
{
if(now_Seven_and_SOS_Step == 14)
now_Seven_and_SOS_Step = 0;
else
now_Seven_and_SOS_Step + + ;
now_seven_and_SOS_Cnt = 255;
}
now_seven_and_SOS_Cnt + + ;
break;
}
}
}

The above code is implemented using the time base now_duty used in the above rgb duty cycle, and the specific time can be adjusted in time.

RGB three-color light colorful gradient and SOS effect implementation source code