Bluetooth command terminator code problem

Hello good morning.
Could you help me with my code? It has given me several errors, and the last error is this: "expected unqualified-id before 'if' " I attach my code below:

 [code]
#include <BluetoothSerial.h>
#include <EEPROM.h>     
#define EEPROM_SIZE 12

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


#define segnal 34  
int maximo;
int monedas = 0;  
char recibe;  
//String comando;  
int sw = 0;  
int pin_entrada = 5;           
//int monedas;
char envia;  

BluetoothSerial SerialBT;   


void setup() {
 
 Serial.begin(9600); // Inicialización de la conexión en serie para la depuración  
  SerialBT.begin("ESP32test"); //Nombre de dispositivo Bluetooth
  
  EEPROM.begin(EEPROM_SIZE);
  Serial.println("Ya puedes conectar");  

  pinMode(segnal, INPUT);
  pinMode(pin_entrada, INPUT);
  
  monedas = EEPROM.read(1);  
  
  Serial.print("Monedas:"); 
  Serial.println(monedas);
  delay(1000); 
}

void loop() {

 maximo = obtenvoltaje(); 

if (maximo > 10) {  
 Serial.print("Maximo:");
    Serial.println(maximo);


    
    monedas++; 
    Serial.print("Monedas:");
    Serial.println(monedas);
    EEPROM.write(1, monedas); 
    EEPROM.commit();
  }
}

int obtenvoltaje() 
{
  
  int sensorValue;             
  int sensorMax = 0;
  
  uint32_t start_time = millis(); 

  
  while((millis()-start_time) < 500)
  {
    sensorValue = analogRead(segnal); 
    if (sensorValue > sensorMax)   
    {
     
      sensorMax = sensorValue;
    }
  }
  return sensorMax;
  
}
if (SerialBT.available()) {    //:point_left: Here mark the last error
     recibe = SerialBT.read(); 
     if (recibe == '#'){   
       sw= 1;   
     }

       Serial.println("recibe");
     } else {
       Serial.println("no llego#");
     }
  //while (SerialBT.available()) {
      //char recibe = (char)SerialBT.read();
      //comando += recibe;

  //}
  if (digitalRead (pin_entrada) == HIGH && maximo > 10)
  {

    monedas = monedas + 1;
    //Serial.print("Monedas: ");
    //Serial.println(monedas);
    EEPROM.write(1, monedas);
    EEPROM.commit();
    //delay(500);
  } 

  envia = monedas;
    envia = SerialBT.write(monedas);

    if (recibe == '#'){
      Serial.print("Cantidad:");
      Serial.println(envia);
      
    }else {
      Serial.print("Cantidad:");
      Serial.println("0");
    }
    
    if (Serial.available()) {
    SerialBT.write(Serial.read());
    }
    if (SerialBT.available()) {
    Serial.write(SerialBT.read());
    }
    delay(2000);  
  }

[/code]
 

I've tried to figure it out myself, but I'm just starting out and still lack expertise, so I'm turning to you guys with more experience.

Thank you very much in advance.

This doesn't seem to be in a function

It would have saved time if you'd posted the actual error message

Thank you very much, Semperidem.

I already rectified the code and it doesn't give me any error.
What's happening is that it doesn't print anything to the serial monitor, and it's supposed to. That is, the loop is not being executed as expected, here is the code:

[code]
#include <BluetoothSerial.h>
#include <EEPROM.h>     
#define EEPROM_SIZE 12

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


#define segnal 34  
int maximo;
int monedas = 0;  
char recibe;  
//String comando;  
int sw = 0;  
int pin_entrada = 5;           
//int monedas;
char envia;  


BluetoothSerial SerialBT;   



void setup() {
 
 Serial.begin(9600); // Inicialización de la conexión en serie para la depuración  
  SerialBT.begin("ESP32test"); //Nombre de dispositivo Bluetooth
  
  EEPROM.begin(EEPROM_SIZE);
  Serial.println("Ya puedes conectar");  

  pinMode(segnal, INPUT);
  pinMode(pin_entrada, INPUT);
  
  monedas = EEPROM.read(1);  
  
  Serial.print("Monedas:"); 
  Serial.println(monedas);
  delay(1000); 

  }

void loop() {

 maximo = obtenvoltaje(); 


  
  if (digitalRead (pin_entrada) == HIGH && maximo > 10){  
 //Serial.print("Maximo:");
    //Serial.println(maximo);


    
    monedas++; 
    //Serial.print("Monedas:");
    //Serial.println(monedas);
    EEPROM.write(1, monedas); 
    EEPROM.commit();

    envia = monedas;
    envia = SerialBT.write(monedas);
  }
}

int obtenvoltaje() 
{
  
  int sensorValue;             
  int sensorMax = 0;
  
  uint32_t start_time = millis(); 

  
  while((millis()-start_time) < 500)
  {
    sensorValue = analogRead(segnal); 
    if (sensorValue > sensorMax)   
    {
     
      sensorMax = sensorValue;
    }
  }
  return sensorMax;
  

if (SerialBT.available()) {
     recibe = SerialBT.read(); 
     if (recibe == '#'){   
       sw= 1;   
     }

       Serial.println("recibe");
     } else {
       Serial.println("no llego#");
     }
  //while (SerialBT.available()) {
      //char recibe = (char)SerialBT.read();
      //comando += recibe;

  
  /*if (digitalRead (pin_entrada) == HIGH);
  {

     envia = monedas;
    envia = SerialBT.write(monedas);
  }*/

    if (recibe == '#'){
      Serial.print("Cantidad:");
      Serial.println(envia);
      
    }else {
      Serial.print("Cantidad:");
      Serial.println("0");
    }
    
    if (Serial.available()) {
    SerialBT.write(Serial.read());
    }
    if (SerialBT.available()) {
    Serial.write(SerialBT.read());
    }
    delay(2000);  
  }

[/code]

I hope I can count with your help.
Thank you very much in advance.

You've got an unconditional return there.
Nothing after it will be executed.

Do you see that in the serial monitor?

No, SemperIdem, " Ya puedes conectar" is not seen in the serial monitor.

That's a fairly unusual line speed

Yes, I see it, thanks. Do you recommend that I place it at the end of the code?

I just took another look, I wasn't familiar with that library and it seems it does take a String.

Whatever.

Try putting the Serial print right after the Serial begin, and then put a Serial.flush after that.

Of course, I'm going to try it right away. thank you so much.

Hello, I followed your instructions and the problem could not be solved, it seems that there are several errors, but I have not found them. It's supposed to print "Ya puedes conectar" as the first thing, and it doesn't. I thank you if you call my attention to something that I have not seen yet. Thank you very much in advance.

Except the one about posting the entire error message...

Hi aarg, I understand your suggestion, but it turns out that there are no compile errors. It's just that the code isn't executing the commands correctly. Thank you so much.

If you made changes that eliminate the errors you posted about before, please post the new code in a new message. Helpers need up to date information.

yes right away

Have you tried running any BT library example sketches? That would eliminate any software mistakes you made...

Did you confirm the baud rate settings in both the sketch and the serial monitor (or terminal emulator, or app)?

Hi!
I already rectified the code and it doesn't give me any error.
What's happening is that it doesn't print anything to the serial monitor, and it's supposed to. It doesn't even print: "Ya puedes conectar". Here is the code:

[code]
#include <BluetoothSerial.h>
#include <EEPROM.h>     
#define EEPROM_SIZE 12

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


#define segnal 34  
int maximo;
int monedas = 0;  
char recibe;  
//String comando;  
int sw = 0;  
int pin_entrada = 5;           
//int monedas;
char envia;  


BluetoothSerial SerialBT;   



void setup() {
 
 Serial.begin(9600);
 Serial.println("Ya puedes conectar");
 Serial.flush();
 SerialBT.begin("ESP32test");  
  
 EEPROM.begin(EEPROM_SIZE);
 
    

  pinMode(segnal, INPUT);
  pinMode(pin_entrada, INPUT);
  
  monedas = EEPROM.read(1);  
  
  //Serial.print("Monedas:"); 
  //Serial.println(monedas);
  //delay(1000); 
Serial.println("Ya puedes conectar");
  }

void loop() {
  

 maximo = obtenvoltaje(); 


  
  if (digitalRead (pin_entrada) == HIGH && maximo > 10){  
 //Serial.print("Maximo:");
    //Serial.println(maximo);


    
    monedas++; 
    Serial.print("Monedas:");
    Serial.println(monedas);
    EEPROM.write(1, monedas); 
    EEPROM.commit();

    envia = monedas;
    envia = SerialBT.write(monedas);
  }
}

int obtenvoltaje() 
{
  
  int sensorValue;             
  int sensorMax = 0;
  
  uint32_t start_time = millis(); 

  
  while((millis()-start_time) < 500)
  {
    sensorValue = analogRead(segnal); 
    if (sensorValue > sensorMax)   
    {
     
      sensorMax = sensorValue;
    }
  }
  
  return sensorMax;

if (SerialBT.available()) {
     recibe = SerialBT.read(); 
     if (recibe == '#'){   
       sw= 1;   
     }

       Serial.println("recibe");
     } else {
       Serial.println("no llego#");
     }
  //while (SerialBT.available()) {
      //char recibe = (char)SerialBT.read();
      //comando += recibe;

  
  /*if (digitalRead (pin_entrada) == HIGH);
  {

     envia = monedas;
    envia = SerialBT.write(monedas);
  }*/

    if (recibe == '#'){
      Serial.print("Cantidad:");
      Serial.println(envia);
      
    }else {
      Serial.print("Cantidad:");
      Serial.println("0");
    }
    
    if (Serial.available()) {
    SerialBT.write(Serial.read());
    }
    if (SerialBT.available()) {
    Serial.write(SerialBT.read());
    }
    delay(2000);  
  
 
  }
[/code]

Thank you very much for your attention and patience, and I look forward to your wise suggestions.

It would be wise to respond to the two questions I just asked...

Considering the symptom you report, the example sketch to try, should come from the basic Serial examples ("04:communications").

Or, just write a simple test sketch that only prints, "hello world". Oddly, they did not include one. :slight_smile:

Of course, here I go:

Question 1: Have you tried running any BT library example sketches?
Answer: No, I have not tried.

Question 2: Did you confirm the baud rate settings in both the sketch and the serial monitor (or terminal emulator, or app)?
Answer: Yes, I did (9600 in both).

Seems like there is some lag in communications going on here. Anyway, try a simple Serial sketch that just prints something. Report back. Post the test sketch and the results.

Here, I'll do it for you...

void setup() { 
 while (!Serial); // be sure serial is running
 Serial.begin(9600);
 Serial.println("Ya puedes conectar");
}
void loop() {
 Serial.println("Yadda");
 delay(1000);
}

Yes. I'm not clear why the initial esp32 output is not buffered so that a later connection picks it up, but if you do not use a delay at startup or the
while (!Serial) the early output is lost.