Function taking too long? Blocking out acceleromter?

Hi there!

My script is attached. I am having problems counting steps when Bluetooth is connected using my device. I can confirm the hardware is fine for both. I would post a picture but there are many wires on one breadboard.

Whenever I include the following part of my script (lines 391-447), my MPU6050 acceleromter, I am unable to process accelerometer data but can still process heart rate data!

The part I remove is:

char input[100];
  char ending[] = "\n\r";
  char lead[] = "RCV=";
  char type[] = "DTCM";
  
  // Read BLE input from UART
  while(Serial1.available() > 0)
  {
    char i = (char)Serial1.read();
    memset(input+strlen(input), i, 1);
    memset(input+strlen(input)+1, '\0', 1);
    memset(&i, 0, 1);
  }

  // Check for leading RCV= and ending \n\r
  if(strcmp(input+(strlen(input)-2), ending) == 0)
  {
    if(strncmp(input, lead, 4) == 0)
    {
      // Remove leading RCV= and ending \n\r
      for(int i = 0+0; i <= (strlen(input)-4); i++)
      {
        input[i] = input[i+4];
      }
      memset(input+(strlen(input)-2), '\0', 1);

      if(strncmp(input, type, 1) == 0)
        strcpy(dateBT, input+2);
      else if(strncmp(input, type+1, 1) == 0)
        strcpy(timeBT, input+2);
      else if(strncmp(input, type+2, 1) == 0)
        strcpy(callBT, input+2);
      else if(strncmp(input, type+3, 1) == 0)
        strcpy(textBT, input+2);

      isBTConnected = true;
    
      // update display
      u8g2.firstPage();
      do {
        updateDisplay();
      } while ( u8g2.nextPage() );
  

      
      // update the time at which Bluetooth was last connected
      btLastConnected = millis();
    
      // Reset input buffer
      memset(input, 0, strlen(input));
    }
    else
    {
      // Reset input buffer
      memset(input, 0, strlen(input));
    }
  }

Thanks for your help!

Esperto_FW_BLE.ino (18.7 KB)

Not familiar with u2g, but updating screen is time consuming. You currently update all pages so the below could be a first step

void loop()
{
  // Check for leading RCV= and ending \n\r
  if(strcmp(input+(strlen(input)-2), ending) == 0)
  {
    if(strncmp(input, lead, 4) == 0)
    {
      ...
      ...      
      // update display
      u8g2.firstPage();
      do {
        updateDisplay();
      } while ( u8g2.nextPage() );

Instead, you can update a single page every time you go through loop().

The below is to give you the idea and is purely based on the above; you might have to move the updateDisplay to another position in loop an possibly keep an additional flag.

void loop()
{
  static bool hasNextPage = false;
  
  // Check for leading RCV= and ending \n\r
  if(strcmp(input+(strlen(input)-2), ending) == 0)
  {
    if(strncmp(input, lead, 4) == 0)
    {
      ...
      ...      
      // update display
      
      if(hasNextPage == false)
      {
        u8g2.firstPage();
        hasNextPage = true;
      }

      updateDisplay();
      hasNextPage = u8g2.nextPage();

Hi sterretje,

thanks for the help.

So you think the display is taking too long? I can try out your recommendation.

But do you know why the heart rate would still be working? If the display is taking too long, would it not affect all the systems?

Thanks

Probably yes, but I did not look too close at your code; it was what stood out when I looked at snippet before looking at the attached code.

You can always embed the do/while in a time measurement and print the duration to e.g. a serial port or on the screen.