Help with defining digital pins

Hey, this is my first arduino project and I'm a complete beginner.
I'm using a heart sensor that will send the heart rate to the arduino to then send it to a HC-05 module and finally the module will resend the data to my phone's bluetooth serial app.

I'm using an Arduino Nano, the heartrate sensor output is placed in D2.
D9 and D8 are my serial communication with the HC05 RX and TX, this communication is working fine.
Finally I have a LED on D13.

All I want to do is send the results from D2 to my phone, however, I'm having issues. The LED on D13 keeps blinking on and off with no synchronization with D2 and everytime it blinks I receive a response line in my phone. I would like to get the LED to blink at the same time it receives a heart signal from D2 and inmediatly get a response on my phone's serial monitor with every blink.

Thanks for your help :slight_smile:

Here's my code, and the attachment is the full code I found online:

#include <SoftwareSerial.h>// import the serial library
#include "DFRobot_Heartrate.h"
#define heartratePin D2
const byte D2 = 2;
DFRobot_Heartrate heartrate(DIGITAL_MODE);

SoftwareSerial Genotronex(8, 9); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer

//  Variables
int pulsePin = 2;                 // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 13;                // pin to blink led at each beat
int fadePin = 5;                  // pin to do fancy classy fading blink at each beat
int fadeRate = 2;                 // used to fade LED on with PWM on fadePin

// Volatile Variables, used in the interrupt service routine!
volatile int BPM;                   // int that holds raw Analog in 0. updated every 2mS
volatile int Signal;                // holds the incoming raw data
volatile int IBI = 600;             // int that holds the time interval between beats! Must be seeded! 
volatile boolean Pulse = false;     // "True" when User's live heartbeat is detected. "False" when not a "live beat". 
volatile boolean QS = false;        // becomes true when Arduoino finds a beat.

// Regards Serial OutPut  -- Set This Up to your needs
static boolean serialVisual = true;   // Set to 'false' by Default.  Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse 


void setup(){
  pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
  Serial.begin(115200);             // we agree to talk fast!
  interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS 
   // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE, 
   // AND APPLY THAT VOLTAGE TO THE A-REF PIN
//   analogReference(EXTERNAL);   
Genotronex.begin(9600);
  Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
  pinMode(ledpin,OUTPUT);
}


//  Where the Magic Happens
void loop(){
  
    serialOutput() ;       
    
  if (QS == true){     //  A Heartbeat Was Found
                       // BPM and IBI have been Determined
                       // Quantified Self "QS" true when arduino finds a heartbeat
        digitalWrite(blinkPin,HIGH);     // Blink LED, we got a beat. 
        fadeRate = 255;         // Makes the LED Fade Effect Happen
                                // Set 'fadeRate' Variable to 255 to fade LED with pulse
        serialOutputWhenBeatHappens();   // A Beat Happened, Output that to serial.     
        QS = false;                      // reset the Quantified Self flag for next time    
       } 
      else { 

      digitalWrite(blinkPin,LOW);            // There is not beat, turn off pin 13 LED
      }
     
   ledFadeToBeat();                      // Makes the LED Fade Effect Happen 
  if (Genotronex.available()){
BluetoothData=Genotronex.read();
   if(BluetoothData=='1'){   // if number 1 pressed ....
   digitalWrite(ledpin,1);
   Genotronex.println("LED  On D12 ON ! ");
   }
  if (BluetoothData=='0'){// if number 0 pressed ....
  digitalWrite(ledpin,0);
   Genotronex.println("LED  On D12 Off ! ");
  }
}
  delay(20);                             //  take a break
}

FRAW610IC5CA2WA.zip (6.54 KB)

i see D2 defined in the code you posted between the code tags, but I don't see it used anywhere in that code or the code in the .zip file

PulseSensor_wt_BT.ino from the zip and the code from the code tags both have setup() and loop() but don't look the same

Hey and thanks for your help!!

the zip file is the original code from the tutorial, and the code I posted has some changes because I'm using D2 to receive the output from the sensor, the original code uses A0 instead of D2, so that's basically the biggest change in my code. However, seems like the Arduino isn't resending the output from D2 to the HC05, all I'm getting in my phone is random heartbeats, but they're not in sync with the sensor.

again, i don't see where D2/heartratePin are used in the code you posted