# 触摸传感器

{% embed url="<https://www.bilibili.com/video/BV1Yx4y1K75q/?vd_source=2653c3369c56df6df4635a87124da1b4>" %}

## 功能简介

该传感器包含左右两个触摸部件，当手指靠近时，它可以检测到电容的变化。 这意味着无论您的手指是轻微触摸还是用力按压，触摸传感器都会输出高电平。您可以通过判断检测到的数值（高电平 1，低电平 0）来实现自己想要的功能。

<figure><img src="/files/YZV7FjAodaylqbUYqOSo" alt=""><figcaption></figcaption></figure>

## 硬件设置

使用电线连接到 NyBoard，如下图所示：

<figure><img src="/files/6g8PZ8Q7WwyTlYBzmwcI" alt=""><figcaption></figcaption></figure>

具体使用时可以将连接传感器的一端固定在机器人的头部（含在Bittle的嘴中，或者顶在Nybble的头部），当然您也可以根据实际需要发挥您的创意。

## 软件设置

[**OpenCat**](https://github.com/PetoiCamp/OpenCat) 项目代码中目前已经集成了使用此传感器的代码，将 **OpenCat.ino** 中&#x7684;**`#define DOUBLE_TOUCH`**&#x884C;取消注释，如下图所示,  然后使用 [Arduino IDE](https://www.arduino.cc/en/software) 将程序上传到机器人主板，可以复现整合了机器人动作的示例功能。

<figure><img src="/files/MAe4V5B4v10COShl2nQR" alt=""><figcaption></figcaption></figure>

如果您想单独测试触摸传感器功能，或者想进一步了解其功能实现原理。您可以使用 [Arduino IDE](https://www.arduino.cc/en/software) 上传示例程序（doubleTouch.ino），如下图所示：

<figure><img src="/files/c3Dl0b6BI0wETpDnoMBb" alt=""><figcaption></figcaption></figure>

示例程序代码（doubleTouch.ino）实现了在[串口监视器](https://docs.petoi.com/v/chinese/arduino-ide/chuan-kou-jian-shi-qi)中实时打印出D6, D7两个引脚的检测值：

<figure><img src="/files/dD01Btp9zN67vRaE7sQP" alt=""><figcaption></figcaption></figure>

### 示例程序代码

{% file src="/files/xTJXB7xiuE91cC88x13x" %}


---

# 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://docs.petoi.com/chinese/kuo-zhan-mo-kuai/chu-mo-chuan-gan-qi.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.
