Problem with basic bluetooth sketch

Hi everyone, thanks a lot for reading this.

I wanted to replace a physical button in one of my projects for a bluetooth one (it’s the first time I work with bluetooth), so I implemented it using an HC-05 module.

I decided to make a simple sketch to try it on, it reads the serial port and turns on/off the led when it gets a ‘z’.

It is working fine now, but at first I tried to do the same when sending a string and I couldn’t make it work. Can you help me to find out what’s wrong? I guess it’s something about string comparisons.

Here’s the code that works:

char str;
const char BotonStr='z';
int cont=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  digitalWrite(LED_BUILTIN,HIGH);
  vaciarPuertoSerie(); //I empty the serial port in case someone pushed the button before I needed it
  if(Serial.available()>0) //I check if the serial port was correctly emptied
  {
    Serial.println("Error al vaciar el puerto serie");
  }else Serial.println("puerto serie vaciado");
}

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available()>0)str=Serial.read();//str=Serial.readStringUntil("\n");
 if(str=='z')botonPresionado();
}

void vaciarPuertoSerie()
{
  int N=Serial.available();
  for(int i=0;i<N;i++)
  {
    byte b=Serial.read();
  }
}

void botonPresionado()
{
  cont++;
  Serial.println(cont);//I print the counter in order to se if it bounces
  if(cont%2==1) digitalWrite(LED_BUILTIN,LOW);
  if(cont%2==0) digitalWrite(LED_BUILTIN,HIGH);
  delay(200); //works as a really simple debouncer
}

And here’s the code that doesn’t work:

String str;
const String BotonStr='aaabbb';
int cont=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  digitalWrite(LED_BUILTIN,HIGH);
  vaciarPuertoSerie(); //I empty the serial port in case someone pushed the button before I needed it
  if(Serial.available()>0) //I check if the serial port was correctly emptied
  {
    Serial.println("Error al vaciar el puerto serie");
  }else Serial.println("puerto serie vaciado");
}

void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available()>0)str=Serial.readStringUntil("\n");
 if(str=="aaabbb")botonPresionado();
}

void vaciarPuertoSerie()
{
  int N=Serial.available();
  for(int i=0;i<N;i++)
  {
    byte b=Serial.read();
  }
}

void botonPresionado()
{
  cont++;
  Serial.println(cont);//I print the counter in order to se if it bounces
  if(cont%2==1) digitalWrite(LED_BUILTIN,LOW);
  if(cont%2==0) digitalWrite(LED_BUILTIN,HIGH);
  delay(200); //works as a really simple debouncer
}

I tried using a few varations like str.equals instead of ==, or using the variable BotonStr for the comparison instead of “aaabbb”.

I really appreciate your comments, since I’m sure it’s a silly mistake and that I’ll feel really stupid when I get enlightened

Serial.readStringUntil("\n");

Serial.readStringUntil('\n**'**);

darrob:
Serial.readStringUntil("\n");

Serial.readStringUntil('\n**'**);

Tryed it, I'm afraid it didn't work :confused:

Okay

From your original post, I thought the problem was that you were not reading the string properly
That is, your program was not recognising "aaabbb"

I can see now that your post should have said that the program would not compile.
( My fault for not checking everything first before posting ::slight_smile: )

The error messages when you compile tell you what is going on.

test.ino:14:23: warning: character constant too long for its type

 const String BotonStr='aaabbb';

                       ^

The line const String BotonStr='aaabbb'; should be const String BotonStr="aaabbb";

With no other changes, the code will compile.
But it will not work until you make the other change I pointed out in my original reply.

darrob, thank you for taking the time for helping me.

I'm confused right now, because I can compile my code just fine. It's now working, though.
Anyway, see that I'm not using the variable BotonStr.

I tried both ways if(str=="aaabbb"), and if(str==BotonStr). I forgot to erase or comment the declaration of the variable when I used str=="aaabbb".
For the record, I also used if(str.equals("aaabbb")) and if(str.equals(BotonStr)). All of them compiled, none of them worked.

I also tried printing str to see what it was catching. It was reading the string fine, and I saw that the difference in using "\n" or '\n' was that, when I used '\n', it printed an empty line. However, in neither of them the string was recognized.

Brian_yn:
I also tried printing str to see what it was catching. It was reading the string fine, and I saw that the difference in using "\n" or '\n' was that, when I used '\n', it printed an empty line. However, in neither of them the string was recognized.

A lot of your problems can be overcome by knowing how to deal with serial input, and how to deal with strings.

First off, try and avoid using the String data type. It will lead to memory problems in the long term.
Use null terminated character arrays instead (also known as C strings)

Secondly, read this excellent tutorial on serial input basics.

Darrob, just read the tutorial. I't really clear, thank you a lot. The question is, it seems that the purpose of readStringUntil() is the same than recvWithEndMarker(); but much simpler since I don't need to write all the code.

So, I still don't know why my version is not working :s