# A03. 外接 LED 呼吸燈

### 專案說明 <a href="#a03.-wai-jie-led-hu-xi-deng-zhuan-an-shui-ming" id="a03.-wai-jie-led-hu-xi-deng-zhuan-an-shui-ming"></a>

外接 LED 發光呈現呼吸效果， 忽明忽暗

### 電路圖 <a href="#a03.-wai-jie-led-hu-xi-deng-dian-lu-tu" id="a03.-wai-jie-led-hu-xi-deng-dian-lu-tu"></a>

* [LinkIt 7697](https://robotkingdom.com.tw/product/linkit-7697/)
* 麵包板
* LED
* 1KΩ 電阻
* 杜邦線

![](/files/twMHxJGZSD06mLkl6TOR)

(圖片由 [Fritzing](http://fritzing.org/home/) 產生, LinkIt 7697 fritzing [下載](https://www.dropbox.com/s/44rgxihq1nri2qn/linkit_7697_fritzing_and_diagrams.zip))

### 積木畫布 <a href="#a03.-wai-jie-led-hu-xi-deng-ji-mu-hua-bu" id="a03.-wai-jie-led-hu-xi-deng-ji-mu-hua-bu"></a>

使用類比輸出方式來控制 LED 明暗。 讓類比輸出的值(變數 led)， 在 0\~255 隨著時間往返， 呈現一個自然燈光明暗的效果

![](/files/PdhuRbjrEm2uAv9uvy8T)

產生出的 Arduino 程式如下

```
int led;
int fade;

void setup()
{
  pinMode(2, OUTPUT);
  led = 0;
  fade = 5;
  digitalWrite(2, LOW);
  Serial.begin(9600);
}

void loop()
{
  analogWrite(2, led);
  led = led + fade;

  if (led <= 0) {
    fade = 5;
  } else if (led >= 255) {
    fade = -5;
  }

  Serial.println(led);
  delay(30);
}
```

### **衍伸學習** <a href="#a03.-wai-jie-led-hu-xi-deng-yan-shen-xue-xi" id="a03.-wai-jie-led-hu-xi-deng-yan-shen-xue-xi"></a>

**數位 I/O** 和 **類比 I/O** 的積木，都會有兩個類似，但參數帶入方式不一樣的變化型。

* **常數版** : 帶入參數是邊編輯積木時就決定了
* **變數版** : 帶入的參數是執行時動態產生帶入

![](/files/abSWWq1FHK2zRnXHLnZ8)

變數版的好處是，可以透過程式來在**執行時才帶入參數**。下面這範例是我們要把 Pin 0 到 Pin 9 共九根針腳都數位寫入到高電位，就可以看出使用變數版的方便性了。

![](/files/Jnmfsb1sC4KVGU0GU5Hx)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cavedu.gitbook.io/linkit-7697/linkit-7697-blocklyduino-shi-yong-zhi-nan/shi-zuo-fan-li-jiao-xue/a03.-wai-jie-led-hu-xi-deng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
