How To Run two tasks simultaneously?


```cpp
#include <ArduinoBLE.h>
#include <math.h>

#define SINE_FREQ 33
#define SAMPLE_RATE 5000
#define MAX_PACKET_SIZE 244
#define FLOAT_SIZE 4
#define PACKET_NUM_SIZE 4
#define QUEUE_SIZE 1000
#define SAMPLES_PER_PACKET ((MAX_PACKET_SIZE - PACKET_NUM_SIZE) / FLOAT_SIZE)

BLEService sineWaveService("f3641400-00b0-4240-ba50-05ca45bf8abc");
BLECharacteristic sineWaveCharacteristic("f3641401-00b0-4240-ba50-05ca45bf8abc", BLENotify, MAX_PACKET_SIZE);
BLECharacteristic controlCharacteristic("f3641402-00b0-4240-ba50-05ca45bf8abc", BLEWrite, 6);

float valueQueue[QUEUE_SIZE];
uint32_t timestampQueue[QUEUE_SIZE];
int queueHead = 0, queueTail = 0;
bool queueFull = false;
volatile bool bleReady = true;
bool startSignalReceived = false;

uint32_t packetNumber = 0;
uint32_t startMillis = 0;
uint32_t lastPushTime = 0;
uint32_t lastPullTime = 0;
int sampleIndex = 0;

const uint32_t pushInterval = 5;
const uint32_t pullInterval = 65;

void setup() {
  Serial.begin(115200);

  if (!BLE.begin()) {
    Serial.println("Failed to initialize BLE!");
    while (1)
      ;
  }

  BLE.setLocalName("NiclaSense");
  BLE.setAdvertisedService(sineWaveService);
  sineWaveService.addCharacteristic(sineWaveCharacteristic);
  sineWaveService.addCharacteristic(controlCharacteristic);
  BLE.addService(sineWaveService);
  BLE.advertise();

  controlCharacteristic.setEventHandler(BLEWritten, onControlReceived);
  Serial.println("BLE Ready Waiting for START SIGNAL....");
}

void loop() {
  BLEDevice central = BLE.central();
  uint32_t currentMillis = millis();

  if ((currentMillis - lastPushTime) >= pushInterval) {
    lastPushTime = currentMillis;
    push();
  }
  //if(packetNumber==1){
  //for(int inx=0;inx<60;inx++){
  //Serial.print(" | Timestamp: " );
  //Serial.println(inx);
  //Serial.println(timestampQueue[inx]); }}

  if (central && startSignalReceived && bleReady && (currentMillis - lastPullTime >= pullInterval)) {
    lastPullTime = currentMillis;
    pull();
  }
}

void push() {
  if (!queueFull) {
    uint32_t timestamp = millis();
    //float time = (float)sampleIndex / SAMPLE_RATE;
    float time = (millis() - startMillis) / 1000.0;
    //Serial.println(time);
    float sineValue = sin(2 * M_PI * SINE_FREQ * time);


    valueQueue[queueTail] = sineValue;
    timestampQueue[queueTail] = timestamp;
    queueTail = (queueTail + 1) % QUEUE_SIZE;
    if (queueTail == queueHead) {
      queueFull = true;
    }

    sampleIndex++;
  }
}

void pull() {
  float values[SAMPLES_PER_PACKET] = { 0 };  // Clear to avoid leftover values
  uint32_t timestamps[SAMPLES_PER_PACKET] = { 0 };

  if (dequeueValues(values, timestamps, SAMPLES_PER_PACKET)) {
    bleReady = false;

    uint8_t packetBuffer[MAX_PACKET_SIZE];
    memcpy(packetBuffer, &packetNumber, PACKET_NUM_SIZE);
    memcpy(packetBuffer + PACKET_NUM_SIZE, values, SAMPLES_PER_PACKET * FLOAT_SIZE);

    Serial.print("[PULL] Packet #");
    Serial.print(packetNumber);
    Serial.print(" | Values: ");

    for (int i = 0; i < SAMPLES_PER_PACKET; i++) {
      Serial.print(values[i], 2);
      Serial.print(" ");
    }
    Serial.println();

    if (sineWaveCharacteristic.writeValue(packetBuffer, sizeof(packetBuffer))) {
      //Serial.println("[PULL] Packet Sent!");
      packetNumber++;
    }

    bleReady = true;
    //} else {
    //Serial.println("[PULL] Not enough samples in queue!");
  }
}

bool dequeueValues(float* outValues, uint32_t* outTimestamps, int count) {
  int available = (queueTail + QUEUE_SIZE - queueHead) % QUEUE_SIZE;
  if (queueFull) available = QUEUE_SIZE;

  if (available < count) return false;

  for (int i = 0; i < count; i++) {
    outValues[i] = valueQueue[queueHead];
    outTimestamps[i] = timestampQueue[queueHead] - startMillis;
    queueHead = (queueHead + 1) % QUEUE_SIZE;
    queueFull = false;
  }

  return true;
}

void onControlReceived(BLEDevice central, BLECharacteristic characteristic) {
  char receivedCommand[6] = { 0 };
  characteristic.readValue(receivedCommand, sizeof(receivedCommand));

  if (strcmp(receivedCommand, "START") == 0) {
    Serial.println("[BLE] START Signal Received");
    startMillis = millis();
    startSignalReceived = true;
    queueHead = queueTail = 0;
    queueFull = false;
    packetNumber = 0;
    sampleIndex = 0;
  }
}

Is 200 Hz the frequency of the vibration that you need to sample? If so, you will need a sampling rate of at least 400 S/s, and preferably 4–6 kS/s or more.

"Real-time" refers to the delay between when the measured event occurs and when it becomes available to you for storage, display, and/or processing (it is not the same as temporal resolution, which is set by the sampling rate). You cannot have absolute real-time measurement, because at a minimum, you will be limited by the speed of light (e.g., a BLE transmission will require 3 nanoseconds of travel time for every meter of separation between the transmitter and the receiver).

Thus, in addition to specifying information that establishes the required sampling frequency, you will need to provide quantitative information about the maximum acceptable delay between measurement and data availability.

To put it another way, by the time you have measured the event, it is already in the past.