Created
May 26, 2010 01:36
-
-
Save shigaku/413934 to your computer and use it in GitHub Desktop.
Arduino用心拍センサーシールドのデモプログラムです。詳細はこちら: http://koress.jp/2010/05/arduinoap_shield.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A.P. Shield 05 Demo Program | |
Created by @shigaku / KORESS | |
http://koress.jp/ | |
http://twitter.com/shigaku | |
*/ | |
int sensorPin = 4; | |
int ledPin = 13; | |
int sensorValue = 0; | |
int integral_plus = 0; | |
int integral_minus = 0; | |
int count = 0; | |
int elapse_up = 0; | |
int elapse_down = 0; | |
int ave = 0; | |
void setup() { | |
// Arduino標準搭載のデモ用 | |
pinMode(ledPin, OUTPUT); | |
// デバッグ用シリアル接続開始 | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
int currentValue = analogRead(sensorPin); | |
int diff = (currentValue - sensorValue); | |
sensorValue = currentValue; | |
// 変化量が0に近く、この時点までの積分値が | |
// 一定量であれば心拍の候補として扱う | |
if ( diff < 10 && diff > -10 && integral_plus > 160 && integral_minus < -200 ) | |
{ | |
// 立ち上がり時間とたち下がり時間を調べて、 | |
// 人間っぽい数値になっていれば、心拍としてみなす。 | |
if( elapse_up > 50 && elapse_up < 400 && elapse_down > 96 && elapse_down < 800 ) { | |
// ★心拍が検出されるとここに処理が移る★ | |
count++; | |
// デバッグ用にシリアルコンソールに出力 | |
Serial.print(count,DEC); | |
Serial.println(); | |
} | |
// クリア | |
elapse_up = 0; | |
elapse_down = 0; | |
integral_plus = 0; | |
integral_minus = 0; | |
} | |
else if ( diff > 20 && diff < 280 ) | |
{ | |
// 波形の立ち上がりを検出 | |
integral_plus += diff; | |
elapse_up += 20; | |
} | |
else if ( diff < -20 && diff > -200 ) | |
{ | |
// 波形の立下りを検出 | |
integral_minus += diff; | |
elapse_down += 20; | |
} | |
// 20msごとにサンプリングするために、待つ。 | |
delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment