Question about variables

Im new with arduino and wanted to make a mini bluetooth car. I am using the arduino uno r3 and a HC 05 module for the bluetooth communication.

#include <SoftwareSerial.h>
SoftwareSerial BL(2, 13); // RX | TX
char state;

void forward(){
  Serial.println("GOING FORWARD");
}


void backward(){
  Serial.println("GOING BACKWARDS");
}

void setup()
{
 
  Serial.begin(9600);
  BL.begin(9600);  //Default Baud for comm, it may be different for your Module. 
  Serial.println("The bluetooth gates are open.\n Connect to HC-05 from any other bluetooth device with 1234 as pairing key!.");
}
 
void loop()
{
 
  // Feed any data from bluetooth to Terminal.
  //if (BL.available())
    //Serial.write(BL.read());

    
  if (BL.available()>0){
      Serial.write(BL.read());
      char state = (Serial.read());
      if (state == 'f'){
        forward();}
        
      if (state == 'b'){
        backward();
      }
      }

  // Feed all data from termial to bluetooth
    /*if (Serial.available()){
    BL.write(Serial.read());
    state = Serial.read();
    Serial.println("STATE:");
    Serial.print(state);
    }*/
    
delay(10);

    
}

On my bluetooth terminal (I use an app on my phone) i sent the letter f and it successfully printed on the serial monitor, however the if statement was not executed. I tried switching the pins on the software serial to 0 and 1 like this : SoftwareSerial BL(0, 1); // RX | TX And suddenly the if statement was executed.
This confused me, so i tried printing the variable "state". If i used 0 and 1 as rx and tx, i would get f. If i used any other pins instead, i would get a question mark.
I dont understand why this happens, sorry if its a noob question. :o

if (BL.available()>0){
      Serial.write(BL.read());
      char state = (Serial.read());

Talk through what is going on here.

I think you meant to say:

  if (BL.available()>0){
      char state = BL.read();
      Serial.write(state);

WARNING: You are declaring a local variable named 'state' here. It will lose its value when this call to loop() ends. If you want to use the GLOBAL variable of the same name, remove the 'char' keyword in front of "state = BL.read();"

TheMemberFormerlyKnownAsAWOL:

if (BL.available()>0){

Serial.write(BL.read());
      char state = (Serial.read());


Talk through what is going on here.

If bluetooth is available, write what is received on the serial monitor. Char state i what is on the serial monitor. Sorry for a very slow response, i just could not find my post.

johnwasser:
I think you meant to say:

  if (BL.available()>0){

char state = BL.read();
      Serial.write(state);



WARNING: You are declaring a local variable named 'state' here. It will lose its value when this call to loop() ends. If you want to use the GLOBAL variable of the same name, remove the 'char' keyword in front of "state = BL.read();"

I have removed char now, so it is only outside the loop. Also, i tried changing it this way, but still wont work with pins other than tx0 and rx0.

I did make it work by removing the software library entirely and using a very simple code.

bt=Serial.read();

I did end up having to connect the communication pins to tx0 and rx0. Thank you for your help and sorry for my late response. :slight_smile:

Creations:
If bluetooth is available, write what is received on the serial monitor. Char state i what is on the serial monitor.

"Serial.read();" does NOT read what you just wrote to Serial Monitor. It reads what is in the buffer that someone typed into Serial Monitor. If there is nothing in the buffer, it returns -1.