> For the complete documentation index, see [llms.txt](https://cavedu.gitbook.io/cavedu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cavedu.gitbook.io/cavedu/hangeekduino/processing_arduino/example02.md).

# 範例02\_連接Arduino\_數位輸入與類比輸入

## 【教學】Processing 互動裝置藝術結合 Arduino 小教室\_ep2 連接Arduino\_數位輸入與類比輸入

{% embed url="<https://youtu.be/GAcywIxifjY>" %}

歡迎來到第一個範例！

首先請取得 Arduino 1.8.5，並安裝好驅動程式之後燒錄 \[StandardFirmata] 程式，這個小程式可以回應來自外部的所有呼叫。是相當好用的範例，當然也有其限制，後續會再深入說明。範例路徑為：**File / Examples / Firmata / StandardFirmata**

![](/files/-M_ZhCFAPJHCNfVSfsFG)

接著要在設定 Processing IDE中設定 Arduino 函式庫。請開啟 Contribution Manager(**Sketch / Import Library... / Add Library...**)

![](/files/-M_ZhCFEMrBfPHFJKzen)

新增一個 Processing 專案，並輸入以下程式碼，確認 Arduino 首次接觸就上手套件已正確燒錄 [StandardFirmata 程式](https://www.arduino.cc/en/reference/firmata)。 輸入完成直接執行以下 pde，會看到以下執行畫面：

**按鈕未按下，可變電阻值 < 800**\
![](/files/-M_ZcX3PboGx51drscl7)

**按鈕按下，可變電阻值 > 800**\
![](/files/-M_ZcX3R-hn1R4AbhwcG)

## Processing程式

檔名 - [arduino\_input\_easy.pde](https://github.com/cavedunissin/boson/tree/master/processing_arduino/example/arduino_input_easy)

```
import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

void setup() {
  size(470, 280);

  // Prints out the available serial ports.
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(6, Arduino.INPUT);
}

void draw() {
  background(off);
  stroke(on);

  if (arduino.digitalRead(6) == Arduino.HIGH) {
    fill(on);
  } else {
    fill(off);
    rect(400, 30, 50, 50);
  }

  if (arduino.analogRead(0) > 800) {
    fill(229, 53, 53);
  } else {
    fill(152, 219, 31);
  }
  ellipse(280, 160, arduino.analogRead(0) / 8, arduino.analogRead(0) / 8);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://cavedu.gitbook.io/cavedu/hangeekduino/processing_arduino/example02.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
