//RGB
LED
#define
G_PIN 3
#define
B_PIN 5
#define
R_PIN 6
int G_PIN_Duty = 0;
int B_PIN_Duty = 0;
int R_PIN_Duty = 0;
void RGB_Demo(void)
{
//Red Light On/Off
analogWrite(R_PIN, 255);
delay(1000);
analogWrite(R_PIN, 0);
delay(1000);
//Green Light On/Off
analogWrite(G_PIN, 255);
delay(1000);
analogWrite(G_PIN, 0);
delay(1000);
//Blue Light On/Off
analogWrite(B_PIN, 255);
delay(1000);
analogWrite(B_PIN, 0);
delay(1000);
}
void setup() {
analogWrite(G_PIN, 0);
analogWrite(B_PIN,
0);
analogWrite(R_PIN, 0);
RGB_Demo();
}
void Control_LED(int PIN, int DelayusTime, int Lightmode)
{
if (Lightmode ==
true)//Off to On
{
for (int i = 0; i <= 255; i++)
{
analogWrite(PIN, i);
delayMicroseconds(DelayusTime);
}
}
else //On to Off
{
for (int i = 255; i >= 0; i--)
{
analogWrite(PIN, i);
delayMicroseconds(DelayusTime);
}
}
}
void loop() {
Control_LED(R_PIN,3000,true);
Control_LED(G_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(G_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
Control_LED(R_PIN,3000,true);
Control_LED(G_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(G_PIN,3000,false);
delay(2000);
Control_LED(G_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(G_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
Control_LED(R_PIN,3000,true);
Control_LED(B_PIN,3000,true);
delay(2000);
Control_LED(R_PIN,3000,false);
Control_LED(B_PIN,3000,false);
delay(2000);
}
完成效果
注意事項