# Command and printout via Bluetooth

**URL:** <https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529>\
**Category:** Programming\
**Created:** [May 4, 2026, 7:11am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529 "2026-05-04T07:11:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wheelie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wheelie/32/1129471_2.png) [@wheelie](https://forum.arduino.cc/u/wheelie)\
**Post date:** [May 4, 2026, 7:11am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529/1 "2026-05-04T07:11:50Z")

</div>

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?

```arduino
#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 = '-';

}

```

 ![cExekY](https://europe1.discourse-cdn.com/arduino/original/4X/3/1/e/31e7f58fe1756da1c127c382a90ea1e399c4f732.jpeg)  
........

 ![sKepZf](https://europe1.discourse-cdn.com/arduino/original/4X/b/4/1/b41270903d047a2c63d688b345535b5d6b68f592.jpeg)

---

<div class="post-metadata">

**Author:** ![wheelie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wheelie/32/1129471_2.png) [@wheelie](https://forum.arduino.cc/u/wheelie)\
**Post date:** [May 4, 2026, 7:29am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529/2 "2026-05-04T07:29:56Z")

</div>

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?

```arduino
#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); 
}
...

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 4, 2026, 7:38am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529/3 "2026-05-04T07:38:02Z")

</div>

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

```cpp
    char data_b = '';

```

so that it is given a specific value

---

<div class="post-metadata">

**Author:** ![wheelie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wheelie/32/1129471_2.png) [@wheelie](https://forum.arduino.cc/u/wheelie)\
**Post date:** [May 4, 2026, 7:42am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529/4 "2026-05-04T07:42:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 31, 2026, 7:43am UTC](https://forum.arduino.cc/t/command-and-printout-via-bluetooth/1442529/5 "2026-10-31T07:43:40Z")

</div>

Closed on October 31, 2026, 7:43am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
