Not understanding a part of the code

Hello Community!!

I'm working on a project that is aiming for getting a signal related the heartbeat from a pulse sensor. Basically, my circuit contains 10 LEDs. And those LEDs will light up when the heart bumps. But I don't know how sometimes just half of the LEDs will light up. (I'm a newbie so please be patient. ) Since I'm a new user, I dont' have the right (I think) to upload the scematic of the circuit to have a general idea.

What else can I provide in order to get some help ?

Ps : I tried to use code tags to make the code easy to read, I don't know if It's working or not.


```
/*
*/
#define USE_ARDUINO_INTERRUPTS false
#include <PulseSensorPlayground.h>

/*
   The format of our output.

   Set this to PROCESSING_VISUALIZER if you're going to run
    the Processing Visualizer Sketch.
    See https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer

   Set this to SERIAL_PLOTTER if you're going to run
    the Arduino IDE's Serial Plotter.
*/
const int OUTPUT_TYPE = SERIAL_PLOTTER;

const int PULSE_INPUT = A0;  //PIN A0 is on Signal of Pulse sensor
const int PULSE_BLINK = 13;    // Pin 13 is the on-board LED
const int THRESHOLD = 600;   // A ajuster en fonction de votre montage
/*
   samplesUntilReport = the number of samples remaining to read
   until we want to report a sample over the serial connection.

   We want to report a sample value over the serial port
   only once every 20 milliseconds (10 samples) to avoid
   doing Serial output faster than the Arduino can send.
*/
byte samplesUntilReport;
const byte SAMPLES_PER_SERIAL_SAMPLE = 10;

/*
   All the PulseSensor Playground functions.
*/
PulseSensorPlayground pulseSensor;


/*LEDS
*/
int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};       // an array of pin numbers to which LEDs are attached
int pinCount = 10;

//Minimum heart rate value to display on the LEDs
const byte bpmMin = 40;
const byte bpmMax = 110;

//SETUP
void setup() {

  Serial.begin(115200);

  // Configure the PulseSensor manager.
  pulseSensor.analogInput(PULSE_INPUT);
  pulseSensor.blinkOnPulse(PULSE_BLINK);
  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);

  //LEDS SETUP in Output
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }

  // Now that everything is ready, start reading the PulseSensor signal.
  if (!pulseSensor.begin()) {//FAILED
    /*
       PulseSensor initialization failed,
       likely because our Arduino platform interrupts
       aren't supported yet.

       If your Sketch hangs here, try changing USE_PS_INTERRUPT to false.
    */
    for (;;) {
      // Flash the led to show things didn't work.
      digitalWrite(PULSE_BLINK, LOW);
      delay(50);
      digitalWrite(PULSE_BLINK, HIGH);
      delay(50);
    }
  }

}

void loop() {

  /*
     See if a sample is ready from the PulseSensor.

     If USE_INTERRUPTS is true, the PulseSensor Playground
     will automatically read and process samples from
     the PulseSensor.

     If USE_INTERRUPTS is false, this call to sawNewSample()
     will, if enough time has passed, read and process a
     sample (analog voltage) from the PulseSensor.
  */
  if (pulseSensor.sawNewSample()) {
    /*
       Every so often, send the latest Sample.
       We don't print every sample, because our baud rate
       won't support that much I/O.
    */
    if (--samplesUntilReport == (byte) 0) {
      samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;

      pulseSensor.outputSample();

      /*
         At about the beginning of every heartbeat,
         report the heart rate and inter-beat-interval.
      */
      if (pulseSensor.sawStartOfBeat()) {
        pulseSensor.outputBeat();
        int myBPM = pulseSensor.getBeatsPerMinute();

        Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
        Serial.print("BPM: ");                        // Print phrase "BPM: "
        Serial.println(myBPM);

        if ( myBPM >= bpmMin && myBPM <= bpmMax ) {
          int purcent  = map(myBPM, bpmMin , bpmMax, 0, 100);
          lightLed100(purcent);
          delay(100);
          lightLed100(0);
        } else {
          lightLed100(0);
        }
      }
    }
  }

  /******
     Don't add code here, because it could slow the sampling
     from the PulseSensor.
  ******/
}


//Led on in purcentage
void lightLed100(int pourcent) {
  //put to LOW first
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    digitalWrite(ledPins[thisPin], LOW);
  }

  //PINS TO HIGH

  int pinsToHigh  = map(pourcent, 0 , 100, 0, pinCount);

  for (int thisPin = 0; thisPin < pinsToHigh; thisPin++) {
    digitalWrite(ledPins[thisPin], HIGH);
  }

}
```

As far as I can tell, faster heart rate lights up more LED's: 40 bpm = 1 LED and 110 bpm = all LED's.

In setup() this for loop.

How does the program exit that loop?

It does not and it should not since it is used to display a failed state of the device.

if pin 13 started to light up that means I'm dealing with a problem therefore the other LEDs will not light up?

Got to realize how fast .050 seconds is? How well do you see a blip of .05 seconds?

What happens when

for (;;) {
      // Flash the led to show things didn't work.
      digitalWrite(PULSE_BLINK, LOW);
      delay(100);
      digitalWrite(PULSE_BLINK, HIGH);
      delay(100);
    }

?

or

for (;;) {
      // Flash the led to show things didn't work.
      digitalWrite(PULSE_BLINK, LOW);
      delay(200);
      digitalWrite(PULSE_BLINK, HIGH);
      delay(200);
    }

?

Thanks for your reply!! Do you think I need to modify it ? just forgive me, I'm not very familiar with coding.

Pin 13 is LED_BUILTIN on UNO/Nano boards. So if the on-board LED flashes, the pulse sensor does not work. If not using UNO/Nano, you could change "PULSE_BLINK" to "2" in order to flash one of the heart rate LED's really fast instead.

1 Like

Thank you so much for your precious time !!

@Danois90 what do you think about the delay time, do I need to adjust it ?

I don't use delay().

@Idahowalker Thanks! So, in this case the led 13 will stay on if there is a problem with the pulse sensor ?

Code it however you wish. Use delay() if you wish. It's your code and your project.

Take a look at File|Examples|02.Digital|BlinkWithoutDelay

1 Like

Thanks for your help!! :grinning:

@Idahowalker
While I certainly recommend learning the BlinkWithoutDelay technique, In this particular case it is of no value whatsoever since it is inside a blocking forever loop. There are plenty of times when delay is appropriate such as this one.

@imane_tech
increase the delay if you would rather that led13 blinks instead of staying on otherwise it's fine.

1 Like

@Hutkikz Thanks !!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.