Go Down

Topic: Relaxino (Read 3688 times) previous topic - next topic

trendski

Jan 16, 2013, 06:23 pm Last Edit: Jan 16, 2013, 09:02 pm by trendski Reason: 1
Hi,

I made a first attempt at a simple relaxation device. I used the Seeed Studio ear-clip heart rate monitor available in th UK from digitalmeans.co.uk.

Explanation of how it works.
It works by measuring you heart rate over 30 seconds 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. This allows you to try to relax and get feedback on how you are doing. Target is assessed by averaging over 30 beats.

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.

Constructive comments very welcome.

Code: [Select]
/*  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
 }

}





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
The device uses a RISING interrupt on pin 2,  so I attached 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 you 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.
Open the serial monitor






Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

strykeroz

You say simple, but I say clever  :)  I often see pulse rate sensors for sale and the impulse to buy is always (thus far) tempered by a lack of anything to do with one.  You may have just changed that.

Are you finding it's easier/quicker to chill after a big night debugging your Arduino projects now?

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

trendski

Thanks for the +ve feedback Geoff,

I agree with you 100%. I had a heart attack last summer so these things are uppermost in my mind. I use the breathing relaxation technique all the time, but now I have biofeedback too.

I wanted something that gave feedback which  correlated better with what you are feeling. As I said you can see the leds sync with you feeling the pulse on your neck. The 30 beat average is just to get a value to compare with the target.

I guess this could also be used for athletic training ie set high value targets and get simple visual biofeedback. I haven't tried it for that yet, but I think the chest-strap version of the pulse monitor would be more suitable, to cope with the constant jogging around https://digitalmeans.co.uk/shop/sensors-category/sensors-medical-category/grove-chest_strap_heart_rate_sensor. So setting upper and lower targets could be useful for training and cool-down.

You need to exercise when recovering from heart problems but within limits. It could help with that too because you can see when it changes over.

Did you see any problems with the code at all? My background is in Biology so I lack some confidence in the coding field.


Best Regards

Craig
Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

strykeroz

Hi Craig

It's even better with the back-story.  Clever, and useful in fact.

There's nothing that stands out as bad in the code.  If it does as designed you've achieved, and everyone has their own style.  For example I would probably have incremented the counter in the interrupt service routine and not had the beatDetect variable, and just do your maths if the counter exceeded the 20 as it does now.
Code: [Select]

void interrupt() {
  counter++;
}

Some would argue that using delays as you have is poor, but I don't subscribe to that in all instances, and because your ISR will still count pulses while it's in the blocked state of a delay I think that's just fine.

Overall you know it works and it does what you're asking of it so you've gotto be happy with that  :)

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

trendski

Hi Geoff,



Cheers, I'll take that on. I worried about that delay issue with interrupts, I should bite the bullet and improve the code.

Many Thanks

Craig
Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

focalist

Back in the stone age, one of the more interesting classes I took was Behavioral Medicine- this was in the 80's, when the whole biofeedback thing really actually gained a bit of legitimacy.  Since you already have skin resistance and there plenty more inputs, maybe youcould add in more sensors..

Oxygen saturation is a good one, sat can drop due to anxiety.. And with the same detector (IR pair on fingertip works I understand) you can determine heart rate and respiration rate.  If you've been in an ER recently, they put one of these on you.  Work great and non invasive.  A few searches should yield some info on how to implement.

THEN, you can implement session data storage and upload to PC via serial, where the data can be graphed and trended...

Products like these, though relatively simple, really do work well and can even help change things for you, medically, in the long run!
When the testing is complete there will be... cake.

trendski

I was at Uni in the 80's. Pre-web-istory and the fax-age !

They did put a sensor on my finger. I think, as you say, you can sense O2 and heat rate on the finger. I saw an article for the CES show in Las Vegas had medical sensors for mobile phones and some of these used the type of sensor you mention. Skin resistance sounds good.

I'll look at other sensors too for measuring stress / relaxation. Blood pressure comes to mind, but the devices used on me were really annoying, and involve arm-squeezing devices, motors etc...

I wonder if you can sense the pulse in the carotid artery made by the heart beating, and somehow calculate the energy in the wave, and correlate that with instantaneous blood pressure?  There seems to be quite a tangible displacement in that pulse.

I don't want to go the whole hog and make a lie-detector type device, but there must be something in between.

Cheers
Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

focalist

Well, "lie detector" was a misnomer from the start, and is of dubious value in many cases.  Was another subject covered in the class.

The proper name- polygraph- is not a negative in any way, however- and is quite accurate a description.  It graphs (reads, stores, writes, manipulates, etc) poly (multiple) physical properties of the body.  The whole idea of it being a lie detector was that if you lie your stress level increases,  and can be sensed.. Obviously a flawed idea from the start.  Sociopaths and psychopaths both feel no stress when lying,, and those are who you are trying to catch.

Biofeedback is an awesome subject, and leads you down a lot of cool thought paths that can even be prototyped with our favorite little toy,...

One warning though, must be made.  Take EXTREME care when dealing with electricity and the body.  One mistake can kill.  Everything battery powered, no mains,  isolated as much as possible,  measure every voltage and current at every contact point to make sure, etc etc etc.  I'm sure there's a few darwin awards that started out as tinkering, don't become one!    :D
When the testing is complete there will be... cake.

mansoor

hi,
I'm completely new to ardiuno and i got a project to build a spring stretch (displacement) meter.This is achieved by altering the frequency of oscillator circuit to which spring is attached, acting as an inductor. The inductance varies with the stretch of the spring attached to the circuit. Microcontroller is programmed in such a way to count the frequency and display the corresponding displacement on the LCD. i have made the oscillator circuit now I need help with programming the ardiuno. please let me know. i would be grateful.
mans

trendski

#9
Feb 05, 2013, 12:45 pm Last Edit: Feb 05, 2013, 01:07 pm by trendski Reason: 1
Hi Mansoor,

I'm sure there will be people to help, make a new topic in the sensors section:


http://arduino.cc/forum/index.php/board,10.0.html

Quote
spring stretch (displacement) meter


Attach a circuit diagram and picture of your spring and device

Cheers
Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

trendski

#10
May 09, 2013, 03:10 pm Last Edit: May 09, 2013, 03:16 pm by trendski Reason: 1
I thought I should post an update to this thread. I have written a beta Android app to work with this project over bluetooth. See http://gampageek.blogspot.co.uk/2013/04/chillout-with-relaxino-heart-app-few.html where all the code is listed for free and instructions given, all open source. The beta App is also on google play https://play.google.com/store/apps/details?id=biz.consett.relaxino&feature=search_result#?t=W251bGwsMSwyLDEsImJpei5jb25zZXR0LnJlbGF4aW5vIl0.

I hope it's useful. If you use it please link back to my blog.

Thanks

edit: It doesn't have any adverts on in-app nags either.
Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

Go Up