Heart Rate Monitor using Android and Arduino

May 2, 2012


Last week I decided to buy some stuff from local reseller of Arduino. I bought a Kyto Heart Sensor that transmits heart pulses to a receiver via radio frequency. The RF receiver can then be interfaced to an arduino and then pass the results to an Android device for visualization.

I’ve used the same Sketch from Seeduino website and just modified it a little bit for ADK.

Setup


void setup()
{
  pinMode(pin, OUTPUT);

  Serial.begin(9600);
  acc.powerOn();

  Serial.println("Please ready your chest belt.");
  delay(5000);
  array_init();
  Serial.println("Heart rate test begin.");
  attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2
}

In the setup function, the most interesting part is the use of attachInterrupt function. What it does is it binds a callback that will be called when a RISING signal is detected on interrupt 0 which is digital port 2 in Arduino. More details about attachInterrupt  on the Arduino documentation. So the data pin in our RF receiver should go to pin 2 in Arduino. Each time the receiver receives a heart pulse from the sensor, it triggers the interrupt and calls the callback function “interrupt”.

Loop


void loop()
{
  if (acc.isConnected()) {
          digitalWrite(pin, state);
   }

  delay(50);

}

For the loop part, I don’t really understand why it needs to call digitalWrite on pin 13 even though there’s nothing connected on that pin.

Finally, the interrupt function simply collects the timings for consecutive 20 heart beats and sums it up and will be sent  to Android device via AndroidAccessory.write call.


void interrupt()
{
    temp[counter]=millis();
    state = !state;
    Serial.print("Received heartbeat: ");
    Serial.println(temp[counter]);

    byte msg[2];
    msg[0] = 0;
    msg[1] = temp[counter];
    acc.write(msg, 2);

    switch(counter)
      {
       case(0):
       sub=temp[counter]-temp[20];

       break;
       default:
       sub=temp[counter]-temp[counter-1];

       break;
      }
    if(sub > max_heartpluse_duty)//set 2 seconds as max heart pulse duty
      {
        data_effect=0;//sign bit
        counter=0;
        Serial.println("Heart rate measure error,test will restart!" );
        array_init();
       }
    if (counter==20 && data_effect)
    {
      counter=0;
      sum();
    }
    else if(counter!=20 && data_effect){
      counter++;
    }else
    {
      counter=0;
      data_effect=1;
    }
}

So for each heartbeat, I am also sending a signal to Android device so that we can make some visualizations such as a heart beating and some cool graph.

In the application side, everything is straightforward. The only thing that’s a bit difficult to carry out is the graph view. After the Android device receives the data from Arduino, it will just execute an animation on the heart image to simulate heart pumping and then draw a signal on the chart. For each 20 consecutive heart pulse, Arduino will send the computed heart rate value to the Android device and we just need to update the value of our TextView via setText call.

comments powered by Disqus