need to find the difference between the BPM before and after

I am just a beginner in arduino and I haven't studied the code that I downloaded from the Internet.

So, I would like to ask the time 2 ms is for what operation? Is it for the inter-beat interval?

Moreover, my pulse sensor keeps on detecting the pulse signal even my finger did not put on it. Can I use the code to make it work only when I put finger on it? How?

By the way, thank you all for pointing out my mistakes.

So, I would like to ask the time 2 ms is for what operation?

It is how often to look at the incoming signal, to be able to spot a peak.