Using millis to measure intervals to trigger a digital output

You are using an analog read so you must have a value which is the ceiling of when the heart pulses and a low value for when it’s in between the pulse.

I think you should start your if statement with the value of the lower range and within that put a millis time reset. Put the next if inside of that if statement for the if millis is greater than the interval so you can change the state of the output pin which triggers the shock when the interval of 500 is hit to high.

Use and else if saying the upper range that corresponds to a beat and make that set the output state to low. If the heat beats quick enough the state will always be low and f it’s longer then your set interval then it shocks.

On the top of the loop digital write the shock pin to the output state outside and separate from the if statements.

I would reference the blink without delay to understand how to write the states. Only in your case your using an else if rather then an else.