# Print function doesn't work

**URL:** <https://forum.arduino.cc/t/print-function-doesnt-work/1233928>\
**Category:** Programming\
**Created:** [March 10, 2024, 8:32pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928 "2024-03-10T20:32:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![CristyRO9](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@CristyRO9](https://forum.arduino.cc/u/CristyRO9)\
**Post date:** [March 10, 2024, 8:32pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/1 "2024-03-10T20:32:26Z")

</div>

Hi! I have an ESP32 board, and I use Bluetooth to communicate with the Serial Bluetooth Terminal on my phone, in my code I need to print a message but using the SerialBT.print/SerialBT.println function doesn't print anything, the code is working and is doing what it should but it does not print any message...

```arduino
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

int FlightTime = 0;

int step = 1;

int onetime = 0;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32");

}

void ProgramMode() {
  
  while(step == 1) {

    if(onetime == 0) {
      SerialBT.print("1.Flight Time Programming");
      onetime = 1;}

    if(SerialBT.available()) {
      char FlightTime = SerialBT.read();
      SerialBT.write(FlightTime);}}

}

void loop() {
  
  ProgramMode();

}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [March 10, 2024, 8:46pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/2 "2024-03-10T20:46:40Z")

</div>

> [@CristyRO9](#):
>
> anything

What are you expecting to be "printed" and where?

---

<div class="post-metadata">

**Author:** ![CristyRO9](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@CristyRO9](https://forum.arduino.cc/u/CristyRO9)\
**Post date:** [March 10, 2024, 8:58pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/3 "2024-03-10T20:58:34Z")

</div>

This

> [@CristyRO9](#):
>
> `SerialBT.print("1.Flight Time Programming");`

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [March 10, 2024, 9:07pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/4 "2024-03-10T21:07:36Z")

</div>

> [@CristyRO9](#):
>
> Serial Bluetooth Terminal on my phone

Is your phone connected/paired to the BT device?

---

<div class="post-metadata">

**Author:** ![CristyRO9](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@CristyRO9](https://forum.arduino.cc/u/CristyRO9)\
**Post date:** [March 11, 2024, 6:04am UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/5 "2024-03-11T06:04:50Z")

</div>

Yes I can see the output of the "FlightTime"

---

<div class="post-metadata">

**Author:** ![asdfdsgtegb](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@asdfdsgtegb](https://forum.arduino.cc/u/asdfdsgtegb)\
**Post date:** [March 11, 2024, 7:11am UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/6 "2024-03-11T07:11:31Z")

</div>

> [@CristyRO9](#):
>
> ```arduino
> while(step == 1) {
> 
> ```

This is the problem.

Arduino framework relies on handling some background work like serial monitors and comminucations after each loop is done. Your program doesn't allow the loop to finish even once leading to the serial protocol not being handled properly.

```arduino
void loop()
{
   if(onetime == 0) {
      SerialBT.print("1.Flight Time Programming");
      onetime = 1;}

    if(SerialBT.available()) {
      char FlightTime = SerialBT.read();
      SerialBT.write(FlightTime);}}
}

```

use void loop as mentioned above or delete the while loop and continue with a non-blocking function. void loop() repeates infinitely no need for another while.

---

<div class="post-metadata">

**Author:** ![CristyRO9](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@CristyRO9](https://forum.arduino.cc/u/CristyRO9)\
**Post date:** [March 11, 2024, 12:12pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/7 "2024-03-11T12:12:20Z")

</div>

but I will have more cases, like step == 1 , step == 2 and so on till 4

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 11, 2024, 1:32pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/8 "2024-03-11T13:32:13Z")

</div>

> [@CristyRO9](#):
>
> more cases

You can _if_/_else_ your way through all that, but since you mentioned cases, you might benefit from using the C/C++ _switch_/_case_ statement.

[https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/](https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/)

a7

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [March 11, 2024, 4:09pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/9 "2024-03-11T16:09:52Z")

</div>

> [@asdfdsgtegb](#):
>
> Arduino framework relies on handling some background work like serial monitors and comminucations after each loop is done. Your program doesn't allow the loop to finish even once leading to the serial protocol not being handled properly.

Serial I/O is handled entirely by interrupts. You can spend ALL your time in loop and Serial monitor I/O will still work just fine. BlueTooth _may_ behave differently, depending on exactly which BT module and board is being used, and how they are connected.

---

<div class="post-metadata">

**Author:** ![CristyRO9](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@CristyRO9](https://forum.arduino.cc/u/CristyRO9)\
**Post date:** [March 11, 2024, 5:50pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/10 "2024-03-11T17:50:46Z")

</div>

The board is a Wemos Loli32

---

<div class="post-metadata">

**Author:** ![asdfdsgtegb](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@asdfdsgtegb](https://forum.arduino.cc/u/asdfdsgtegb)\
**Post date:** [March 12, 2024, 7:40am UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/11 "2024-03-12T07:40:57Z")

</div>

> [@RayLivingston](#):
>
> Serial I/O is handled entirely by interrupts.

Oh mb, I mixed it up with serialEventRun() for a moment... Sorry for the inconvenience.

> [@CristyRO9](#):
>
> but I will have more cases, like step == 1 , step == 2 and so on till 4

yeah just use switch case.

> [@CristyRO9](#):
>
> Yes I can see the output of the "FlightTime"

Wait so you're able to see the values and not the start text? That could be caused due to your serial monitor not displaying first packet that arrives. (happened to me on rp2040 and vscode terminal) If so, you might wanna send a single garbage packet at setup like helloWorld and then you might be able to see the "1.Flight Time Programming"

---

<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:** [September 8, 2024, 1:47pm UTC](https://forum.arduino.cc/t/print-function-doesnt-work/1233928/13 "2024-09-08T13:47:28Z")

</div>

Closed on September 8, 2024, 1:47pm UTC

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