Program it

Eight FLORA NeoPixels, arranged in the shape of a heart, are controlled by the following code according to incoming beats from the heart rate sensor. The LEDs flash brightly with each beat, visually representing your beating heart. Make sure you've got the NeoPixel library installed, then copy and paste the following code into the Adafruit Arduino IDE:
Download: file
/*
Heart Rate Badge with heart-shaped neopixel display
written by Becky Stern for Adafruit Industries
Based on sample code from http://learn.parallax.com/KickStart/28048
*/

#include <Adafruit_NeoPixel.h>

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 12, NEO_GRB + NEO_KHZ800);

//Definitions  
const int HR_RX = 2;
byte oldSample, sample;

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  colorWipe(strip.Color(20, 0, 0), 50); // Red
  
  Serial.begin(9600);
  pinMode (HR_RX, INPUT);  //Signal pin to input  
  
  Serial.println("Waiting for heart beat...");

 // Wait until a heart beat is detected  
  while (!digitalRead(HR_RX)) {};
  Serial.println ("Heart beat detected!");
}

void loop() {
  sample = digitalRead(HR_RX);  //Store signal output 
  if (sample && (oldSample != sample)) {
    Serial.println("Beat");
    heartBeat();
  }
  oldSample = sample;           //Store last signal received 
  

for (volatile int i=0; i<strip.numPixels(); i++){
      strip.setPixelColor(i, strip.Color(20, 0, 0)); 
    }
    strip.show();
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void heartBeat (){
  Serial.println("heartbeat");
for (volatile int i=0; i<strip.numPixels(); i++){
      strip.setPixelColor(i, strip.Color(255, 0, 0)); 
    }
    strip.show();
   delay(10);
}
Here's some code for a different version of the badge, using our i2c 8x8 matrix display. It scrolls your current beats per minute across the display, great for taking with you on your workout. You will need the GFX Library and the LED Backpack Library.
Download: file
/*
Heart Rate Badge with 8x8 i2c matrix
displays beats per minute
written by Becky Stern for Adafruit Industries
BPM calculation adapted from http://randomcontent.wolfnexus.net/RandomSite/arduino-hrm/
*/

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

volatile int diff1 = 0;
volatile int diff2 = 0;
volatile int diff3 = 0;
volatile int diff4 = 0;
volatile int diff5 = 0;
volatile int diff6 = 0;
volatile int diff7 = 0;
volatile int diff8 = 0;
volatile int diff9 = 0;
volatile int diff10 = 0;
int BPM, BPMforDisplay;
unsigned long iterationCounter;
int animationPosition = 4;
byte oldSample, sample;
long pulsetime, lastpulsetime;

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

void setup() {
  Serial.begin(9600);
  Serial.println("8x8 LED Matrix Test");
  pinMode(10, INPUT);
  Serial.println("Waiting for heart beat...");

  //Wait until a heart beat is detected  
  while (!digitalRead(10)) {};
  Serial.println ("Heart beat detected!");
  matrix.begin(0x70);  // pass in the address
  matrix.setTextSize(1);
  matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
  matrix.setTextColor(LED_ON);
}

void loop() {
/*
  sample = digitalRead(10);  //Store signal output 
  if (sample && (oldSample != sample)) {
    Serial.print("Beat: ");
    Serial.println(BPM);
    HRpulse();
  }
  oldSample = sample;           //Store last signal received
  */
  if (animationPosition < -20){
    animationPosition = 6;
    BPMforDisplay = BPM;
  }
      sample = digitalRead(10);  //Store signal output 
  if (sample && (oldSample != sample)) {
    Serial.print("Beat: ");
    Serial.println(BPM);
    HRpulse();
  }
  oldSample = sample;           //Store last signal received
    if (iterationCounter % 900 == 0){
    
    matrix.clear();
    matrix.setCursor(animationPosition,0);
    matrix.print(BPMforDisplay);
    matrix.writeDisplay();
    //delay(100);
    animationPosition--;
  }
  
 iterationCounter++;
 //Serial.println(iterationCounter % 1200);
}

void HRpulse()
{
  pulsetime = millis();
  rollBuffer();
  diff1 = pulsetime - lastpulsetime;
  if (diff10 != 0) {
    BPM = 60000 / ((diff1 + diff2 + diff3 + diff4 + diff5 + diff6 + diff7 + diff8 + diff9 + diff10)/10);
  }
  lastpulsetime = pulsetime;
}

void rollBuffer()
{
  diff10 = diff9;
  diff9 = diff8;
  diff8 = diff7;
  diff7 = diff6;
  diff6 = diff5;
  diff5 = diff4;
  diff4 = diff3;
  diff3 = diff2;
  diff2 = diff1;
  diff1 = 0;
}
This guide was first published on Jul 09, 2013. It was last updated on Jul 09, 2013. This page (Program it) was last updated on May 04, 2015.