BT app text sending is unreliable to HC-06 connected to a Arduino

Have made my first app and connected to the HC-06 (tagged the HC-05 as it is only one available) all successful except some cases.

Background is I'm using the BT app to allow a User to choose from a list of patterns what colour a strip of LEDs lights to. The code to change the LED patterns are on the Arduino and i used a if, If else ladder to swap between which pattern is running. The app sends the first three letters of the pattern chosen from the Pick List selection to the arduino.

To try and fix the issues I have joined a messageStart and messageEnd character to each message sent. when Serial. available is true it peeks and looks for the messageStart character if it is not the right char then it reads the char from the buffer in-order to clear it. If the peeked char is the messageStart char then it reads the data (as a string) untill the messageEnd char is found this didn't help much... My next thoughts are to scope the RX signal to see if it is still a square. Since I am using Serial.Print() to print the received text could this slow the received messages?

BT_App_2.ino (11.0 KB)

*When I choose red, white or blue from the pick list the data does not send reliably and the button needs tapped a few times caused by below
*after a few taps the data received is not "Red" "Whi" or "Blu" and sometimes comes up as "))?" and other garbled charicters
*The when the any colour message is tapped a few times it stalls changing the colour, when you pick another pattern to get out of the stall it runs the any colour pattern.
*I feel like this is mainly a Arduino code issue as the MIT app has worked pretty much flawlessly

if you need the full app let me know how to link it or I can screenShot it

Export your .aia file and upload it here.
export_and_upload_aia

Hello Martin

That is a very strange loop you have there :upside_down_face:

Also:

Your App should prevent repeated taps, that will only exacerbate the problem.

However many choices (patterns) there are in the App, just send an integer (as text) to represent each choice: 1 = red, 2 = white and so on. You do not need to use peek.

Try this (partial) code, combined with yours for the strip object:

EDIT: Every Switch Case must end with a Break:
BT_App_3.ino (1.4 KB)

1 Like

BT_Lights.aia (12.9 KB)

Since the BT looses connection when changing between screens, the layout boxes are my "screens" and are either enabled or disabled, so there's a layout to connect to BT and a layout for the patterns.

1 Like

I'll have a tweak and probably be back with some questions, yea the loop is a bit odd I'm not particularly proud of the night rider function but it works at least.

1 Like

You mean Arrangements :slightly_smiling_face: Using Arrangements as virtual screens makes coding a lot easier in most types of Apps.

1 Like

Your code works flawlessly, plenty more reliable, I'm sure to figure out how it does what it does, as I continue to work on the project. Rather than the text, I'm using the chosen list pick index, it even works on double digits!!

Project going smoother now, but future ideas are to update the Arduino sketch via the BT module so I don't have to disassemble car interior to do a small update for a pattern ant tips or a point on the right direction would spoil me, i know the bootloader needs changed etc and time can teach me the rest.

1 Like

As you mentioned in the code comments, the code runs every 500ms, half a second. Would problems arise when the code in the if statement takes longer than 0.5sec to run (some patterns take time 3-10 sec).

Thanks for all the help, your a star!

You can change that time interval so that it is longer than the time taken by the most complex pattern.