I am trying to use the servo motor with heart rate sensor but the latter outputs values even when I am not touching the sensor.
The code:
#include <Servo.h>
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library
const int PulseWire = 0; // 'S' Signal pin connected to A0
const int LED13 = 13; // The on-board Arduino LED
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore
int distance;
PulseSensorPlayground pulseSensor; // Creates an object
Servo servo;
long duration;
void setup() {
Serial.begin(9600);
servo.attach(12);
servo.write(0);
// Configure the PulseSensor object, by assigning our variables to it
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); // Blink on-board LED with heartbeat
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and began seeing a signal
if (pulseSensor.begin()) {
Serial.println("PulseSensor object created!");
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calculates BPM
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if a beat happened
Serial.println("♥ A HeartBeat Happened ! "); // If true, print a message
Serial.print("BPM: ");
Serial.println(myBPM); // Print the BPM value
}
delay(20);
for (int i=0; i<180; i++){
servo.write(i);
//usdist();
delay(5);
}
for (int i=180; i>0; i--){
servo.write(i);
//usdist();
delay(5);
}
}
When I test the heart rate sensor separately, it works fine that is only when I place it on my skin