Send data by Bluetooth with a push Button

Hello,

I have an Arduino Nano with a push button, a led, a buzzer and a bluetooth module (https://hackspark.fr/fr/hc-05-bluetooth-v2-0-breakout-board-host-and-slave-3-6-6v.html).

I want to activate the led and the buzzer when I push the button, and at the same time, send a message with the bluetooth.

Unfortunately, when I push the button, the message is not sent. I think that the bluetooth is unabled when the led and the buzzer run, maybe because of power limits. On the other side, the message is sent when I don’t push the button (see the test “I am a test” in the code below).

Here is my code :

#include <SoftwareSerial.h>

const int buzzer1 = 2;
const int buzzer2 = 3;
const int led = 4;
const int button = 5;
int buttonState = 0;
int buttonLastState = 0;

// Declaration of bluetooth RX and TX
SoftwareSerial Bluetooth(10, 11);

void setup()
{
  pinMode(led, OUTPUT);
  pinMode(buzzer1,OUTPUT);
  pinMode(buzzer2,OUTPUT);
  pinMode(button, INPUT);

  Bluetooth.begin(9600);
}

void loop()
{
  buttonState = digitalRead(button);

  // Send text by bluetooth
  Bluetooth.println("I am a test !");
  bluetoothFunc(buttonState);

  // Activate led and buzzer
  ring(buttonState);
}

void bluetoothFunc(int buttonState)
{
  if (Bluetooth.available())

  if (buttonLastState != buttonState) {
    if (buttonState == 1) {
      Bluetooth.write('2');
      buttonLastState = buttonState;
    }
  }
}

void ring(int buttonState)
{
  digitalWrite(buzzer2, LOW);

  if (buttonState == HIGH) {
    digitalWrite(led, HIGH);
    digitalWrite(buzzer1, HIGH);
    digitalWrite(buzzer2, HIGH);
    delay(40);

    digitalWrite(led, LOW);
    digitalWrite(buzzer1, LOW);
    delay(40);
  }
  else {
    digitalWrite(led, LOW);
    digitalWrite(buzzer1, LOW);
    digitalWrite(buzzer2, LOW);
  } 
}
void bluetoothFunc(int buttonState)
{
  if (Bluetooth.available())

Why does sending data depend on there being data to read?

PaulS:

void bluetoothFunc(int buttonState)

{
  if (Bluetooth.available())



Why does sending data depend on there being data to read?

I thought that it was to check if bluetooth module was running. But even when I remove it, there is the same problem.

When I push the button, the led on the bluetooth module turns off.