HC-06 Bluetooth low latency problems

Hello,

I’ve been using the HC-06 Bluetooth module with an Arduino Nano to send over an unsigned long and a char as much as possible every second to an android game, made in Unity game engine.

Communication is ok, but the latency is unbearable. I’m trying to send over the data from a gyroscope and accelerometer (to track 3D rotation) and apply that into my game engine in real time.

The code I’m using is here:

void loop() {

    // if programming failed, don't try to do anything
    if (!dmpReady) return;

    // wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt && fifoCount < packetSize) {
    }

    // reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

    // get current FIFO count
    fifoCount = mpu.getFIFOCount();

    // check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        // reset so we can continue cleanly
        mpu.resetFIFO();
        //Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

        // read a packet from FIFO
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;

        mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
        
       x = (ypr[0] * 180 / M_PI);
       if( x < 0) x += 360;

       y = (ypr[1] * 180 / M_PI);
       if( y < 0) y += 360;
       
       z = (ypr[2] * 180 / M_PI);
       if( z < 0) z += 360;

        packedInfo = ((uint32_t)x << 21 |
                     (uint32_t)y << 12 |
                     (uint32_t)z << 3 |
                    (digitalRead(5) == LOW ? 1 : 0) << 2 |
                    (digitalRead(6) == LOW ? 1 : 0) << 1 |
                    (digitalRead(7) == LOW ? 1 : 0));
                    
        
        BT.print(packedInfo);
        BT.print('%');
       }
}

I’m using a SoftwareSerial for the HC-06 initialized as “BT”, at a baudrate of 4800. I’ve tried all the baudrates ranging from 1200 to 115200, but the higher the baudrate, the smoother the dataflow, but the latency also increases. for example:

  • 1200 baudrate: Rotation update once every second, but the data was fast delivered, more real time than a higher baudrate
  • 115200 baudrate: Rotation update felt like 60 frames per second, but had atleast a 3 second delay

I also decreased the sending data for one rotation (and 3 buttons) from 17 bytes (in string format) to 4 bytes using bitshifting and storing everything in an unsigned float. But the delay is still too much.

Can I increase this delay somehow? I would need at least 15 ish updates a second as fast as possible to get this to work.