Servo and pulsesensor project

Hi!

Me and my friend need some help with the programming. (We are very new on this).

Our goal is to create a box that opens by a servo when the pulsesensor has measure the "maximal pulse" (that you can by you self decide in the serial monitor). The box will be open for a limit of time and then close again. Then you have to repet the process: Measure the BPM--> the box will open after the maximal pulse has acheved--> Box is open for limit of time and then it will close.

We have done a sketch, but we are getting error like:

-The servo is activ all the time and not depending on the pulsesensor.

Please help us :slight_smile:

Servo_and_Pulsesensor_project.ino (2 KB)

The OPs code for everyone's convenience
OPO, please note the code has been Auto Formatted in the IDE and posted using code tags to make it easy to scroll through and copy

#include <Servo.h>
#include <PulseSensorPlayground.h>
Servo luckan; // Vi definerar servon som obejkt för att kontrollera luckan.

long pos = 0; // Pos är variabel som tilldelas för att förklara luckans position.
const int PulseWire = A0;
int anvandignor = 550; // Vilka signaler den ska använda som beat och vilka den ska ignorera.
// int pulseSensor=A0;
PulseSensorPlayground pulseSensor;
int MaxPuls = 120; // Skriva in sin maxpuls (helst i seriall monitor)

void setup()
{
  Serial.begin(9600);
  pulseSensor.analogInput(PulseWire);
  if (pulseSensor.begin())
  {
    Serial.println("We created a pulseSensor Object !");
    luckan.attach(9);// Servon ska tillhöra pin 9.
  }
}

void loop()
{
  int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
  if (Serial.available() < 0)
  {
    pos = Serial.read();
  }
  if (pulseSensor.sawStartOfBeat())    // Testar ifall den får ett beat
  {
    delay(15000); // Väntar i 15 sekunder och sedan printa ut
    Serial.println("Kom igen, kämpa! "); // If test is "true", print a message "a heartbeat happened".
    Serial.print("BPM: ");                        // Print phrase "BPM: "
    Serial.println(myBPM);                        // Print the value inside of myBPM.
  }
  if (myBPM > MaxPuls) // Vi vill att den ska skriva att "om pulsmätaren kommer upp till 120"...
  {
    Serial.println("Yey, Let's get HIGH on candy! ");
    Serial.print("BPM: ");
    Serial.println(myBPM);
    for (pos = 10 ; pos <= 90; pos += 1)   // går från 0 grader till 120 grader.
    {
      // in steps of 1 degree
      luckan.write(pos);              // Säger till luckan att gå till den bestämda positionen.
      delay(800);                       // Väntar 800 ms
    }
    for (pos = 90; pos >= 90; pos -= 1)   // Går från 120 grader till 0.
    {
      luckan.write(pos);              // Säger till luckan att gå till den bestämda positionen.
      delay(20);       // Väntar 50 ms.
    }
  }
}