AD8232 heart monitor: calculating BPM

Hey! For a project in school, I’m using the AD8232 heart rate sensor by sparkfun. I tried writing code myself for calculating the heart rate in BPM (the sensor just reads in an analog signal, identifying the electrical activity of the heart), but failed horribly doing this. So i decided to go online to look for code, doing this. I quickly came across the following script, made by viron11111 on Github.

unsigned int average_value[60];
int i = 0;

long lastDebounceTime = 0;
long debounceDelay = 300;

long counter = 0;

void setup() {
  // initialize the serial communication:
  pinMode(10, INPUT); // Setup for leads off detection LO +
  pinMode(3, INPUT); // Setup for leads off detection LO -

  attachInterrupt(0, right_Hand_Detect, FALLING);
  //attachInterrupt(1, left_Hand_Detect, FALLING);


void loop() {
  float sum = 0.0;
  float average = 0.0;
  if((digitalRead(10) == 1)||(digitalRead(11) == 1)){
    if ((millis() - lastDebounceTime) > debounceDelay){
      average_value[i] = analogRead(A0);
      if (i >= 30){
        for (int j = 0; j < 60; j++){
          sum = sum + average_value[j];
          if (average_value[j] > 370){
        average = sum/60.0;
        //Serial.print("The Average is: ");
        i = 0;
  //Wait for a bit to keep serial data from saturating

void right_Hand_Detect(){
  if(digitalRead(3) == 0){
    //Serial.println("Right hand");
    lastDebounceTime = millis();

/*void left_Hand_Detect(){
  Serial.println("Left hand");

This code seems to work, but i dont really understand how it works and how the interupts work. Could somebody help me with this?

Could somebody help me with this?

Without knowing what is connected to the external interrupt pin? No.

Are we also supposed to guess what it connected to pins 10, 11, and A0?