Hi all,
I tried to control RGB LED on a Arduino mini pro with HC-05 BT module from my Android phone.
In my program, I continuously sending strings "255.255.255.255" to test my program codes from Android phone to HC-05 connected to a Arduino mini pro.
However, I found out Arduino seems not be able to receive the same strings all the time.
As you can see from the output below, some times the string is 1 char, sometime is 16 characters, sometimes only 8 characters.
What could be the problem? How do I get "255.255.255.255" all time time?
I searched the same topic on the Internet, but couldn't find a good answer for this.
Thanks for the help!
Here is the output:
Size of RGB=1
RGB: [ ]
Size of RGB=16
RGB: [255.255.255.255 ]
Size of RGB=1
RGB: [ ]
Size of RGB=25
RGB: [255.255.255.255 255.255.]
Size of RGB=8
RGB: [255.255]
Size of RGB=16
RGB: [255.255.255.255 ]
Size of RGB=1
RGB: [ ]
Size of RGB=16
RGB: [255.255.255.255 ]
However, I found out Arduino seems not be able to receive the same strings all the time.
Yes, it is. It just does not receive the whole string at once. It receives characters one at a time, slowly.
What could be the problem?
A lack of understanding of how serial data arrives.
How do I get "255.255.255.255" all time time?
Send reasonable data, including an end-of-packet marker. Read and store whatever data is available, until the end of packet marker arrives. Only then should you use the data you have received.
Send reasonable data, including an end-of-packet marker. Read and store whatever data is available, until the end of packet marker arrives. Only then should you use the data you have received.
Hi PaulS,
Thanks for the hint! Here is the logic that I had in my mind now...
char RevChar = ‘’;
String STR = “”;
while(RevChar != “\n” ){ //my 255.255.255.255 has a \n ending char.
if(BT.available(){
RevChar = char(BT.read());
STR += RevChar
}
}
Serial.println(STR);
//This should get the complete string 255.255.255.255
Hi All,
Here is the final code that works on my Arduino and HC-05.
My BT module can receive complete RGB code from my Android phone.
Just want to share this with you.
Thanks!
//Include libraries: SoftwareSerial & Wire
#include <SoftwareSerial.h>
#include <Wire.h>
//Define BT as SoftwareSerial and PIN11 & PIN12 as RX and TX pins
SoftwareSerial BT(11,12);
void setup() {
Serial.begin(9600); //Arduino serial port baud rate?9600
BT.begin(9600);//Taobao HC-05 module default baud rate.
//You have to check your BT board baud rate or it won't work
RGB.reserve(30); //Reserve 30 space for RGB code received
}
void loop(){
//Read each character from Serial Port(Bluetooth)
while(BT.available()){
char ReadChar = (char)BT.read();
// I use right parentheses ')' to indicate complete of the RGB string
if(ReadChar == ')'){
RGB_Completed = true;
}else{
RGB += ReadChar;
}
} // while BT has something to read
//Print out the result
Serial.print("RGB:");
Serial.println(RGB);
} //end of loop