V是VCC,R是紅色,B是藍色,G是綠色。
參考這塊模組的電路圖,內部己有限流電阻了
圖片來源:
https://docs.google.com/file/d/0ByLk3af9VgyyM0V4TG5jaElJaWc/edit
注意一下,這顆LED是共陽極的,也就是你的PWM輸出必須是0才會全亮,
255則是閉關的,下方的程式碼是關閉所有燈。
//off
analogWrite(LEDR,255);
analogWrite(LEDB,255);
analogWrite(LEDG,255);
你可以選擇單色亮起,亦可以透過PWM來控制頻色的值。
我們先透過arduino 來控制3個顏色做呼吸燈
V接在5V,R接11 ,B接10,G接9
程式碼
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
off();
}
void loop()
{
//off all led
//off();
ledbreathe(redpin);
ledbreathe(greenpin);
ledbreathe(bluepin);
}
void off()
{
analogWrite(redpin,255);
analogWrite(bluepin,255);
analogWrite(greenpin,255); //off
}
void ledbreathe(int led)
{
for(val=255; val>=0; val-=5)
{
analogWrite(led, val);
delay(30);
}
//hold on 30ms
delay(30);
for(val=255; val>0; val-=5)
{
analogWrite(led, 255-val);
delay(30);
}
}
最後是成果
沒有留言:
張貼留言