A very well presented question and code.
Not being an expert I ask if You could try using a lower baudrate. For Serial monitor I've used 115200 but does Your periferal support 38400?
Receiving the wrong characters sounds like a baudrate mismatch, baudrate trouble, to me.
@wildbill?
Yes. Do You know the HC-05 Bluetooth module? I don't and I don't have the ambition to look up and read specs for every questioner, therefore my wague advice.
Using specs and "worst case" made my designs never fail during plenty of yeras.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
First check your Bluetooth modules' baud rate, you will need to use AT commands to check, normally HC-05 are set at 9600 and in your case they don't have same baud rate.
AT+UART? tells you the default baud rate
AT+UART=9600 sets the baud rate to 9600.
Moreover, try using Serial.print instead of Serial.write
Laplume:
Hello everybody , Thanks for you help.
I fixed my issue
jackthom41 was righ, it was a baud rate problem.
both of my BT module was at 9600.
I changed them to 38400 , and now it is working perfectly !!
Laplume
Good to know that it worked for you.
Btw if both modules were at 9600 then it must have worked unless you have added 38400 in the code. So, instead of changing the baud rate of your modules, if you have just changed the baud rate in code to 9600 then it would have worked too. I hope you got that.