Character modified in Bluetooth

Hi there !

In my project of an MIT/Arduino photo Focus Stacking machine, I need to enter a motor step value.

I then chose in MIT a "text box" to manually write what I need.

I managed to do what I want.

However, in testing, when I write/delete/rewrite several times quickly, I sometimes get unwanted values.

For example at some point I will write "975" , and the value received in Arduino serial monitor is 875... (afterwards when I type another value, it returns to normal.)
At another point, I will write for example "653", and the received value is "6A3" (or sometimes with weird sign).

What do you think?

I'm quite a beginner. There is probably some information missing, don't hesitate to ask me.

I post pictures of information that seems useful to me.

Thanks in advance! :slight_smile:

[I use Google translate]

Capture d’écran 2022-12-10 150233

Capture d’écran 2022-12-10 150326

  if(bluetooth.available()){            // Si Bluetooth recois qql ch
  messageRec =;        // lire le message recus et le stocker dans variable messageRec

  if (messageRec=='$'){                                                                   //
   arretTemps = 1;   //Arrêt envoye bluethoot position moteur XYZR toute les 100mili.s    //
   boucleString = 1;                                                                      //
   stringBtPas="";   // rénitialisation stringBtPas, a chaque nouvelle entrer sur MIT     //
  }                                                                                       //
  if (boucleString==1){                                                                   //
    stringBtPas = stringBtPas + messageRec;                                               //
    Serial.print("stringBtPas = ");                                                       //
    Serial.println(stringBtPas);                                                          //
  }                                                                                       //
  if (messageRec=='*'){                                                                   //
    stringBtPas = stringBtPas.substring(1, stringBtPas.length()-1);                       //
    //Serial.print("stringBtPas* = ");                                                    //
    //Serial.println(stringBtPas);                                                        //
    nmbPas=stringBtPas.toInt();                                                           //
    Serial.print("nmbPas = ");                                                            //
    Serial.println(nmbPas);                                                               //
    arretTemps = 0;   //Reprise envoye bluethoot position moteur XYZR toute les 100mili.s //    
  }                                                                                       //

  } // Fin BT```

Hello Jon

So you are sending numbers as text, but sometimes an incorrect value arrives at the Arduino. That sounds as though there is some sort of interference disrupting the data stream, an object between your Android device and the Arduino or the distance between the two being too far. It's also possible for your App to be disrupted by an Anti-Virus App.

Hello Chris Ward,

Thank you for your reply :slight_smile:

So you send numbers as text, but sometimes an incorrect value comes to the Arduino.

Yes that's it. Inaccurate, and sometimes incomplete... (about 1 time out of 10). I use this method because I saw it somewhere as an example, but if there is an easier way, why not!

I read somewhere that with Arduino, using the "String" character string was not great (why?) and that it was better to use a "char" array. Do you have an opinion on that?

Could interference be possible from a phone, computer, Wi-Fi network?
Could an antivirus be the cause?

For distances:
My tablet is connected via wifi via a repeater (good connection)
Arduino in bluetooth to the tablet (less than a meter)
Wired computer at the internet box

Do you think an unsuitable baud rate could be the cause? Too slow, too fast, who?

Thank you for these tips! :slight_smile:


---- *
---- Pas
---- $

Here other ideas...

Hello Juan Antonio,

Essayer mixte
---- *
---- Pas
---- $

Forgive me, but I don't think I understood very well.

Yes it is on this page that I took example 2A, I added a "$" at the beginning, so that Arduino knows when the value I send to it starts and when it stops. Ex: $684*

I also use part of example 5 for the buttons (sends a letter to the Arduino)
As well as example 7 for two potentiometers (XY motor control joystick)

This is why when I send for example $684* and Arduino receives 68R, it triggers the action linked to the letter R…

To make it more meaningful, I tried to make two videos to illustrate my problem (I had to lower the quality to be able to post them here).

You will see that on the 1st video:

  • The first number I type is 60, and Arduino receives $60(
  • The second digit 606, and I get 607
  • Then the others all is OK

For the second video:

  • The first 5 numbers are received correctly.
  • Arrived at the 6th, the number 3 gives $?*

Have you encountered this type of problem before?


Oh okay! I will try that right away! :slight_smile:

Thanks for that info (I didn't even know you could add more entries to the "join" block :grinning_face_with_smiling_eyes:).
But unfortunately, I still have numbers that sometimes come with unwanted signs...

Think it might be a baud rate issue?
I'm at 115200 baud on Bluetooth as well as the Arduino serial monitor. Too fast ? too slow ? synchronization problem? Or does it have nothing to do with it?

I had a doubt, maybe it was my Arduino program that modified the values ​​it received. So I displayed on the Ardunio serial monitor, directly what it received by Bluethoot. And I confirm that it sometimes receives unwanted characters (letters, signs).

So I think it's either a problem at MIT or a problem between MIT and Arduino... But what... :thinking:

The Arduino library is based on 'C'. 'String' is from C++. It can work with C but you have to know what you are doing (C and C++ are not really close relations).

Given that there are hundreds of Users using App Inventor's Bluetooth successfully today I think your assumption is off the mark.

The values you are sending do not match-up with the Arduino Script snippet you have posted here, so first of all we need that .ino file (upload as a file).

You can experiment with that (try 9600)

You haven't given us basic details - Arduino model, Arduino Bluetooth version (is it built in or a module/shield?), Android version and Bluetooth version of the Tablet. We don't know how the send event is triggered nor the frequency (time interval between sends).

Have you ensured there isn't any interference that could corrupt the signal? A further consideration is the environment - temperature and humidity - an excess of either will make the Arduino fail. Also, depending on the model, the Arduino power supply may not be adequate.

Thank you for your help!

I will search, and I will put here all the information you requested.

Thanks again everyone! :slight_smile:

Dear @Jon1,
in your .ino file it seems that you never re-initialize the variable boucleString.
I suppose that when the received string from AI2 is completed ("*" as ending character), you should reinitialize it to 0.
Or, do you that, elsewhere in your code ?
Apart of that, as @ChrisWard said, try to reduce the baudrate (of the BT side only, not the one to the serial monitor) to 9600. Provided that your BT on Arduino side is a HC05 or HC06. Which CPU board are you using (UNO, MEGA, NANO, ESP32,....) ? In any case, some details on your HW will help the community to understand a bit more your environment (as suggested by Chris).
Last, but not least, strings are really time (and memory) consuming on Arduino implementation. By using array of characters will avoid you (some) headaches due to unexpected behaviours.
Meilleures salutations,

Hello Ugo,

I'm not very expert, but the re-initialize is done with this line of code right? (see picture) With each new reception that starts with a "$", my "stringBtPas" re-initialize, doesn't it? Or did I not understand? :roll_eyes:

In a few days I will post details of my hardware.

Do you advise me to use a "char" array instead of a "string"?


Hello @Jon1 ,
probably I've been misundesrtood. I will switch to French (je le comprend un petit-peu).
La variable boucleString est mise à 1 lorsque ton code reçoit le charactère "$", c'est à dire le début de la phrase qui arrive de AI2. Quand elle est mise à 1 tu vas ajouter le charactère reçu à stringBtPas, mais comme tu ne la met plus à 0 elle reste à 1, donc tu vas ajouter toujour le charactère à stringBtPas. En effet tu re-initialises stringBtPas à "" (null string) et ça peut etre qu'il va te "sauver", mais selon moi, quand tu reçois le "*", la variable boucleString devrait etre mise à 0.
Si non, quel est le sens de cette variable ?

This is why we need to see your .ino file - the code snippet in isolation does not make sense:

1 Like

Try other codes for receiving data, here I have put one with readString...
p9A1_bluetooth_textoi_2.aia (3.0 KB)

Bluetooth module in pin 2,3 of Arduino.

#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(2,3);
String texto;

void setup() { 

void loop() { 
  if(Bluetooth.available()) {
    // texto = Bluetooth.readStringUntil('\n');
    texto = Bluetooth.readString();

Possibly the error is in the way of sending data, use an ENTER key (sendtext button) to send the data instead of the Clock.

Salut Ugo,

merci pour cette info, j’ai effectué la modification sur mon programme comme tu me la conseillé. Malheureusement, cela n’a pas résolu le problème, mais peut-être que c'est plus logique qu'avant, donc je garde la modification que tu ma conseillé. Merci ! :slight_smile:

Je met aussi la traduction pour que tous le monde puisse suivre :
I also put the translation so that everyone can follow:

thank you for this info, I made the modification on my program as you advised me. Unfortunately, this didn't solve the problem, but maybe it makes more sense than before, so I keep the modification you advised me. Thanks ! :slight_smile:

Hi Juan Antonio,

thanks for these informations.

I do not know if I understood well.

I don't think I can use your way of doing it, because I have to (it seems to me) attach a "$" to the start and a "*" to the end, so Arduino knows when to start and when to end the value I want ( I send it quite a few other values).

In your Arduino code, "Bluetooth.setTimeout(100);" is very interesting! I didn't know that. I tried in my code, but the problem is still there. Thanks anyway, maybe I'll use it a day !

Yes I had also thought of using a button to send the text, that might have solved the problem. But the addition of this button did not suit me.

On the other hand, I would have liked very much that when I type the value, AI2 does not send anything, and when I click on the button of the Android keyboard "validated" it sends the value.

Because for example when I type 6584, there are 4 values sent (6-65-658-6584), and the same when I delete and when I retype a value.

Can AI2 detect hitting the "send/submit" button on Android keyboard?

Thanks to you !:slight_smile: