How To Run two tasks simultaneously?

How to run two tasks at the same time, currently im using nicla sense me i want to sample the values and transmit through Bluetooth at the same time
Now, after sampling it takes time to transmit the data through Bluetooth, and sampling is starting only after the data is transmitted.

how can i run two tasks parallelly like sampling and transmitting need to be done in same time

Hi! Welcome to the Forum.

You can't actually run two tasks at the same time in a single core microcontroller. However, you can run things fast enough for a human to believe it happens at the same time.

This suggests you may be usign delay() in a code that should be non blocking.

Take a look at the topic How to get the best out of this forum and post your code accordingly :wink:

After reading the 'How to get the best ... then look in the Tutorials section for 'How to run two or more tasks, or look at State machines.

sounds similar to a commercial project I worked on a few years ago

  1. a LSM9DS1 9-axis iNEMO inertial module (IMU) was sampled using interrupts every 20mSec with results output to a double buffer
  2. in main() when one of the buffers was full it was transmitted over BLE to a smart phone

microcontroller was a PIC24FJ256GA705 using a BM71 BLE module

data transmitted every second in binary approximately 1000bytes

Post the code. Either the halves that need combining or the combination that doesn't work the way you want. Yet.

Say which Arduino board you are using.

Draw a block diagram if there is the least bit of of complexity to your project.

a7

sounds like you want to sample multiple times, possibly continuously, but transmit each value after sampling.

what are you sampling?
how many samples do you need?

why not collect a # of samples and then send the samples, possibly in just one bluetooth msg?

or if you're trying to detect something in a range of values, why not jut send a msg when the range is entered?

okay, thank you

here i need to sample the accelerometer values same time and BLE transmission need to be done at the same time
or else samples are pushed into queue and pulls all the values in queue in packets through BLE
is it possible to run different tasks at the same time?

yes, here im sampling accelerometer values of nicla sense me which consist in-built BLE, when sampling is continously done by nicla sense it stops the sampling when the bluetooth packets are transmitted
i am getting random delays between last sample and first sample after transmission, If we do the sampling as one task and BLE transmission as another task they will not overlap eachother and we can overcome this problem.

im using arduino nicla sense me board, im sampling accelerometer values of nicla sense me which consist in-built BLE and in-built acceleromter sensor, when sampling is continously done by nicla sense it stops the sampling when the bluetooth packets are transmitted
i am getting random delays between last sample and first sample after transmission, If we do the sampling as one task and BLE transmission as another task they will not overlap eachother and we can overcome this problem.

maybe possible using a microcontroller with two cores, e.g. ESP32
although I have worked on various industrial and research projects requiring data acquisition and transmission never needed to use such techniques

by using interrupts to sample data into buffer which are then transmitted in the main program (e.g. in loop() using Arduino IDE) it can appear that parallel operations are taking place at the same time

you could also look at using DMA - the sensor hardware writes data directly into a buffer in memory

what are your requirements - describe the projects? e.g. how many sensors, how many data samples/second, what is the data receiver, etc

So far you have not specified any numbers.
Depending on

  • how many datavalues
  • aquired at what frequency
  • how many databytes per vmeasured value
  • how much delay is accapetable between aquiring measurings

It will be sufficient to go on using your existing hardware
by just writing clever code

or it might be nescessary to change to a much faster microcontroller with DMA-features to realise what you want.

By only telling sending data by bluetooth and aquiring measurings at the same time
you have not specified anyting but saying "same time"

There is no such thing as making a microcontroller executing two different tasks at the EXACT same time down to the same femto-second
1 femto-second 0,000 000 000 000 001 Seconds
Not even the highest advanced supercomputer can do that,
because it would require a clock of 1000 GHz

And this is the reason why you have to specify numbers.

could sampling be driven by an interrupt that collects the samples? won't the interrupt continue to run during any bluetooth operations?

i have Nicla sense ME board which have in-built BLE and in-built accelerometer sensor, i want to read accelerometer values continuosly for real-time

you have said this several times already

which sugestions are you not satisfied with?

What do you mean by "real Time", e.g. every microsecond, every millisecond, every second, every minute, every hour???
the definition of "real Time" depends on the project requirements

for a start answer the questions asked by @StefanL38 in post 12

i need real-time values and for different frequencies upto 200hz, the accelerometer sensor in-built in nicla sense me give 4byte float value per axis, and delay will be our sampling rate.

that's useful information
every 5ms

which suggestions are you not satisfied with?

in millisecond, and that interval or delay will be our sampling rate if we are reading the samples for every 5ms then our sampling rate will be 200hz for 1second

here's code that uses a timer driven interrupt

// demonstrate use to timer driven interrupt

#include <TimerOne.h>

const int PinLed = LED_BUILTIN;

// -----------------------------------------------------------------------------
// toggle led
void isr (void)
{
    digitalWrite (PinLed, ! digitalRead (PinLed));
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    Serial.println ("ready");

    pinMode (PinLed, OUTPUT);

    Timer1.initialize(200000L);        // 0.2 sec in usec
    Timer1.attachInterrupt (isr);
}

// -----------------------------------------------------------------------------
void loop () {}