你好: 有一個問題想很久還是想不出來,請有緣的朋友替我解答 感恩
Q.我要設計2顆led燈能呈現交替呼吸燈(一顆漸漸亮,另一顆漸漸暗)+透過電腦端的Serial Monitor 控制"呼吸"時間
目前2顆替換呼吸燈大概瞭解 主要控制呼吸燈時間程式該如何寫?(想破頭了)
以下是替換呼吸燈程式:
/* 交替呼吸燈 */
int led1 = 10; // the pin that the LED is attached to
int led2 = 11; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 10; // how many points to fade the LED by
// 此 setup 程序只有在微控制器按 reset 時執行一次
void setup() {
// declare pin 10 to be an output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
analogWrite(led1, brightness);
analogWrite(led2, 255-brightness);
// 透過迴圈改變下次的亮度值
brightness = brightness + fadeAmount;
// 進行邊界值的限制
if (brightness>255) brightness=255;
if (brightness<0) brightness=0;
// 判斷亮度值是否到達最小值或最大值
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// 為了方便觀察微量的亮度變化需延遲一小段時間
delay(30);
} |