0

Hello Arduino Community,

I am encountering an issue trying to retrieve a heart rate reading from an op-amp and instrumentation amplifier and sending it via Bluetooth. I used this website as a basis of my design: http://www.instructables.com/id/Electrocardiograph-Heart-Rate-Monitor/?ALLSTEPS. I would appreciate it if you could take a look at that. Here are a couple of pictures of my design.
1 2 3 Here is the code that I used:

    const int  signal = 7;    // Pin connected to the filtered signal from the circuit
    unsigned long time;   
    unsigned long frequency;

    // Internal variables
    int period = 2000;
    int starttime = 2000;
    int input = 0;
    int lastinput = 0;
    unsigned long death = 0;

         void setup() {
           pinMode(signal, INPUT);
           Serial.begin(9600);
         }

     void loop() {
       delay(1000);

       time = millis();
       input = digitalRead(signal);

       if ((input != lastinput)&&(input == HIGH)) {
       // If the pin state has just changed from low to high (edge detector)
       period = time - starttime; // Compute the time between the previous beat and the one that has just been detected
       starttime = time; // Define the new time reference for the next period computing
       death = time;
      }
      lastinput = input; // Save the current pin state for comparison at the next loop iteration

      if (period < 0) {
      frequency = 0;
      }
      else {
      frequency = 60000/period; // Compute the heart rate in beats per minute (bpm) with the period in milliseconds
      }


      if ((time - death) > 2000) { // Detect if there is no beat after more than 2 seconds
      Serial.println("dead");
      }

      else {
      char freq[3];

      if (frequency/100 == 0) {
       freq[0] = 32; // Print a space to the first character if the frequency is below 100 bpm
      } 
      else {
       freq[0] = frequency/100+48; // Sort the hundreds character and convert it in ASCII
      }
       freq[1] = (frequency/10)%10+48; // Sort the thents character and convert it in ASCII
       freq[2] = frequency%10+48; // Sort the units character and convert it in ASCII

      Serial.print(freq);
      Serial.print("alive! ");
      }

      }`

But all I'm getting as output is 'dead'. I am really starting to lose it here since I am a complete amateur with Arduino and circuitry. For reference, I copied the design from this image from the website (this is just an enlarged version): http://www.instructables.com/file/FVBC6TYHOW0HQ7H/?size=ORIGINAL. I would appreciate it if anyone could provide some extra guidance.

  • This question (at least the wiring) may be better suited for an EE.SE post, it involves circuitry. – TheDoctor Dec 10 '14 at 15:16
  • As far as I see, you amplified the electr. signal comming from your heart and read the coresponding voltage (hopefully cliped at 5V ;-) ) from pin seven of your arduino. But in your loop you wait for a second then check the pin 7 if it has been changed. This checking lasts a few microseconds so the probabillity that "the heart beats" at that moment is low. It would be better to use interrupts for this purpose to react on every heartbeat. Or at least: avoid any delay where possible. Another finding: Use if (period <= 0) in your program to avoid divide by zero in the else case. – Peter Paul Kiefer Nov 20 '15 at 15:01
1

enter image description hereassuming your circuit works, your code has some serious issues. I rewrote the code for you and also provided a simplified circuit that I have tested and know works. Instead of getting the pulse from the heartbeat detector circuit, we will simulate it on the Arduino by pulsing pin 2 periodically.

const int  signal = 7;    // Pin connected to the filtered signal from the circuit
unsigned long currentBeatTime;   
unsigned long previousBeatTime;

unsigned long frequency;

unsigned long myBeatFreq;
unsigned long myBeat;

// Internal variables
unsigned long period = 0;
int input = 0;
int lastinput = 0;


void setup() {
    pinMode(signal, INPUT);
    pinMode(2, OUTPUT);
    Serial.begin(9600);

    previousBeatTime = millis();
    myBeat = previousBeatTime;
}

void loop() {
    if ( millis()-myBeat > myBeatFreq ) 
    {
      digitalWrite(2, HIGH);
      myBeat=millis();
    }
    else
    {
      digitalWrite(2, LOW);
    }

    input = digitalRead(signal);

    if ((input != lastinput) && (input == HIGH)) {
        // If the pin state has just changed from low to high (edge detector)
        currentBeatTime = millis();

        period = currentBeatTime - previousBeatTime; // Compute the time between the previous beat and the one that has just been detected
        previousBeatTime = currentBeatTime; // Define the new time reference for the next period computing
    }

    lastinput = input; // Save the current pin state for comparison at the next loop iteration

    // Detect if there is no beat after more than 2 seconds
    if ( (millis() - previousBeatTime) > 2000 ) 
    { 
        Serial.println("dead");
    }
    else 
    {
        if (period <= 0) 
        {
            frequency = 0;
        }
        else 
        {
            frequency = 60000/period; // Compute the heart rate in beats per minute (bpm) with the period in milliseconds
        }

        Serial.print(frequency);
        Serial.println(" : alive! ");
    }
}
  • It compiles OK! – Mark Williams Dec 11 '14 at 19:06
  • Hey, sorry, late post. I'll see if it works for me and get back to you! Thanks! – user3744439 Dec 14 '14 at 22:00
  • Hey, I tried it, but it I still received "dead" as my output. Is there something wrong with my circuitry? – user3744439 Dec 21 '14 at 19:57
  • I can't see your images at the moment (blocked from work), but if it says "dead" after two seconds that means signal is never going high. To be sure, connect a wire to pin 7. Start the Arduino and then connect the wire to 5V and then disconnect it repeatedly to simulate a proper heartbeat signal. If it works, you'll know the code is working and something else is wrong with your circuit or the heartbeat detector. – imjosh Dec 22 '14 at 16:03
  • @imjosh Hey I just did that with a battery and found out it still says 'dead'. I'm pretty sure it's the circuitry, but I don't know how to fix it. – user3744439 Dec 24 '14 at 2:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.