Problems when I use Event Groups

Hello everyone. I have a problem when I use event groups in my aplication and the problem is about when I need to receive values of my oximeter. When I use only tasks, I can to receive the values, but when I use task together with event groups, I can’t.
My code below. The expected behavior is return pox.getSpO2() values. But when I use events, my code on vTask4 only print the value 0.

#include <Wire.h>
#include "MAX30100_PulseOximeter.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"

#define TASK3_FLAG (1<<0)
#define TASK4_FLAG (1<<1)

void vTask3(void *pvParameters);
TaskHandle_t task3Handle = NULL;

void vTask4(void *pvParameters);
TaskHandle_t task4Handle = NULL;

void callBackTimer1(TimerHandle_t pxTimer);
TimerHandle_t xTimer;
EventGroupHandle_t xEvents;

int marker = -1;
 
#define REPORTING_PERIOD_MS 10
 
PulseOximeter pox;
 
uint32_t tsLastReport = 0;
 
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
  Serial.println("Beat!");
}
 
void setup()
{
  Serial.begin(115200);
   
  Serial.print("Initializing pulse oximeter..");
 
  // Initialize the PulseOximeter instance
  // Failures are generally due to an improper I2C wiring, missing power supply
  // or wrong target chip
  if (!pox.begin()) 
  {
    Serial.println("FAILED");
    for(;;);
  } else {
    Serial.println("SUCCESS");
  }
   
  // The default current for the IR LED is 50mA and it could be changed
  // by uncommenting the following line. Check MAX30100_Registers.h for all the
  // available options.
  // pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
   
  // Register a callback for the beat detection
  pox.setOnBeatDetectedCallback(onBeatDetected);

  xEvents = xEventGroupCreate();
  xTimer = xTimerCreate("TIMER1",pdMS_TO_TICKS(10),pdTRUE,0,callBackTimer1);
 xTaskCreatePinnedToCore(vTask3,"TASK3",configMINIMAL_STACK_SIZE+4096,NULL,5,&task3Handle, APP_CPU_NUM);
  xTaskCreatePinnedToCore(vTask4,"TASK4",configMINIMAL_STACK_SIZE+4096,NULL,5,&task4Handle, APP_CPU_NUM);  

  xTimerStart(xTimer, 0);
}
 
void loop()
{
  vTaskDelete(NULL);
}

void vTask3(void *pvParameters)
{
  (void) pvParameters;

  for(;;)
  {
    xEventGroupWaitBits(xEvents, TASK3_FLAG, pdTRUE, pdTRUE, portMAX_DELAY);
    Serial.println("Wait!");
    vTaskDelay(pdMS_TO_TICKS(1000));     
    
  }

}

void vTask4(void *pvParameters)
{
  (void) pvParameters;

  for(;;)
  {
    xEventGroupWaitBits(xEvents, TASK4_FLAG, pdTRUE, pdTRUE, portMAX_DELAY);
    pox.update();
    Serial.print(pox.getSpO2());
    Serial.println("%");    
    delay(100);     

  }

}

void callBackTimer1(TimerHandle_t pxTimer)
{
  marker++;
  if (marker == 0)
  {
    xEventGroupSetBits(xEvents, TASK3_FLAG);
  }
  else if (marker >= 500 && marker < 3500)
  {
    xEventGroupSetBits(xEvents, TASK4_FLAG);
  }
  else if (marker == 3600)
  {
    marker = -1;
  }
  
}

Would you please elaborate about what you mean that your code works when you do not use event groups? Are you saying that if you remove the following line from task 4, it works:

 xEventGroupWaitBits(xEvents, TASK4_FLAG, pdTRUE, pdTRUE, portMAX_DELAY);

Also, what are you trying to achieve? Do you want to just call the pox.getSpO2() at some regular interval? If so, can you not do it all in one task (something like the following):

uint32_t run=0;

for(;;)
{
    vTaskDelay(pdMS_TO_TICKS(10));
    run++;
    if( run == 500 )
    {
         /* Call getSPO2. */
         run = 0;
    }
    else
    {
        /* Just wait. */
    }

Thanks…

My answer to this question is yes. I only want to call pox.getSpO2() inside of the vTask4 at a some regular interval.

And here, you got it right. when I eliminate all things about events and timers, my application can to return the right values about my oximeter. Ex: when I put my finger, my oximeter returns via Serial.print(pox.getSpO2()) a value between 0% to 100%.

What I do if I want to return the “pox.getSpO2()” using event groups?

Thanks.

I am not familiar with pox.getSpO2() and therefore, it is hard to guess. Can you step through the function in both the success and failure case and try to see the difference?

Thanks.