# Keyes -手指偵測心跳模組（光量測式）

<figure><img src="https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LaZQFBYOS3O0ksiEmR1%2Fuploads%2F90pHjBXko4sEe4KqqZ6y%2F%E5%9C%96%E7%89%875.png?alt=media&#x26;token=34d7dc48-564b-4a72-9c32-7c3daf4ce7ce" alt="" width="216"><figcaption></figcaption></figure>

{% hint style="danger" %} <mark style="color:red;">**注意：**</mark>

* <mark style="color:red;">**避免電線晃動造成雜訊**</mark>
* <mark style="color:red;">**人體動作會影響訊號**</mark>
* <mark style="color:red;">**電極需確實貼合皮膚**</mark>
* <mark style="color:red;">**本感測器不能用於醫療目的。**</mark>
  {% endhint %}

### 測量前準備

將任意手指(建議食指)的前側指腹，放置在光感測器上方。

<figure><img src="https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LaZQFBYOS3O0ksiEmR1%2Fuploads%2FOLbGtwqx4NLAwEs2nST9%2F%E5%9C%96%E7%89%876.png?alt=media&#x26;token=54d23304-a8d2-4051-8ed6-3305a96251a4" alt="" width="227"><figcaption></figcaption></figure>

### Keyes -手指偵測心跳模組（光量測式）模組電路圖

* Raspberry Pi Pico W
* Raspberry Pi Pico W 擴充板
* 手指偵測心跳模組（光量測式）

{% hint style="success" %}
*<mark style="color:$warning;">**手指偵測心跳模組（光量測式）是類比訊號輸入。本範例模組的 S 腳位需接至Raspberry Pi Pico擴充板 A0/D26 腳位。**</mark>*
{% endhint %}

<figure><img src="https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LaZQFBYOS3O0ksiEmR1%2Fuploads%2F7N7eNuQMcVfYL30QbiRD%2F%E5%9C%96%E7%89%877.png?alt=media&#x26;token=87173f6b-b7b5-43ad-b74e-920cf7e976af" alt=""><figcaption></figcaption></figure>

### Arduino 程式如下

```
#include <Arduino.h>
#define samp_siz 4
#define rise_threshold 4

// Pulse Monitor Test Script
int sensorPin = A0;

void setup() {
    Serial.begin(9600);
}

void loop ()
{
    float reads[samp_siz], sum;
    long int now, ptr;
    float last, reader, start;
    float first, second, third, before, print_value;
    bool rising;
    int rise_count;
    int n;
    long int last_beat;

    for (int i = 0; i < samp_siz; i++)
      reads[i] = 0;
    sum = 0;
    ptr = 0;

    while(1)
    {
      // calculate an average of the sensor
      // during a 20 ms period (this will eliminate
      // the 50 Hz noise caused by electric light
      n = 0;
      start = millis();
      reader = 0.;
      do
      {
        reader += analogRead (sensorPin);
        n++;
        now = millis();
      }
      while (now < start + 20);  
      reader /= n;  // we got an average
      
      // Add the newest measurement to an array
      // and subtract the oldest measurement from the array
      // to maintain a sum of last measurements
      sum -= reads[ptr];
      sum += reader;
      reads[ptr] = reader;
      last = sum / samp_siz;
      // now last holds the average of the values in the array

      // check for a rising curve (= a heart beat)
      if (last > before)
      {
        rise_count++;
        if (!rising && rise_count > rise_threshold)
        {
          // Ok, we have detected a rising curve, which implies a heartbeat.
          // Record the time since last beat, keep track of the two previous
          // times (first, second, third) to get a weighed average.
          // The rising flag prevents us from detecting the same rise more than once.
          rising = true;
          first = millis() - last_beat;
          last_beat = millis();

          // Calculate the weighed average of heartbeat rate
          // according to the three last beats
          print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 * third);

          Serial.print("心跳數:");
          Serial.print(print_value);
          Serial.print("\n");
          third = second;
          second = first;        
        }
      }
      else
      {
        // Ok, the curve is falling
        rising = false;
        rise_count = 0;
      }
      before = last;
      ptr++;
      ptr %= samp_siz;
    }
}
```

### 程式執行結果

<figure><img src="https://1275793585-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LaZQFBYOS3O0ksiEmR1%2Fuploads%2FAXJmKZl3A8L42R9IaC5G%2F%E5%9C%96%E7%89%878.png?alt=media&#x26;token=8ec9babf-daed-46a5-8a61-de48fe09adfd" alt=""><figcaption></figcaption></figure>
