找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2309|回复: 0
收起左侧

使用ECG的心跳指示器制作

[复制链接]
ID:472844 发表于 2019-7-22 09:09 | 显示全部楼层 |阅读模式

原理图

原理图

LED指示灯闪烁至心脏跳动,并通过发光LED的数量和颜色显示当前的BPM。

硬件组件:
uECG设备× 1
Arduino Nano R3× 1
Adafruit NeoPixel Ring:WS2812 5050 RGB LED× 1
锂离子电池1000mAh× 1
手动工具和制造机器:
烙铁(通用)
实际上所有繁重的工作都是由uECG 完成的- 一种小型可穿戴心电图设备,它是开源的,具有Arduino友好的输出引脚(该引脚在每次心跳时变高/变低)。处理这些引脚状态比处理ECG信号更容易,我试图从中获得最大的收益。

Arduino源程序如下:
  1. #include <Adafruit_NeoPixel.h>

  2. #ifdef __AVR__
  3.   #include <avr/power.h>
  4. #endif

  5. // DI pin of LED ring
  6. #define PIN            11
  7. // number of pixels in the ring
  8. #define NUMPIXELS      16
  9. // input pin for connecting uECG
  10. int in_pin = 3;

  11. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

  12. void setup() {
  13.   pixels.begin(); // This initializes the NeoPixel library.
  14.   pinMode(in_pin, INPUT); //set pin to input mode
  15.   digitalWrite(in_pin, 1); //enable PULLUP: this is critical, uECG doesn't have internal pull-up
  16. }

  17. //we store last 20 heartbeats to averge BPM over them
  18. //with higher value, it will become more reliable,
  19. //but it will take more time to see output change when BPM changes
  20. #define BEAT_HIST 20
  21. long beats[BEAT_HIST];

  22. void push_beat(long ms) //shift all beats in array and insert current one
  23. {
  24.   for(int x = 0; x < BEAT_HIST-1; x++)
  25.   {
  26.     beats[x] = beats[x+1];
  27.   }
  28.   beats[BEAT_HIST-1] = ms;
  29. }

  30. int get_bpm() //using time difference between first and last beats
  31. {
  32.   long dt = beats[BEAT_HIST-1] - beats[0];
  33.   long bpm = BEAT_HIST * 60000 / dt;
  34.   return bpm;
  35. }

  36. long last_pix_upd = 0; //to keep track of when we updated pixels previous time
  37. int prev_in_state = 0; //previous state of input pin: we want to process only changes of state

  38. void loop()
  39. {
  40.   long ms = millis();
  41.   int in_state = digitalRead(in_pin); //1 when no beat detected, 0 in beat
  42.   if(in_state == 1 && prev_in_state == 0) //react only to change
  43.   {
  44.     push_beat(ms);
  45.   }
  46.   prev_in_state = in_state;
  47.   if(ms - last_pix_upd > 10) //don't update pixels too often
  48.   {
  49.     int r, g, b;
  50.     last_pix_upd = ms;
  51.     int bpm = get_bpm();
  52.     int max_bright = 120; //value of maximum brightness, max 255. But you don't always want it at max :)
  53.     float dd = 20; //change in BPM between color tones (blue->green->yellow->pink->red)
  54.     float t1 = 90, t2, t3, t4; //t1 - "base" BPM, lower than t1 would be blue
  55.     t2 = t1 + dd;
  56.     t3 = t2 + dd;
  57.     t4 = t3 + dd;
  58.     //code for changing color depending in which t1...t4 range we are now
  59.     if(bpm < t1){ r = 0; g = 0; b = max_bright; }
  60.     else if(bpm < t2) { r = 0; g = max_bright * (bpm-t1)/dd; b = max_bright - g; }
  61.     else if(bpm < t3) { r = max_bright * (bpm-t2)/dd; g = max_bright - r; b = r/4; }
  62.     else if(bpm < t4) { r = max_bright; g = 0; b = max_bright/2 - max_bright * (bpm-t3)/(2*dd); }
  63.     else {r = max_bright; g = 0; b = 0; }
  64.     if(in_state) //when not in beat, 1/4 intensity, so only beats are highlighted
  65.     {
  66.       r *= 0.25;
  67.       g *= 0.25;
  68.       b *= 0.25;
  69.     }
  70.     int on_pixels = (bpm+5)/10; //number of used LEDs: for 60 BPM, 6 LEDs will be on, for 120 - 12 etc
  71.     for(int i=0;i<NUMPIXELS;i++)
  72.     {
  73.       if(i < on_pixels) pixels.setPixelColor(i, pixels.Color(r,g,b));
  74.       else pixels.setPixelColor(i, pixels.Color(0,0,0)); //turn off all other LEDs
  75.     }
  76.     pixels.show();
  77.   }
  78. }
复制代码
0.png
所有资料51hei提供下载:
使用ECG的心跳指示器.zip (989.79 KB, 下载次数: 6)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表