找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3033|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

使用ECG的心跳指示器制作

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

LED指示燈閃爍至心臟跳動(dòng),并通過發(fā)光LED的數(shù)量和顏色顯示當(dāng)前的BPM。

硬件組件:
uECG設(shè)備× 1
Arduino Nano R3× 1
Adafruit NeoPixel Ring:WS2812 5050 RGB LED× 1
鋰離子電池1000mAh× 1
手動(dòng)工具和制造機(jī)器:
烙鐵(通用)
實(shí)際上所有繁重的工作都是由uECG 完成的- 一種小型可穿戴心電圖設(shè)備,它是開源的,具有Arduino友好的輸出引腳(該引腳在每次心跳時(shí)變高/變低)。處理這些引腳狀態(tài)比處理ECG信號(hào)更容易,我試圖從中獲得最大的收益。

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. }
復(fù)制代碼

所有資料51hei提供下載:
使用ECG的心跳指示器.zip (989.79 KB, 下載次數(shù): 6)



評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表