Does this simplified flowchart describe my code?

Hello Community !!

My project is about doing a simulation of a circuit in order to display a signal related to my pulse sensor. Actually, I'm not quite familiar with coding using the C++ language, I tried to make a flowchart that describes what is the code doing in general. So what Do you think ? Did I neglect some important parts ?

(I'm a newbie, so please just be patient!!)

What else can I provide to get some help ?

ORGANIGRAM


```
/*
*/
#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(100);
      digitalWrite(PULSE_BLINK, HIGH);
      delay(100);
    }
  }

}

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);
  }

}
```

Is this correct ?

The code is yours, the flowchart is yours, you want us to say if the flowchart agrees with the code :thinking: .

Does anyone still use flowcharts?

@LarryD I found the code on Github, and I need it for my project but I'm not really familiar with C++ language, That's why I don't know If I really undestood what the code is already doing !!

изображение

1 Like

Thanks for your help, @kolaha !!

so, what do you need really?

What is "validated code" and how do you know it's valid?

How do you know the pulse sensor doesn't work? And shouldn't you flash the led AFTER you have determined it doesn't work.

when is PPG aquired?

Does your program actually End? or does it loop forever.

A flowchart should show every decision the program makes and its effect.

Thanks for your reply, @Hutkikz ! This is the first time I make a flowchart, I did my research but it seems that I'm still struggling with understanding the code. Any advice, please!!

I meant with validated code that if the code is running correctly without any problem, as a result I could display the signal on the serial plotter which is my main goal.

Maybe, when the pin 13 will light up that means that the pulse sensor is not actually working.
For your last question, I have no clue, since I didn't really get that code.

I'm Sorry but I really need your help!

@kolaha I think I need to split the code into parts and try all my best to understand it!!

I did one for your setup() function to get you started:

HINT: The loop() function never ends so your flow chart should come back around to the top of the loop() function instead of ending

1 Like

Is this a school assignment where you are given a flowchart and asked to code it?

wally02's keen on them.

Hey @anon57585045, thanks for your reply!! Actually, I've got a code and I need to transform it to a flowchart.

Does it have to be a flowchart, or is it ok to do a state diagram?

Hey @octopirate !! I think that making a flowchart is easier that making a state diagram for the code. What do you think ?

Thanks you so much for your help @Hutkikz !!

I haven't looked at the code in any detail, but I'm a big fan of state diagrams which would be my first port-of-call.

What do you think about Foster Wheeler - a British Company -- are they not using Flow Chart to describe the whole instrumentation process (of a giant Fertilizer Factory) in the Control Room for quick detection of a leakage point?

A Flow Chart is still a powerful tool for general programmers to describe the solution of a problem in visual form using only seven pre-defined geometrical figures? A Control Structure is an alternative form which requires different intelligent to understand.

Okay, is it a school assignment where you are asked to write code?