0

I have a question about BT communication between Arduino and HC-05 module using Serial comm. I'm trying to control modes of RGB light on my Arduino nano through BT Terminal on my phone (in the future it will be Android APP) but I'm stuck where I need to check if the command sent is a letter or string with color. Below is the example:

I want to use mode where letter T is set up as a command so I have code:

    if (Serial.available()) {
    mode = Serial.read();

if( mode ==  'T')  {doSomething(); }

and it's working perfectly fine but now I wanted to send over BT RGB colour like 255,255,255 and then put it in the code

leds[i].r = redInt; 
leds[i].g = greenInt; 
leds[i].b = blueInt;

I've tried to save it as a string and then if it's not any of the modes, parse it to the int's ( redInt, greenInt, blueInt ) but I don't know how to do it. I tried with parseInt, but it's saying it won't work with a string. The question is how to save incoming Serial.read() as a String and after checking if it's not, the command how to parse it to the 3 separate int's to let me set up the color?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
revan
  • 17
  • 3

1 Answers1

0

HC-05/HC-06 takes the string like a sequence of character. If you want to send 255 then HC-05 takes it like '2','5','5'. So your first work is determining whether it is an integer or string. I have added the different prefix before the integer and the string and a common postfix('#') by which I can determine the end of the input. Hopefully, this helps you.

while (Serial.available()) {

delay(3);  //small delay to allow input buffer to fill

char c = Serial.read();  //gets one byte from serial buffer
if (c == '#') {
  break;
}  //breaks out of capture loop to print readstring
readString += c;
}

After taking all the bit now you have to figure out what you have taken with the help of the prefix.

if (readString.charAt(0) == 'i')
{
 //"i255,255,255#"
readString.replace("i", "0");
value1 = readString.substring(1, 3).toInt();
value2 = readString.substring(5, 7).toInt();
value3 = readString.substring(8, 11).toInt();

readString = "";

}
else if (readString.charAt(0) == 's')
{
 //Do as you wish
readString = "";
}