// --------------------------------------------------------------------------------
/*
11 三色 LED 彩虹特效
[學習重點]
1. 彩虹七色的計算方法
[挑戰]
- 思考一下如何簡化程式,不需實作,因為答案明天會揭曉。
Created by Jason on 11 Aug 2022.
*/
// --------------------------------------------------------------------------------
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
void setup()
{
pinMode(redPin, OUTPUT); // 設定為輸出模式
pinMode(bluePin, OUTPUT); // 設定為輸出模式
pinMode(greenPin, OUTPUT); // 設定為輸出模式
}
void loop()
{
// 紅 - 黃
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, 255);
analogWrite(greenPin, fade);
analogWrite(bluePin, 0);
delay(50);
}
// 黃 - 綠
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, 255 - fade);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(50);
}
// 綠 - 青
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, fade);
delay(50);
}
// 青 - 藍
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, 0);
analogWrite(greenPin, 255 - fade);
analogWrite(bluePin, 255);
delay(50);
}
// 藍 - 紫
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, fade);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(50);
}
// 紫 - 紅
for (int fade = 0; fade <= 255; fade = fade + 5) {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255 - fade);
delay(50);
}
}