# Controlling car with button and voice via Bluetooth

**URL:** <https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060>\
**Category:** Libraries\
**Created:** [October 25, 2022, 3:02pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060 "2022-10-25T15:02:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 25, 2022, 3:02pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/1 "2022-10-25T15:02:38Z")

</div>

Hi.  
I want to control my car with button and voice via Bluetooth. I tried to create 2 task: VoiceControl and ButtonControl but they didn’t work as I expected. After discussion on this forum, I decided to post my flow chart of the origin code. Looks like I missed some solutions.  
Hope you can help me. Thank you all.

 ![Copy of Thuật toán app](https://d2v6vdsk2p900z.cloudfront.net/original/2X/3/32066823de2e736d2c01b81ad5f4caf27a6f6ce7.jpeg)

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 25, 2022, 3:47pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/2 "2022-10-25T15:47:49Z")

</div>

For completeness reasons

> [@Task only runs once](https://forums.freertos.org/t/task-only-runs-once/16043):
>
> I created 2 tasks ButtonControl and VoiceControl. At first, when there’s a voice signal, VoiceControl is created and then deleted itself. But after that, it never ran again. I really don’t know where I should fix. Here are my codes and the result on monitor. #include \<Arduino\_FreeRTOS.h\> #include\<String.h\> #include \<SoftwareSerial.h\>// thư viện này cho phép bất cứ cổng digital nào của arduino để giao tiếp theo phương thức serial #define TX\_PIN 8 //định nghĩa các chân giao tiếp với blueto…

is the previous discussion Lucy is referring to.

---

<div class="post-metadata">

**Author:** ![DavidGC-FreeRTOS](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@DavidGC-FreeRTOS](https://forums.freertos.org/u/DavidGC-FreeRTOS)\
**Post date:** [October 25, 2022, 5:59pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/3 "2022-10-25T17:59:17Z")

</div>

Hi @Lucy781227

I’m not sure what distinguishes this issue from the [original post](https://forums.freertos.org/t/task-only-runs-once/16043). If it’s all the same issue, please keep a single thread so everything is there, should anyone have similar queries in the future.

From what I last got from the original thread, you can simply make a queue for the voice commands, and have the listener task continuously listen. Whatever tasks acts on those commands, every 2 seconds IIRC, can simply pop from that queue independent of whether the listening task is currently in-taking another command.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 26, 2022, 3:20am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/4 "2022-10-26T03:20:33Z")

</div>

> [@hs2](#):
>
> For completeness reasons

Yes, it is. I dont know what I missed. ☹

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 26, 2022, 2:56pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/5 "2022-10-26T14:56:34Z")

</div>

> [@DavidGC-FreeRTOS](#):
>
> From what I last got from the original thread, you can simply make a queue for the voice commands, and have the listener task continuously listen. Whatever tasks acts on those commands, every 2 seconds IIRC, can simply pop from that queue independent of whether the listening task is currently in-taking another command.

I see. But there’s a problem here. When VoiceControl is running, I set a delay ~2s for each action. But right after saying a command, if I clicked on the button, I want my car to move following that button.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 26, 2022, 2:57pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/6 "2022-10-26T14:57:58Z")

</div>

> [@DavidGC-FreeRTOS](#):
>
> If it’s all the same issue, please keep a single thread so everything is there, should anyone have similar queries in the future.

It was my fault. Thanks for telling me.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 26, 2022, 3:41pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/7 "2022-10-26T15:41:22Z")

</div>

What about the hints already given to keep the task responsive to further input events (button or voice) from Bluetooth after the car was started resp. while the car runs ?

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 26, 2022, 11:46pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/8 "2022-10-26T23:46:10Z")

</div>

When using the ‘hints’, you mean semaphore?

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 26, 2022, 11:54pm UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/9 "2022-10-26T23:54:43Z")

</div>

When controlling by voice, if you say ‘go straight’, it means the car will be going straight for 2s and then stop. It’s like

> GoStraight();  
> delay(2000);  
> Stop();

During 2s, if there is a button signal, I want my car to move following it. But if I used semaphore, when there’s no more button signal, VoiceControl will be resumed with the rest of code. I want it to start from the beginning.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [October 27, 2022, 2:42am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/10 "2022-10-27T02:42:52Z")

</div>

It sounds like you need a somewhat more complicated sequencing system.

Likely a “Voice” system and a “Button” system that issue commands to a “Car” system with a queue.

If you want commands to be able to interrupt other commands, then rather than doing Go / Wait / Stop, the Go needs to start a timer, which issues the Stop command when the timer expires (and if a command interrupts, it cancels the timer).

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 27, 2022, 8:32am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/11 "2022-10-27T08:32:30Z")

</div>

I didn’t mention _semaphore_ … 🤔  
My point was and is that you should build a state machine resp. a sequencing system as also Richard proposed and use a timeout (or a timer) for the delays.  
The task could always poll for incoming events and just trigger the desired action, if an event arrived. The states of the state machine reflect the states of the car.  
If a timeout (e.g. 2 s) was set (because there was an event to trigger _GoStraight_), check for the expiration of the armed timeout while still polling for incoming events (to issue _Stop_ on expiration) or re-start the timeout if another _GoStraight_ event comes in…  
Good luck 👍

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 27, 2022, 8:58am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/12 "2022-10-27T08:58:19Z")

</div>

> [@richard-damon](#):
>
> If you want commands to be able to interrupt other commands, then rather than doing Go / Wait / Stop, the Go needs to start a timer, which issues the Stop command when the timer expires (and if a command interrupts, it cancels the timer).

Yes. I create functions for them, just using analogWrite(HIGH) or LOW.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [October 27, 2022, 10:11am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/13 "2022-10-27T10:11:02Z")

</div>

> [@hs2](#):
>
> My point was and is that you should build a state machine resp. a sequencing system as also Richard proposed and use a timeout (or a timer) for the delays.  
> The task could always poll for incoming events and just trigger the desired action, if an event arrived. The states of the state machine reflect the states of the car.  
> If a timeout (e.g. 2 s) was set (because there was an event to trigger _GoStraight_), check for the expiration of the armed timeout while still polling for incoming events (to issue _Stop_ on expiration) or re-start the timeout if another _GoStraight_ event comes in…

I see. So setting a timer is different with _delay()_ or _vTaskDelay()_?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 27, 2022, 10:37am UTC](https://forums.freertos.org/t/controlling-car-with-button-and-voice-via-bluetooth/16060/14 "2022-10-27T10:37:45Z")

</div>

You really should get more familiar also with FreeRTOS. This will help you a lot !  
There is a very good documentation for instance here:

> **[Free RTOS Book and Reference Manual](https://www.freertos.org/Documentation/RTOS_book.html)**
>
> RTOS book

> **[FreeRTOS Features - FreeRTOS](https://www.freertos.org/features.html)**
>
> FreeRTOS Kernel Developer Docs Tasks Queues, Mutexes, Semaphores... Direct To Task Notifications Stream & Message Buffers Software Timers Event Groups (or "Flags") Source Code Organization FreeRTOSConfig.h Static vs Dynamic Memory Heap Memory...

> **[FreeRTOS API categories](https://www.freertos.org/a00106.html)**
>
> Links to FreeRTOS API function descriptions ordered by category. FreeRTOS is a portable, open source, mini Real Time kernel. A free RTOS for small embedded systems
