Monday 14 January 2013

Arduino Meets Yogic Arts

The Relaxino!


Grove Ear-clip Heart Monitor for Arduino

I just bought this item and got it working nicely with the Seeduino test-code. Beware some peoples ear lobes are too thick (mine are) so play around with the position. I had to put it on a thin flattish part of my ear.

Available in th UK from digitalmeans.co.uk. Heart Sensor with ear-clip

I made a first attempt at a simple relaxation device. Enter the Relaxino :)

Explanation of how it works.
It works by measuring your heart rate and taking an average. Each heart beat is displayed on either a red or green LED. An LED flashes at the approx frequency of your heart rate to give you biofeedback. You set a target rate in the code, so a red LED flashes when you are above target and a green one when you are at, or below the target heart rate (set in code). This allows you to try to relax and get feedback on how you are doing. Target achievement is assessed by averaging over 20 beats. Future versions will allow setting heart rate by external controls.

Set the target initially close to your resting heart rate (mine is typically 65). It needs 30 beats to get going so both LED's will flash on startup / Arduino reset. The word "BEAT" will print in the monitor for each heart beat detected, and an LED will flash on each heart beat. You can check by feeling the pulse in your neck and watching the LED - both should be approx. in sync.

Lie or sit down and try to relax, taking long deep breaths in and out, empty your mind and gently focus on the LED light (being in the dark helps so you can see the pulses through closed eyelids).

Gradually increase the length of the breaths, counting if you need to: 6-8 seconds for in, 6-8 seconds for out (or do whatever you can manage). Build up to longer and longer ins and outs. Don't rush it and get out of breath - your heart will beat faster!

With practice it can help you relax by providing the simplest of biofeedback http://en.wikipedia.org/wiki/Biofeedback.

Attach the anodes of a green LED on digital pin 13, and a red LED on pin 12.
Current limiting resistors used for the LED's are 560 Ohm and connect cathodes via the resistor to ground pins

Geekery Recipe:
  • The device uses a RISING interrupt on pin 2,  so attach the output wire (yellow) of the heart monitor to pin 2 of the Arduino.
  •  Black wire goes to ground and red to +5v.
  • Attach the ear clip to your left ear. Beware some peoples ear lobes are too thick (mine are) so play around with the position. I had to put it on a thin flattish part of my ear.
  • Upload the code to the board. 
  • Set your heart rate target e.g. 65bpm
  • Open the serial monitor


CODE----------------------------------------------------------------------------------------
/*  Program to measure and display heart beats on two led's (and on serial monitor for debug)
*   Green led flashes at approx freq of your heart rate when you are at or below your target, 
*   if you go above this (eg 65 bpm) the red. 
*   led flashes at the approx freq of your heart rate
*   You can check by feeling your pulse 
*   in your neck and looking at LED - both should be in sync
*   C. Turner 16/01/2013. 
 
 *  Seeed product wiki http://www.seeedstudio.com/wiki/Grove_-_Heart_rate_ear_clip_kit
*  Avaialible in UK here:  https://digitalmeans.co.uk/shop/sensors-category/sensors-medical-category/grove-ear_clip_heart_rate_sensor
*  More detailed project write-up here: http://gampageek.blogspot.co.uk/2013/01/arduino-meets-yogic-arts-seeduino-ear.html

// Sketch is provided "As is" with no guarantees, or support from the Author. 
// Help with Arduino and shields can be found by joining the forum on the Arduino website: http://arduino.cc/en/ 

*/
unsigned char HIpin = 13;//red led = over target & use 560 Ohm resitor in series
unsigned char LOpin = 12;// green = @target heart rate or below & use 560 Ohm resitor in series
// times 
unsigned long t1 = 0;
unsigned long t2 = 0;

unsigned long counter = 0; // count interrupts = beats
unsigned long secsElapsed = 0; // time for 60 beats
unsigned long myCount = 0; // heart beat count so far

float heartRate = 0; // instantaneous rate
float averageHeartRate = 0;// ave over 30 beats

boolean beatDetect = false; 

void setup()
{
  pinMode(HIpin, OUTPUT);
  pinMode(LOpin, OUTPUT);


  Serial.begin(9600);
  Serial.println("Please ready your ear clip.");
  digitalWrite(HIpin, HIGH);// red led off

  delay(2000);//time to place clip, note that some people's earlobes are too thick (mine are)
 // I had to place it on the main ear. May need to play until you get sensible results.
 // change to 5000 or more if need more time.
 
  digitalWrite(HIpin, LOW);// leds off
  digitalWrite(LOpin, LOW);
  
  Serial.println("Heart rate test begin.");// debug

  t1 = millis(); // set start time.

  attachInterrupt(0, interrupt, RISING);//set interrupt 0, digital pin 2 - 
  //Seed / grove heart monitor out pin is attched here.
  
}
void loop()
{

  if (beatDetect == true)// if interrupted on pin 2 ie beat was detected
  {
    //*****************************************************************************     

    Serial.println("BEAT");// debug: print BEAT on each interrupt

    if (counter > 20 )// use heart rate when 20 beats averaged
    { 
      Serial.println("Heart Rate is: ");// debug
      averageHeartRate = heartRate;
      Serial.print(averageHeartRate);// debug
      Serial.println();// debug
    }
    //************************************************************************************   


    counter = counter + 1 ;

    t2 = millis(); // check time 

    myCount = counter;// heart beats so far
    
    secsElapsed = (t2 - t1 ) / 1000 ; // seconds elapsed 
    
    heartRate =  (float(myCount) / float(secsElapsed) ) * 60;// beats per minute
    
    beatDetect = false;// reset for next interrupt (beat)

    if (counter == 61) 
    {
      counter = 0; // reset counter after 30 beats
      t1 = millis(); // reset t1 to millis()
    }


    //display the info **************************
    if (averageHeartRate != 0)
    {
// SET TARGET HEART BEAT HERE ++++++++++++++++++++++++++++++++++++++++++++++++
      if (averageHeartRate > 63)// my average resting heart rate = 65 bpm - choose your own
      {
        //HIGH target RED led
        digitalWrite(HIpin, HIGH);// red led on
        delay10thsOfSeconds (1);// flash at heart beat rate
        digitalWrite(HIpin, LOW);// red led off
        delay10thsOfSeconds (1);// flash at heart beat rate
        digitalWrite(LOpin, LOW);// green led off
      }


      if (averageHeartRate <= 63) // my average resting heart rate = 65bpm choose your own
      {   
        //LOW target green led   
        digitalWrite(LOpin, HIGH);// green led on
        delay10thsOfSeconds (1);// flash at heart beat rate
        digitalWrite(LOpin, LOW);// gree led off
        delay10thsOfSeconds (1);// flash at heart beat rate
        digitalWrite(HIpin, LOW);// red led off
      } 
    }

    else if (averageHeartRate == 0)// when starting up flash both leds

    {
      digitalWrite(HIpin, HIGH);// red led on
      delay10thsOfSeconds (1);// flash at heart beat rate
      digitalWrite(HIpin, LOW);// red led off
      delay10thsOfSeconds (1);// flash at heart beat rate
      digitalWrite(LOpin, LOW);// green led off

      digitalWrite(LOpin, HIGH);// green led on
      delay10thsOfSeconds (1);// flash at heart beat rate
      digitalWrite(LOpin, LOW);// gree led off
      delay10thsOfSeconds (1);// flash at heart beat rate
      digitalWrite(HIpin, LOW);// red led off


    }

  }// end of beat detected


}


void interrupt()
{
  beatDetect = true; 

}


void delay10thsOfSeconds (int multi){// delay multi x 0.1 sec 

  for (int i = 1; i < (multi * 10); i++) // wait 10 = 0.1 sec
  {
    delayMicroseconds(10000);   // multi x 100 x 10000 us
  }

}
END CODE-----------------------------------------------------------------------


3 comments:

  1. Hi there!

    Just wondering - how did you connect this to the arduino? Did you strip the cables? I notice the format seems to be quite arduino-unfriendly.

    Any help would be great :)

    Cheers! Looks SUPER interesting

    ReplyDelete
  2. Thanks, but i don't known function attachInterrupt(0, interrupt, RISING);my program error thic function
    Can you help me!
    my email: hthungst@gmail.com

    ReplyDelete
    Replies
    1. attachInterrupt(0, interrupt, RISING); you need to assign a constant called interrupt at the beginning of the program eg int interrupt = 2;

      Delete