Heartbeat sensor programming help.

Dear all, i have a small heartbeat detector with a signal pin and a g and v pin. its all connected up and is printing the info to the serial monitor. But what i want to do is add a bit of code to this "see below" so everytime the value is above 1000 which is what it reads when it dosent detect a heart beat, i want the led to light up. but when its reading the value 1000 i want it to turn off.

nt ledPin = 13;

int sensorPin = 0;



double alpha = 0.75;

int period = 20;

double change = 0.0;



void setup()                   

{

  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);

}



void loop()                    

{

    static double oldValue = 0;

    static double oldChange = 0;

    int rawValue = analogRead(sensorPin);

    double value = alpha * oldValue + (1 - alpha) * rawValue;



    Serial.print(rawValue);

    Serial.print(",");

    Serial.println(value);



    oldValue = value;

    delay(period);

}

would be great to get a bit of code to do that. so everytime it reads and prints the value and its above 1000 the led will turn on, when it reads 1000 led turn off.

Best regards

Lewis

It’s as simple as:

int ledPin = 13;
int sensorPin = 0;

double alpha = 0.75;
int period = 20;
double change = 0.0;

void setup()                   
{
  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);
}

void loop()                    
{

    static double oldValue = 0;
    static double oldChange = 0;

    int rawValue = analogRead(sensorPin);
    double value = alpha * oldValue + (1 - alpha) * rawValue;

    if(value<1000){
       Serial.print(rawValue);
       Serial.print(",");
       Serial.println(value);
       digitalWrite(ledPin,LOW);
    }
    else{
       digitalWrite(ledPin,HIGH);
    }

    oldValue = value;
    delay(period);
}

You may want to change things a bit to change what you do with oldValue when value>1000, and you may not want that delay outside of the if statement, but these are things for you to decide.

Thank you for your help. But now i try to run the code and it reads this in the serial monitor and the loop stop after about 2seconds

1023,255.75
1023,447.56
1023,591.42
1023,699.32
1023,780.24
1023,840.93
1023,886.45
1023,920.58
1023,946.19
1023,965.39
1023,979.79
1023,990.60
1023,998.70

before it was const writing 1000 until i put my finger on the sensor, now it just dosent work. Any ideas?

Kind Regards

Lewis

Ok if i write if(value <1023) it will serial.print

but this is what happens in my serial monitor when i touch my finger on the heartbeat sensor?

1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1023,1023.00
1022,1022.75
1022,1022.56
1022,1022.42
1022,1022.32
1022,1022.24
1022,1022.18
1022,1022.13
1022,1022.10
1022,1022.08
1022,1022.06
1022,1022.04
1022,1022.03
1022,1022.02
1022,1022.02
1022,1022.01
1022,1022.01
1022,1022.01
1021,1021.76
1022,1021.82
1022,1021.86
1022,1021.90
1022,1021.92
1022,1021.94
1021,1021.71
1022,1021.78
1022,1021.83
1021,1021.63
1022,1021.72
1022,1021.79
1022,1021.84
1021,1021.63
1022,1021.72
1021,1021.54
1021,1021.41
1022,1021.56
1022,1021.67
1022,1021.75
1022,1021.81
1022,1021.86
1022,1021.89
1022,1021.92
1022,1021.94
1022,1021.96
1022,1021.97
1022,1021.97
1021,1021.73
1021,1021.55
1022,1021.66
1022,1021.75
1022,1021.81
1021,1021.61
1022,1021.71
1022,1021.78
1022,1021.83
1022,1021.88
1022,1021.91
1022,1021.93
1022,1021.95
1022,1021.96
1022,1021.97
1021,1021.73
1022,1021.80
1022,1021.85
1022,1021.89
1022,1021.91
1022,1021.94
1022,1021.95
1022,1021.96
1022,1021.97
1022,1021.98
1022,1021.98
1022,1021.99
1022,1021.99
1022,1021.99

Best Regards

Lewis

lewisjoyce:
Ok if i write if(value <1023) it will serial.print

but this is what happens in my serial monitor when i touch my finger on the heartbeat sensor?

Yes, what were you expecting it to do?

everytime it reads a pulse and displays it, turn the led on but inbetween pulses i want the led to turn off. But the information i get back from the serial monitor seems quite hard to decode.

This is when it is detecting my pulse. but i dont know how to distinquish a pulse from no pulse.

1022,1021.97
1022,1021.98
1022,1021.98
1022,1021.99
1022,1021.99
1022,1021.99
1022,1021.99
1022,1022.00
1022,1022.00
1021,1021.75
1022,1021.81
1022,1021.86
1022,1021.89
1022,1021.92
1022,1021.94
1022,1021.96
1022,1021.97
1021,1021.72
1022,1021.79
1022,1021.85
1022,1021.88
1022,1021.91
1022,1021.93
1022,1021.95
1022,1021.96
1021,1021.72
1022,1021.79
1022,1021.84
1022,1021.88
1022,1021.91
1021,1021.68
1022,1021.76
1022,1021.82
1021,1021.62
1022,1021.71
1021,1021.53
1022,1021.65
1021,1021.49
1021,1021.37
1022,1021.52
1022,1021.64
1022,1021.73
1021,1021.55
1022,1021.66
1022,1021.75
1022,1021.81
1022,1021.86
1022,1021.89
1021,1021.67
1022,1021.75
1022,1021.81
1021,1021.61
1022,1021.71
1022,1021.78
1022,1021.84
1021,1021.63
1021,1021.47
1021,1021.35
1021,1021.26
1021,1021.20
1022,1021.40
1022,1021.55
1021,1021.41
1021,1021.31
1021,1021.23
1022,1021.42
1021,1021.32
1021,1021.24
1022,1021.43
1022,1021.57
1021,1021.43
1021,1021.32
1021,1021.24
1021,1021.18
1021,1021.14
1021,1021.10
1022,1021.33
1022,1021.49
1022,1021.62
1022,1021.72
1022,1021.79
1022,1021.84
1022,1021.88
1021,1021.66
1021,1021.50
1022,1021.62
1022,1021.72
1022,1021.79
1022,1021.84
1022,1022.10

Regards

Lewis

There are not pulses there, look at the data, it's pretty much a constant value of 1022?