iooops 发表于 2016-12-30 17:05:14

【9月心率传感器试用】Heartbeat Viewer

来给Luna大大交作业……{:5_123:}

上小视频 = =
http://player.youku.com/player.php/sid/XMTg5MTA5NDk4MA==/v.swf




使用工具:
Processing
Arduino
心率传感器


1. 心率传感器连接
我参考了以下wiki的教程1:
https://wiki.dfrobot.com.cn/index.php?title=(SKU:SEN0203)%E5%BF%83%E7%8E%87%E4%BC%A0%E6%84%9F%E5%99%A8heart_rate_sensor

https://wiki.dfrobot.com.cn/images/thumb/7/79/%E5%BF%83%E7%8E%87%E4%BC%A0%E6%84%9F%E5%99%A8%E8%BF%9E%E7%BA%BF%E5%9B%BE.png/600px-%E5%BF%83%E7%8E%87%E4%BC%A0%E6%84%9F%E5%99%A8%E8%BF%9E%E7%BA%BF%E5%9B%BE.png
我用的Digital Mode, 心率传感器拨向D。

Arduino代码:
#define heartratePin A1
#include "DFRobot_Heartrate.h"

DFRobot_Heartrate heartrate(DIGITAL_MODE); ///< ANALOG_MODE or DIGITAL_MODE

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

void loop() {
uint8_t rateValue;
heartrate.getValue(heartratePin); ///< A1 foot sampled values
rateValue = heartrate.getRate(); ///< Get heart rate value
if(rateValue){
    Serial.println(rateValue);
}
delay(20);
}


2. 连接Processing并写Processing代码

如下:

import processing.serial.*; //import the Serial library

int end = 10;    // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages
String serial;   // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char")
Serial port;// The serial port, this is a new instance of the Serial class (an Object)

int ellipseSize = 0;

int bpm = 0;
int time;

float a = 0;
float ref = 1000.0 * TWO_PI/(60000);
float c;

boolean flag = false;

import ddf.minim.*;

Minim minim;
AudioSample hb;

void setup() {
// Serial
print(Serial.list());
port = new Serial(this, Serial.list(), 115200); // initializing the object by assigning a port and baud rate (must match that of Arduino)
port.clear();// function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial')
serial = null; // initially, the string will be null (empty)

// Size
size(640, 640, P2D);
//fullScreen();
frameRate(30);
smooth();
noStroke();
ellipseMode(CENTER);

ellipseSize = int(height/1.5);

// time
time = millis();

// sound heartbeat
minim = new Minim(this);
hb = minim.loadSample("heartbeat.wav", 512);
}

void draw() {
//Heartrate data
while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it
    serial = port.readStringUntil(end);
}
if (serial != null) {//if the string is not empty, print the following
    //println(serial);
    serial = trim(serial);
    bpm = int(serial);
    println("bpm = ", bpm);
}

// Visual
//background(255);
float r = random(200, 255);
float g = random(55);
float b = random(55);
   
background(color(r, g, b));

a += bpm * ref / frameRate;
c += 0.05;

pushMatrix();
translate(width/2,height/2);
rotate(a > 0 ? c : 0);
for( int i = 0; i < 10; i++) {
    noStroke();
    fill(#FF6750);
    beginShape();
    vertex(0,0);
    vertex(cos(radians(i*360/10))*400,sin(radians(i*360/10))*400);
    vertex(cos(radians((0.5+i)*360/10))*400,sin(radians((0.5+i)*360/10))*400);
    endShape();
}
popMatrix();

// heart motion
fill(0,0,0);
pushMatrix();
translate(width/2, height/2);
scale( 2 + 0.5 *sq( sin(a) > 0 ? sin(a): 0 ) );
translate(-width/2 + 165, -height/2 + 150);
// heart shape
beginShape();
vertex(150, 150);
bezierVertex( 150, 120, 100, 120, 100, 150);
bezierVertex( 100, 180, 150, 185, 150, 210 );
bezierVertex( 150, 185, 200, 180, 200, 150 );
bezierVertex( 200, 120, 150, 120, 150, 150 );
endShape();
popMatrix();

if(sin(a) > 0 && flag == false) {
    hb.trigger();
    flag = true;
    println("trigger 1");
} else if(sin(a) < 0 && flag == true) {
    flag = false;
}

textSize(16);
fill(0);
text(bpm, width - 30, height - 20);
}



= = 楼主考虑等段数刷上去了之后用蓝牙连接安卓手机做可视化和可听化。
{:5_133:}



iooops 发表于 2016-12-30 17:06:28

沙发是我的!{:5_129:}

iooops 发表于 2016-12-30 17:06:40

板凳是我的!{:5_130:}

iooops 发表于 2016-12-30 17:06:50

地板是我的!{:5_123:}

hnyzcj 发表于 2016-12-31 08:51:22

我也来源给,哈哈哈

iooops 发表于 2016-12-31 15:52:01

hnyzcj 发表于 2016-12-31 08:51
我也来源给,哈哈哈

{:5_135:}

luna 发表于 2017-1-10 15:12:41

没有硬件连接的图~有空补上啊~

iooops 发表于 2017-1-11 18:24:21

luna 发表于 2017-1-10 15:12
没有硬件连接的图~有空补上啊~

就按照WIKI连的啊……
页: [1]
查看完整版本: 【9月心率传感器试用】Heartbeat Viewer