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