2016-12-30 17:05:14 [显示全部楼层]
6065浏览
查看: 6065|回复: 7

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

[复制链接]
来给Luna大大交作业……

上小视频 = =





使用工具:
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


我用的Digital Mode, 心率传感器拨向D。

Arduino代码:
[mw_shl_code=c,true]#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);
}
[/mw_shl_code]

2. 连接Processing并写Processing代码

如下:
[mw_shl_code=java,true]
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()[1]);
  port = new Serial(this, Serial.list()[1], 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);
}[/mw_shl_code]



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




HeartRate Sensor.zip

35.71 KB, 下载次数: 2921

iooops  中级技匠
 楼主|

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

沙发是我的!
回复

使用道具 举报

iooops  中级技匠
 楼主|

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

板凳是我的!
回复

使用道具 举报

iooops  中级技匠
 楼主|

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

地板是我的!
回复

使用道具 举报

hnyzcj  版主

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

我也来源给,哈哈哈
回复

使用道具 举报

iooops  中级技匠
 楼主|

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

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

回复

使用道具 举报

luna  初级技神

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

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

使用道具 举报

iooops  中级技匠
 楼主|

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

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

就按照WIKI连的啊……
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail