Command and printout via Bluetooth

Hi, thanks for all help in the past.

I connected a HC-06 Bluetooth card to an Arduino Uno R3 (voltage divider for signal to HC-06). I want commands and program output via Bluetooth.

I now send individual characters as commands via the free app Bluetooth Graphics. If I send characters one by one from the phone the program acts as I expect, it repeats the character back to the phone. If I send more characters only the first comes out right.

I also added a case-statement to interpret the commands sent from the phone. If either of the allowed commands 'P' or 'p' is detected, a procedure will repeat "****" and the character 10 times. I did this to see better if execution speed goes up.

But after adding the case statement, if I enter an allowed command, the program just runs wild and repeats the printout of *****. I can stop this by letting the allowed command be followed by a random letter. 'P' will make the program run wild, "PX" will not and is interpreted as P.

How do I get some structure in this so that whatever characters I enter will be interpreted and not repeated?

#include <SoftwareSerial.h>
SoftwareSerial bluetoothSerial(9, 10);

void setup() {
  bluetoothSerial.begin(9600);  // Initialize software serial for Bluetooth module

  pinMode(LED_BUILTIN, OUTPUT);  // Initialize pin 13 as output
  digitalWrite(LED_BUILTIN, LOW); 
}


void print_repeat(char c) {

  for (int itmp = 1; itmp <= 10; itmp++) {
    bluetoothSerial.print("*************** >");
    bluetoothSerial.print(c);
    bluetoothSerial.println("<");
  }

}


void loop() {

  char data_b;

  if (bluetoothSerial.available()) {
    data_b = bluetoothSerial.read();
    bluetoothSerial.write(data_b);
  }

  switch (data_b) {
    case 'P':
      digitalWrite(LED_BUILTIN, HIGH); 
      print_repeat('P');
      break;
    case 'p':
      digitalWrite(LED_BUILTIN, LOW); 
      print_repeat('p');
      break;
  }
data_b = '-';

}


........

I apologize for a simple error. I now moved the type statement of data_b to global and the program does not run wild. Is this the solution to the whole problem or do you have more comments?

#include <SoftwareSerial.h>
SoftwareSerial bluetoothSerial(9, 10);

  char data_b;


void setup() {
  bluetoothSerial.begin(9600);  // Initialize software serial for Bluetooth module

  pinMode(LED_BUILTIN, OUTPUT);  // Initialize pin 13 as output
  digitalWrite(LED_BUILTIN, LOW); 
}
...

In your original sketch, when you declare data_b you do not initiialise it so its value will be taken from wherever in memory it is stored. In practice it is likely that it uses the same memory location every time you declare it so it will inherit the last value stored, hence your problem.

Try

    char data_b = '';

so that it is given a specific value

You're absolutely right. I had a similar statement at the end of the original code, like in the first post. But putting it at the start of loop() is better. Or making a global like I just did. ... or static
Thanks.