How to Make Your Robot Smarter With Concurrent Tasks
by AfrelEdTech in Circuits > Robots
62 Views, 0 Favorites, 0 Comments
How to Make Your Robot Smarter With Concurrent Tasks

As we saw in the article, doing multiple actions at once—like playing music while following a line—is difficult with a single task.
In robotics competitions, your robot must monitor sensors, react quickly, and handle several tasks simultaneously.
In this project, you’ll learn how to build a smarter LEGO SPIKE Prime robot that can do more than one thing at a time — thanks to a real-time operating system (RTOS) and cyclic handlers.
New to SPIKE-RT?
It’s a real-time OS that brings multitasking power to the SPIKE Prime.
Check out this intro first to see how it works:
Introducing SPIKE-RT: the C-Language Software Platform for SPIKE Prime
Objective
Build a robot that can:
- Follow a line continuously
- Stop when an obstacle is detected
- Pause and resume when the touch sensor is pressed
Supplies
- LEGO SPIKE Prime a touch sensor, distance sensor, and color sensor
- 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
- Touch Sensor : Port D
- Distance Sensor : Port E
Set Up the Program
We’ll use three separate tasks in C:
- Main: handles line following
- distance_task: checks for walls
- touch_task: handles emergency stop and restart
Each task is small and focused. Cyclic handlers automatically run each sensor task at regular intervals— no need to call them manually.
.c
.cfg
See It in Action

- Emergency Stop & Resume — Tap to pause, tap again to resume
- Line Following — Smooth and stable
- Wall Detection — Stops automatically when it senses a wall
Thanks to the Real-time OS and Task Separation:
- Each part of the robot runs independently
- Sensor input is processed without delay
- You don’t have to write complex logic in a single loop
This makes your code cleaner, faster, and easier to expand when you want to add more features.
【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