KY039 Arduino Heart rate code

Hello,

I just got one of those, and i was able to (sort of) make a led blink with the same frequency of my heart.

As you can see at the image below, in a heart beat there is more than one area of "no pressure variation". Consider this while you coding.

Do not press your finger too tight, or the sensor wont catch the blood pressure change.

Using the code below, the led will light when there is no pressure variation at the sensor.

int ledc = 10;  
int sensorPin = 0;
int period = 100;
int change, oldvalue, value;
void setup ()
{
  pinMode(ledc, OUTPUT);
  Serial.begin (9600);
}
void loop ()
{
  value = analogRead (sensorPin);
  change = abs(oldvalue - value);
  if(change >= 2 )
  {
    Serial.println (change);
    analogWrite(ledc, 0); 
  }
  else
  { 
    analogWrite(ledc,255); 
  }
  oldvalue = value;
  delay (period);
}