1 year, 11 months ago.

Led blinking multithreading

Hope everyone's well, i have a two thread functions i am calling under "int main()" but one of them works which has the main process while the other one which is "LED_BLINK" does not work i have look up for example on thread and multi-threading for LEDs , how can i let the LED thread work?

int main() {
        

        humidity.init();

        humidity.calib();

        printf("---------ELEC350/351 Simple Temperature Humidity and Pressure Sensor Monitor---------\n\r");

        printf("---------Using the X-NUCLEO-IKS01A1 shield and mbed-os Libraries---------\r\n\n");

        thread.start(Data_Protocol); //Thread Reads data from the array

        thread.start(LED_BLINK); //Thread for led to be blinking individually

        while(1) {

            struct record record1;

            //Reads from humidity and tempreture sensor

            humidity.ReadTempHumi(&tempCelsius, &humi);

            record1.temp=tempCelsius;

            record1.humidity=humi;

            //Reads from pressure sensor

            barometer.get();

            record1.pressure=barometer.pressure();

            add_to_array(record1); //passing structure to add_to_array function to read,save and update data

            
           

        }

    }
posted by Eng Riyadh 20 Nov 2017

.

posted by Eng Riyadh 20 Nov 2017

If you use

<<code>>
your code
<</code>>

Then the message board won't mess up the code formatting.

You need to include all the code if you want people to be able to help. It doesn't look like you've included the two threads.

posted by Andy A 21 Nov 2017
Comment on this question

1 Answer

1 year, 10 months ago.

Eng,

I would recommend reviewing the Thread guide in our reference documentation. Looking at your code snippet, either your code is incomplete or you just didn't paste the entirety of your program.

Make sure you have code similar to this working:

Thread example

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread;
 
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(1);
    }
}
 
int main() {
    thread.start(led2_thread);
    
    while (true) {
        led1 = !led1;
        wait(0.5);
    }
}

This example has an LED that is flashing in the main thread, as well as another thread specifically dedicated to blinking an LED - similar to the desired result you stated in your question.

In addition, once you have that running, you can add the humidity and barometer sensors to the led thread, to the main thread, or even create another thread that is separate from your LED (depending on your specific implementation)

Thank you , i have actually sorted it out using another thread as at the top i have initialized one thread called "thread" i added another thread and called it thread2 , and edit the first thread and called it thread1 so it became like that

thread1.start(Data_Protocol); Thread Reads data from the array

thread2.start(LED_BLINK); Thread for led to be blinking individually

instead of using

thread.start(Data_Protocol); Thread Reads data from the array

thread.start(LED_BLINK); Thread for led to be blinking individually

..... it works well now

posted by Eng Riyadh 22 Nov 2017

You need to log in to post a question