首先我們來找出彩虹顏色的R,G,B值,在wiki上有前輩己經將顏色整理好。
圖片來源:
https://zh.wikipedia.org/wiki/%E5%BD%A9%E8%99%B9
接下來我們開始寫程式
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()
{
ledbreathe(255,0,0);
ledbreathe(255,165,0);
ledbreathe(255,255,0);
ledbreathe(0,255,0);
ledbreathe(0,0,255);
ledbreathe(43,0,255);
ledbreathe(87,0,255);
}
void off()
{
analogWrite(redpin,255);
analogWrite(bluepin,255);
analogWrite(greenpin,255);
}
void ledbreathe(int r,int g, int b)
{
analogWrite(redpin, 255-r);
analogWrite(greenpin, 255-g);
analogWrite(bluepin, 255-b);
delay(1000);
}
成果影片
沒有留言:
張貼留言