Go Down

Topic: Problems with the avarage loop (Read 173 times) previous topic - next topic

RokiRokus

Hello,

I m having a problem with my code and i thought if you can help me with my issue.

For my application I'm trying to get an average result with the code bellow.

Code: [Select]
void loop() {
  //return;
  //You have to call update with frequency at least 37Hz. But the closer you call it to 100Hz the better, the filter will work.
  pulseoxymeter_t result = pulseOxymeter->update();
 
   if( result.pulseDetected == true )
  {
    Serial.println("BEAT");
    /*
    Serial.print( "BPM: " );
    Serial.print( result.heartBPM );
    Serial.print( " | " );
 
    Serial.print( "SaO2: " );
    Serial.print( result.SaO2 );
    Serial.println( "%" );

    Serial.print("{P2|BPM|255,40,0|");
    Serial.print(result.heartBPM);
    Serial.print("|SaO2|0,0,255|");
    Serial.print(result.SaO2);
    Serial.println("}");
  */
 
   
    uint32_t SaO2 = result.SaO2 ;
    uint32_t BPM = result.heartBPM ;
   
    Serial.println
    Serial.print(BPM);
   // Serial.println(
    //Serial.print(SaO2);
 
    BEATS[i] = BPM;
   // SaO2AVG[i] = SaO2;
    if (i % 10 == 0 && i != 0) {
      for (int i = 0 ; i < 10 ; i++){
        sum += BEATS[i];
      }
   //     sum1 += SaO2AVG[i];

       Serial.println("AVERAGE BPM"); Serial.println(sum/10);
  //     Serial.println("AVERAGE SaO2"); Serial.println(sum1/10);
      BEATS[10] = {};
      sum = 0;
      i = 0;
    }
    i = i + 1;
  }
  delay(10);
 

}


But the problem is that when the average result is calculated it wont restart the counting.

Did any body have any idea ?

Thank you very much

DangerToMyself

#1
Sep 14, 2017, 01:59 am Last Edit: Sep 14, 2017, 12:41 pm by DangerToMyself
I'm curious as to what this "i" is
Code: [Select]
if (i % 10 == 0 && i != 0) {

Reason I'm asking is that it appears (to me, anyway) that you re-declare it as an int in the very next line.

You also have && i != 0. But one of the last lines sets i=0. So, if it's being set to 0 at the end, then !=0 is not possible. I'm no guru, so maybe there's more to it than what I'm seeing. Or think I'm seeing.

sterretje

Please post full code. As @DangerToMyself says, we have no idea what all your variables are.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

PaulS

Code: [Select]
      BEATS[10] = {};
What do you THINK this code is doing? It almost certainly is NOT.
The art of getting good answers lies in asking good questions.

RokiRokus


Here you can find the complete code with all the variables.

So i was using this average code on other projects and it works fine.

Now i don't understand why it wont work on this specific application.

Thank you again for the help


Code: [Select]
#include <Arduino.h>
#include <math.h>
#include <Wire.h>

#include "MAX30100.h"

MAX30100* pulseOxymeter;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("Pulse oxymeter test!");

  //pulseOxymeter = new MAX30100( DEFAULT_OPERATING_MODE, DEFAULT_SAMPLING_RATE, DEFAULT_LED_PULSE_WIDTH, DEFAULT_IR_LED_CURRENT, true, true );
  pulseOxymeter = new MAX30100();
  pinMode(2, OUTPUT);

  //pulseOxymeter->printRegisters();
}

float temp;
int i = 0;
uint32_t sum = 0;
uint32_t sum1 = 0;
uint32_t BEATS[10];
uint32_t SaO2AVG[10];

void loop() {
  //return;
  //You have to call update with frequency at least 37Hz. But the closer you call it to 100Hz the better, the filter will work.
  pulseoxymeter_t result = pulseOxymeter->update();
 
   if( result.pulseDetected == true )
  {
    Serial.println("BEAT");
    /*
    Serial.print( "BPM: " );
    Serial.print( result.heartBPM );
    Serial.print( " | " );
 
    Serial.print( "SaO2: " );
    Serial.print( result.SaO2 );
    Serial.println( "%" );

    Serial.print("{P2|BPM|255,40,0|");
    Serial.print(result.heartBPM);
    Serial.print("|SaO2|0,0,255|");
    Serial.print(result.SaO2);
    Serial.println("}");
  */
 
   
    uint32_t SaO2 = result.SaO2 ;
    uint32_t BPM = result.heartBPM ;
   
    Serial.println("BPM meritve na sekundo");
    Serial.print(BPM);
   // Serial.println("SaO2 meritve na sekundo");
    //Serial.print(SaO2);
 
    BEATS[i] = BPM;
   // SaO2AVG[i] = SaO2;
    if (i % 10 == 0 && i != 0) {
      for (int i = 0 ; i < 10 ; i++){
        sum += BEATS[i];
      }
   //     sum1 += SaO2AVG[i];

       Serial.println("AVERAGE BPM"); Serial.println(sum/10);
  //     Serial.println("AVERAGE SaO2"); Serial.println(sum1/10);
      BEATS[10] = {};
      sum = 0;
      i = 0;
    }
    i = i + 1;
  }
  delay(10);
 

}

PaulS

Code: [Select]
      BEATS[10] = {};
You still haven't explained what you think this does. There is no element 10 in a 10 element array. The elements are numbered 0 to 9.

You have not said what the code actually does. You have not said what you expect it to do. You have not explained how those two things are different.
The art of getting good answers lies in asking good questions.

gfvalvo

* You are defining another (local) variable called 'i' in the 'for' loop. It shadows the global 'i' for the scope of the loop. While technically not prohibited and doesn't affect your code (this time), it's a really bad idea.

* The test in your 'if' statement is screwing things up. Just let 'i' count from 0 to 9 for God's sake. Then run your loop to sum the values when 'i > 9' -- if that's the way you HAVE to do it, see next comment.

* Why are you bothering to store the values in 'BEATS[]' and then summing them? You don't use 'BEATS[]' anywhere else in the code. Why not just sum them in a variable on the fly? Go crazy and call it 'beatsSum'.

DangerToMyself

I'm curious as to what this "i" is
Code: [Select]
if (i % 10 == 0 && i != 0) {

Reason I'm asking is that it appears (to me, anyway) that you re-declare it as an int in the very next line.

You also have && i != 0. But one of the last lines sets i=0. So, if it's being set to 0 at the end, then !=0 is not possible. I'm no guru, so maybe there's more to it than what I'm seeing. Or think I'm seeing.
After really looking at the OPs code, I guess, after the first time through the loop, it would always start with i equaling 1. Right? I overlooked the "i=i+1;" before because I have always done  "i++;". The "if" statement sets i to 0 and then right after adds 1 to it.

And I completely overlooked the whole BEATS[10] = {}; thing. But I'm learning. I'm slow. But I'll catch up eventually :)


MorganS

I see magic numbers.

What does "10" mean here?
Quote
uint32_t BEATS[10];
Is it the same "10" as here?
Quote
uint32_t SaO2AVG[10];
It's certainly not the same "10" as here:
Quote
delay(10);
Give all these constants names. Then use the names everywhere. That will make it a thousand times easier to change your averaging period in the future, because it certainly will change.
"The problem is in the code you didn't post."

Go Up