📖 Scenario: You are building a simple health monitoring system for a FreeRTOS-based embedded device. The system needs to track the heartbeat of a task to ensure it is running properly. If the heartbeat is missed, the system should detect it and raise an alert.
🎯 Goal: Create a FreeRTOS task that sends a heartbeat signal periodically. Then, implement a monitoring task that checks the heartbeat and prints a message if the heartbeat is missed.
📋 What You'll Learn
Create a global variable heartbeat_flag to track heartbeat status
Create a task HeartbeatTask that sets heartbeat_flag to 1 every 1000 ms
Create a task MonitorTask that checks heartbeat_flag every 1500 ms
If heartbeat_flag is 0, print "Heartbeat missed!"
If heartbeat_flag is 1, print "Heartbeat received." and reset heartbeat_flag to 0
💡 Why This Matters
🌍 Real World
Heartbeat monitoring is used in embedded systems to detect if critical tasks are running properly and to trigger recovery actions if they fail.
💼 Career
Understanding task health monitoring and heartbeat mechanisms is important for embedded software developers and DevOps engineers working with real-time systems.
Progress0 / 4 steps
1
Create the heartbeat flag variable
Create a global variable called heartbeat_flag and initialize it to 0.
FreeRTOS
Hint
Use volatile int heartbeat_flag = 0; to declare the flag.
2
Create the HeartbeatTask
Create a FreeRTOS task function called HeartbeatTask that sets heartbeat_flag to 1 every 1000 milliseconds using vTaskDelay.
FreeRTOS
Hint
Use an infinite loop and vTaskDelay(pdMS_TO_TICKS(1000)); to wait 1 second.
3
Create the MonitorTask
Create a FreeRTOS task function called MonitorTask that checks heartbeat_flag every 1500 milliseconds. If heartbeat_flag is 0, print "Heartbeat missed!". If it is 1, print "Heartbeat received." and reset heartbeat_flag to 0.
FreeRTOS
Hint
Use an infinite loop and check heartbeat_flag. Use printf to print messages.
4
Print the heartbeat status in main
In the main function, create the tasks HeartbeatTask and MonitorTask using xTaskCreate. Start the scheduler with vTaskStartScheduler(). The program should print heartbeat status messages.
FreeRTOS
Hint
Use xTaskCreate to create tasks and vTaskStartScheduler() to start FreeRTOS.
Practice
(1/5)
1. What is the main purpose of a heartbeat task in FreeRTOS?
easy
A. To permanently block other tasks from running
B. To repeatedly indicate the system is running and healthy
C. To increase CPU usage by running continuously without delay
D. To handle user input events exclusively
Solution
Step 1: Understand heartbeat task role
A heartbeat task runs repeatedly to show the system is alive and working properly.
Step 2: Eliminate incorrect options
Blocking other tasks or handling user input are not typical heartbeat functions.
Final Answer:
To repeatedly indicate the system is running and healthy -> Option B
Quick Check:
Heartbeat = system health indicator [OK]
Hint: Heartbeat tasks show system health by running repeatedly [OK]
Common Mistakes:
Thinking heartbeat blocks other tasks
Assuming heartbeat handles user input
Believing heartbeat increases CPU load unnecessarily
2. Which FreeRTOS function is best to use inside a heartbeat task to wait without blocking other tasks?
easy
A. vTaskResume()
B. vTaskSuspend()
C. vTaskDelete()
D. vTaskDelay()
Solution
Step 1: Identify delay function for non-blocking wait
vTaskDelay() suspends the task for a time, allowing others to run.
Step 2: Understand other functions
vTaskSuspend() pauses a task indefinitely, vTaskDelete() removes it, and vTaskResume() restarts a suspended task.
Final Answer:
vTaskDelay() -> Option D
Quick Check:
Use vTaskDelay() for timed waits [OK]
Hint: Use vTaskDelay() to wait without blocking [OK]
Common Mistakes:
Using vTaskSuspend() which blocks indefinitely
Confusing vTaskDelete() with delay
Trying vTaskResume() to wait
3. Given this heartbeat task snippet, what will be the output if the task runs correctly?
A. vTaskDelay() argument should be in ticks, not milliseconds
B. printf() cannot be used inside tasks
C. while(1) should be for(;;)
D. Missing task notification call
Solution
Step 1: Check vTaskDelay argument type
vTaskDelay() expects delay in ticks, not milliseconds directly.
Step 2: Correct usage
Use pdMS_TO_TICKS(1000) to convert 1000 ms to ticks for vTaskDelay.
Final Answer:
vTaskDelay() argument should be in ticks, not milliseconds -> Option A
Quick Check:
vTaskDelay(ms) wrong, use pdMS_TO_TICKS(ms) [OK]
Hint: Always convert ms to ticks with pdMS_TO_TICKS() [OK]
Common Mistakes:
Passing milliseconds directly to vTaskDelay()
Thinking printf() is disallowed in tasks
Believing while(1) is invalid syntax
5. You want to implement a heartbeat task that signals system health by toggling an LED every 500 ms and also logs a message every 5 seconds. Which approach is best?
hard
A. Create two separate tasks: one toggling LED every 500 ms, another logging every 5 seconds
B. Use one task with vTaskDelay(500) to toggle LED and count loops to log every 5 seconds
C. Use a single task with vTaskDelay(5000) to do both toggle and log at once
D. Toggle LED and log message without any delay to maximize responsiveness
Solution
Step 1: Understand timing requirements
LED toggles every 500 ms, logging every 5 seconds require different intervals.
Step 2: Evaluate task design options
Two separate tasks allow independent timing without complex counting or blocking.
Step 3: Consider drawbacks of other options
Single task with 500 ms delay and counting adds complexity; 5000 ms delay misses LED timing; no delay wastes CPU.
Final Answer:
Create two separate tasks: one toggling LED every 500 ms, another logging every 5 seconds -> Option A
Quick Check:
Separate tasks for different intervals = clean design [OK]
Hint: Use separate tasks for different heartbeat intervals [OK]
Common Mistakes:
Trying to handle all timing in one task
Using long delays missing faster toggles
No delays causing CPU overload
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.