Bluetooth Communication

Hello i am new here :)

I am a little experienced with programming and elctronics.

i hope you can help me with my project. I want to set up a simple communication with my tablet and my arduino via bluetooth.

It is working so far, my tablet finds the blutooth module can send and recieve commands and answeres to and from the arduino.

My goal is to simply turn on and off a led with String commands.

When i send the commands via usb-serial to the arduino its working well, but when i send the same command via bluetooth the code is not working.

Here is my code:

String command;
char input;
boolean led;
// variable to receive data from the serial port
int ledpin = 8; // LED connected to pin 48 (on-board LED)
void setup() {
  pinMode(ledpin, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  Serial.begin(9600);       // start serial communication at 9600bps
}
void loop() {
  readCommand();
  if(command.equals(".")){
    led=true;
  }
  else if(command.equals("-")){
    led=false;
  }
  Serial.print("Arduino is now executing command: ");
  Serial.println(command);
  
  if(led==true){
    digitalWrite(ledpin,HIGH);
  }
  else if(led==false){
    digitalWrite(ledpin,LOW);
  }
  Serial.println("Command Executed");
}

void readCommand(){
  command="";
  while(Serial.available()==0){}
  Serial.flush();
  command="";
  while(Serial.available()==0){}
  while(Serial.available()>0){
    input=Serial.read();
    command+=String(input);
    delay(1); 
  }
  Serial.print("Arduino revieved command: ");
  Serial.println(command);
}

When i send the commande via usb serial i get the output: Arduino revieved command: . Arduino is now executing command: . Command Executed

Arduino revieved command: - Arduino is now executing command: - Command Executed

I get the same output when i send the command via bluetooth but the led doesnt turn on.

I am happy for every suggestion

Ok i solved the problem,

i used Integer based commands instead of String based.

kann somebody tell me why this Stringcomparison Operiation is wrong in my code above?

if(command.equals("compareString"){
     //do somthing
}

A few things occur to me about your code (in no particular order).

Serial.flush() does nothing with incoming data. It's purpose is to cause a program to wait until the transmit buffer has emptied. It is poorly named.

while(Serial.available()==0){} is (literally) a waste of time as it will wait indefinitely if there is NO data in the buffer because there is no code to empty the buffer. It is sufficient to check for Serial.available() > 0

The use of C Strings (with a capital S) is generally a bad idea on an Arduino as it has very limited memory. You should normally used strings (with a small s) which are null terminated character arrays. I am not familiar with Strings.

In your case you seem to be only reading a single character so you don't even need a string. Just use a char variable.

You seem to be using the exact same code for bluetooth as well as for the wired Serial connection. That implies that your bluetooth device is connected to pins 0 and 1. If so, how is the Arduino powered? If it is powered from the USB connection I suspect your problem is trying to send two streams of data onto the same pins - which won't work

...R

Edit to correct my comment on Serial.available == 0 - I had the logic backwards in my mind. ...R

Robin2: while(Serial.available()==0){} is (literally) a waste of time as it will wait indefinitely if there is data in the buffer because there is no code to empty the buffer.

I think it will spin until there is data available to read. I don't know if that's what is wanted here, but it could be useful in some situations.

PeterH: I think it will spin until there is data available to read.

Thanks for spotting that. I have corrected my post.

...R