Can You Play Do-Re-Mi While Line Following? – Task Management in C
by AfrelEdTech in Circuits > Robots
42 Views, 0 Favorites, 0 Comments
Can You Play Do-Re-Mi While Line Following? – Task Management in C

Robotics competitions often require robots to perform multiple tasks at the same time — like moving while playing a sound, or detecting something while keeping balance. This tutorial explores one such challenge using a simple, classic test:
Can a robot play Do-Re-Mi-Fa-So-La-Ti-Do while smoothly following a line?
We’ll demonstrate how different the results can be when task separation is handled properly in C — and why basic task management techniques are essential even for simple robots.
Objective
Compare two implementations:
- Single-task version – sound and motion are in one loop
- Multitask version – sound playback is handled in a cyclic handler
Supplies
- LEGO SPIKE Prime (or compatible device) with a color sensor and speaker
- 1 PC with Windows 10 or 11 operating system
Hardware Setup
The robot is a simple two-wheeled line follower with a color sensor facing down and mounted near the front center.
- Right motor : Port A
- Left motor : Port B
- Color sensor : Port C
- Speaker : Built-in
Single-Task Implementation
In the first version, we write a single task that:
- Follows the line
- Plays each musical note
Observation:
- While the robot is playing sound, it can’t monitor the line.
- As a result, it drifts off the line before finishing the melody.
Task-Separated Implementation (With Cyclic Handler)
In this version, the music is played using a cyclic handler (a periodic task), while the main task continuously handles line tracing.
.c
.cfg
Observation:
- The robot follows the line smoothly.
- Sound plays without interrupting motion.
- Each function runs in its own timing = better control.
Comparison Video

Why It Matters
Task separation is more than just a coding technique — it’s a strategic advantage in robotics competitions.
Even a simple challenge like “play Do-Re-Mi while following a line” becomes much harder without proper task handling.
In a single-task version, we use dly_tsk(200000) inside the main while loop to play each note.
While the robot is playing a sound, it pauses its line sensing, which leads to:
- Missed line position during playback
- Delayed reaction and drifting off the line
By splitting the music and movement into separate tasks, the robot can monitor the line while playing the melody — resulting in smoother and more reliable behavior.
Even a basic challenge can benefit from task separation. Especially in real-world environments like robotics competitions, it brings:
- Improved responsiveness
- Cleaner, more readable code
- A design that’s easier to expand and maintain
Summary
Single-task : Robot drifts off the line during playback
Multitask (cyclic handler) : Smooth line following and uninterrupted sound playback
What's Next?
We’ll handle a more practical challenge:
Drive, Detect, React Line follow + wall detection + emergency stop with resume.
Stay tuned!
【Free Trial】Start C Language Programming With Afrel's Educational Materials
Are you intrigued by the idea of programming your SPIKE™ Prime with C? Did you know that Afrel is a key information provider for SPIKE™ Prime?
To meet the demand of those who want to "learn more about SPIKE™ Prime " and "try programming in C," Afrel has developed and sells educational materials for learning C language programming with SPIKE™ Prime.
Stepping up from Python to C is an excellent opportunity to further enhance your programming skills. Learning C will give you a deeper understanding of how computers work and enable more advanced control.
Afrel's C language materials are designed for beginners, ensuring a smooth learning experience. Why not take this chance to discover the joy of controlling SPIKE™ Prime with C and expand your programming horizons?
If you're ready to tackle C language programming, click the link below for more details.
Learn more about SPIKE™ Prime C Language Programming Materials here