28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
return 1;
}
printf("Program is starting ...\n");
ledInit();
while(1){
r=random()%100;
g=random()%100;
b=random()%100;
ledColorSet(r,g,b);
printf("r=%d, g=%d, b=%d \n",r,g,b);
delay(300);
}
return 0;
}
First, in subfunction of ledInit(), create the software PWM control pins used to control the R, G, B pin
respectively.
void ledInit(void)
{
softPwmCreate(ledPinRed, 0, 100);
softPwmCreate(ledPinGreen,0, 100);
softPwmCreate(ledPinBlue, 0, 100);
}
Then create subfunction, and set the PWM of three pins.
void ledColorSet(int r_val, int g_val, int b_val)
{
softPwmWrite(ledPinRed, r_val);
softPwmWrite(ledPinGreen, g_val);
softPwmWrite(ledPinBlue, b_val);
}
Finally, in the “while” cycle of main function, get three random numbers and specify them as the PWM duty
cycle, which will be assigned to the corresponding pins. So RGBLED can switch the color randomly all the time.
while(1){
r=random()%100;
g=random()%100;
b=random()%100;
ledColorSet(r,g,b);
printf("r=%d, g=%d, b=%d \n",r,g,b);
delay(300);
}