xTaskCreate();

When you use the task notification in the BLE task to finally start the measurement loop it must be outside the loop, right ? Otherwise it’s waiting again on the next loop iteration and waits forever.
That would be a simple programming bug you could easy track and find out with a debugger just stepping through the (measurement loop) code. The same applies to the verification if the dk_task works as desired. Set a breakpoint at the next line after waiting for the task notification, step through the code to see what’s going on. Also the variables can be inspected or watched.
I’m unsure - do you know what a debugger is ? If not you really should find out. You’ll need it :wink:

Although I do not understand what you have shared, I would like to thank you!

I’m sure there are PSOC6 JTAG/SWD debuggers and I’m also sure that others use them.
Google is your friend :slight_smile: Good luck !

Your problem is not very clear. Please share your complete code.

That’s the code you helped me build earlier!
However, now it no longer works as it used to, but it is wrong from the beginning.

This is code int main function of my

/*******************************************************************************
 * Header Files
 *******************************************************************************/
#include "cybsp.h"
#include "cy_retarget_io.h"
#include <FreeRTOS.h>
#include <task.h>
#include "timers.h"
#include "GeneratedSource/cycfg_bt_settings.h"
#include "wiced_bt_stack.h"
#include "cybsp_bt_config.h"
#include "cybt_platform_config.h"
#include "cy_pdl.h"
#include "cyhal.h"
#include <ble_adc.h>
#include <queue.h>
#ifdef ENABLE_BT_SPY_LOG
#include "cybt_debug_uart.h"
#endif

/*******************************************************************************
 *        Macro Definitions
 *******************************************************************************/
#define BLE_TASK_STACK_SIZE                 ((configMINIMAL_STACK_SIZE * 4 ))

#define BLE_TASK_PRIORITY                   ((configMAX_PRIORITIES - 1 ))

#define UART_TASK_STACK_SIZE                 ((configMINIMAL_STACK_SIZE * 4 ))

#define UART_TASK_PRIORITY                   ((configMAX_PRIORITIES - 2 ))

#define DK_TASK_STACK_SIZE                 ((configMINIMAL_STACK_SIZE * 4))

#define DK_TASK_PRIORITY                   ((configMAX_PRIORITIES - 3 ))

/*******************************************************************************
 * Variable Definitions
 *******************************************************************************/
TaskHandle_t uart_handle;
TaskHandle_t dk_task_handle;
TaskHandle_t ble_task_handle;
QueueHandle_t print_queue;

/******************************************************************************
 * Function Definitions
 ******************************************************************************/

/*******************************************************************************
 * Function Name : main
 * *****************************************************************************
 * Summary :
 *   Entry point to the application. Set device configuration and start BT
 *  stack initialization.  The actual application initialization will happen
 *  when stack reports that BT device is ready.
 *
 * Parameters:
 *    None
 *
 * Return:
 *    None
 ******************************************************************************/
int main()
{
	cy_rslt_t cy_result;
	BaseType_t rtos_api_result = pdPASS;


#if defined (CY_DEVICE_SECURE)
    cyhal_wdt_t wdt_obj;

    /* Clear watchdog timer so that it doesn't trigger a reset */
    result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
    CY_ASSERT(CY_RSLT_SUCCESS == result);
    cyhal_wdt_free(&wdt_obj);
#endif

	/* Initialize the board support package */
	cy_result = cybsp_init();

	if (CY_RSLT_SUCCESS != cy_result)
	{
		CY_ASSERT(0);
	}

    cy_result = cyhal_gpio_init(DATA_PIN, CYHAL_GPIO_DIR_BIDIRECTIONAL, CYHAL_GPIO_DRIVE_PULLUP, 1);
    cy_result = cyhal_gpio_init(P6_3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, 1);
    cyhal_gpio_init(P10_6, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, false);

	/* Enable global interrupts */
	__enable_irq();

#ifdef ENABLE_BT_SPY_LOG
	{
		cybt_debug_uart_config_t config = {
				.uart_tx_pin = CYBSP_DEBUG_UART_TX,
				.uart_rx_pin = CYBSP_DEBUG_UART_RX,
				.uart_cts_pin = CYBSP_DEBUG_UART_CTS,
				.uart_rts_pin = CYBSP_DEBUG_UART_RTS,
				.baud_rate = DEBUG_UART_BAUDRATE,
				.flow_control = TRUE};
		cybt_debug_uart_init(&config, NULL);
	}
#else
{
	/* Initialize retarget-io to use the debug UART port */
	cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
}
#endif //ENABLE_BT_SPY_LOG

printf("***************BTSTACK FreeRTOS Example *********************\n");
printf("****************** PSoC 6 BLE ADC DHT11 ********************\n");

print_queue = xQueueCreate(10, sizeof(struct read));
if(print_queue == NULL)
{
	CY_ASSERT(0);
}

xTaskCreate(DHT_Task, "DHT Task", configMINIMAL_STACK_SIZE, (void*) print_queue, 2, NULL);
xTaskCreate(Print_Task, "Print Task", 3*configMINIMAL_STACK_SIZE, (void*) print_queue, 1, NULL);

/* Create a Button Task */
//rtos_api_result |=xTaskCreate(uart_task, "Uart", UART_TASK_STACK_SIZE, NULL, UART_TASK_PRIORITY, &uart_handle);

rtos_api_result |=xTaskCreate(ble_task, "BLE", BLE_TASK_STACK_SIZE, NULL, BLE_TASK_PRIORITY, &(ble_task_handle));

rtos_api_result |=xTaskCreate(dk_task, "DK", DK_TASK_STACK_SIZE, NULL, DK_TASK_PRIORITY, &(dk_task_handle));

if (pdPASS  == rtos_api_result)
{
	/* Start the RTOS scheduler. This function should never return */
	vTaskStartScheduler();

	/* Program should never reach here! */
	printf("[Error]   : FreeRTOS scheduler failed to start \r\n");
}
else
{
	printf("[Error]   : FreeRTOS failed to create task \r\n");
}

printf("PSoC 063 BLE read DHT11 using Modus Toolbox 3.0");

/* Should never get here */
CY_ASSERT(0);
}

/* END OF FILE [] */

This is the Bluetooth communication code

void ble_task(void *arg)
{
	wiced_result_t wiced_result = WICED_BT_SUCCESS;

	/* Configure platform specific settings for the BT device */
	cybt_platform_config_init(&cybsp_bt_platform_cfg);

	/* Register call back and configuration with stack */
	wiced_result = wiced_bt_stack_init (app_bt_management_callback, &wiced_bt_cfg_settings);

	/* Check if stack initialization was successful */
	if( WICED_BT_SUCCESS == wiced_result)
	{
		printf("Bluetooth Stack Initialization Successful \n");
	}
	else
	{
		printf("Bluetooth Stack Initialization failed !! \n");
	}

	for(;;)
	{
		ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

			if(char_notification_enabled == true)
			{
				/* Sample input voltage at channel 0 */
				adc_single_channel_process();

				adc_send_notification();
			}
			if(humidity_notification_enabled == true)
			{
				humidity_send_notification();
				printf("\r\nHumidity = %.2f\r\n",DHT_read.humidity);
			}

			if(temperature_notification_enabled == true)
			{
				temperature_send_notification();
				printf("\r\nTemperature = %.2f\r\n",DHT_read.temperature);
			}
			vTaskDelay(pdMS_TO_TICKS(2000));
	}
}

Here is the condition review

void dk_task( void * pvParam )
{
    for( ;; )
    {
        /* Wait for a notification from the uart_task. */
        ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

        if(adc_percent < 50)
        {
        	cyhal_gpio_write( P10_6, true );
        }
        else
        {
        	cyhal_gpio_write( P10_6, false );
        }
        if(notify_humidity < 60)
        {
            cyhal_gpio_write( P10_6, true );
        }
        else
        {
        	cyhal_gpio_write( P10_6, false );
        }
        if(notify_temperature > 35)
        {
            cyhal_gpio_write( P10_6, true );
        }
        else
        {
            cyhal_gpio_write( P10_6, false);
        }
        vTaskDelay(pdMS_TO_TICKS(200));
    }
}

And here is my DHT11 communication function

void DHT_Task(void* pvParameters)
{
	extern TaskHandle_t dk_task_handle;
	extern TaskHandle_t ble_task_handle;
	/* Variable to store the queue handle */
	QueueHandle_t print_queue;
	print_queue = (QueueHandle_t) pvParameters;

	/* Variable to store temperature and humidity values */
	struct read DHT_read = {0, 0}; /* Variables to store temperature and humidity values */

		for(;;)
		{
			DHT_read.result_code = DHT_Read(&DHT_read.humidity, &DHT_read.temperature);

			if(DHT_read.result_code == SUCCESS)
			{
				/* Toggle the LED to indicate that a valid reading is obtained */
				cyhal_gpio_toggle(P6_3);

				/* *************************************************************
				 * If the sensor values are valid, pass the readings to the queue.
				 * If the queue is full, enter blocked state and wait for the
				 * Print_Task to read the value from the queue.
				 * *************************************************************/
				xQueueSendToBack(print_queue, &DHT_read, 0);
			}
		else
		{
			/* Exit critical section */
			taskEXIT_CRITICAL();

			/* *************************************************************
			 * If the sensor values are not valid, pass the error code along
			 * with the previous value to the queue. Print_Task reads the
			 * error value and decides the course of action.
			 * *************************************************************/
			xQueueSendToBack(print_queue, &DHT_read, 0);
		}

		/* ******************************************************************
		 * As the sensor sampling rate is ~2 seconds. task-delay function is
		 * called for 2 seconds.
		 * ******************************************************************/
		xTaskNotifyGive(ble_task_handle);
		xTaskNotifyGive(dk_task_handle);

		vTaskDelay(pdMS_TO_TICKS(2000));
	}
}

And here is the code that reads the ADC


void adc_single_channel_process(void)
{
	 ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

	 for(;;)
	 {
		 /* Read input voltage, convert it to millivolts and print input voltage */
		 adc_result_0 = cyhal_adc_read_uv(&adc_chan_0_obj) / MICRO_TO_MILLI_CONV_RATIO;
		 printf("\nChannel 0 input: %4ld\r\n", (long int)adc_result_0);

		 adc_percent = (100-((int)adc_result_0)*100/3298+0);
		 printf("\r\nGiatriphantram = %d%%\r\n", (int)adc_percent);
	 }
	 xTaskNotifyGive(ble_task_handle);
	 xTaskNotifyGive(dk_task_handle);
	 vTaskDelay(pdMS_TO_TICKS(2000));
}

That’s it!

The problem I have is that the program executes the task at dk_task. My intention is to use dk_task as task to compare condition.

if (adc_percent < 50)
           {
           cyhal_gpio_write( P10_6, true );
           }
           other
           {
           cyhal_gpio_write(P10_6, false);
           }
           if (notify_humidity < 60)
           {
               cyhal_gpio_write( P10_6, true );
           }
           other
           {
           cyhal_gpio_write(P10_6, false);
           }
           if (notice_temperature > 35)
           {
               cyhal_gpio_write( P10_6, true );
           }
           other
           {
               cyhal_gpio_write(P10_6, false);
           }
}

I set up pin P10_6 to control my motor. If the above condition is satisfied, the above code will control the motor to open, if not, the motor will be turned off.
But when executing the program dk_task opens the motor permanently and does nothing else!
I don’t understand what the problem I am having!
I have tried many methods as above but still can’t find the problem!
Thank!

Really ? There is no matching taskENTER_CRITICAL(); at least in the posted code.
The code you’ve posted seems to be different than in the zip’d project, right ?
That’s very confusing and misleading.

Also in the zip’d project there are serious bugs like returning from a function with a critical section being entered:

In DHT_Read()

	taskENTER_CRITICAL();
	while( cyhal_gpio_read(DATA_PIN) == 1)
	{
		cyhal_system_delay_us(1);
		ack_time++;
		if(ack_time > timeout_duration)
		{
			/* Connection timed out */
			return DHT_CONNECTION_ERROR; <----- taskEXIT_CRITICAL(); missing !!!

You should sit back and take your time to review and understand your code in detail.
Think about how to rework and verify your application in a systematic and modular way.
When posting code you should post the code you’re really using.
I afraid otherwise you’ll be lost :wink:

It is true that this code has been modified compared to the code in zip’d.
That’s the DHT11 library I got on github!
Thanks!

How do you think we should figure out which code to look at? Can you share the latest complete code on GitHub?

Yes, I will share it on github

That’s the file I shared on github

The code seems to send notifications and add delays randomly. I cleaned up some of those and uploaded a version which I think should work. I’d strongly recommend that you read this book to learn FreeRTOS fundamentals - https://www.freertos.org/fr-content-src/uploads/2018/07/161204_Mastering_the_FreeRTOS_Real_Time_Kernel-A_Hands-On_Tutorial_Guide.pdf

Thank you for your help!