BLE to PWM-output programming help

Hello, I'm completely new to the arduino world. I have a little project and i want so use BLE to controll a few things. I want to send the numbers 1 to 4 from my phone to the arduino and for every number, the arduino is supposed to put out a PWM signal on one specific output. I have zero knowlege about BLE an tried to write some code by looking up code from others, who did something similar. Here is my code:

#include <ArduinoBLE.h>

#define PIN1 3
#define PIN2 5
#define PIN3 6
#define PIN4 9

#define DAUER_AUSGABE 1500

int command;

BLEService controlService("1234");
BLEByteCharacteristic controlChar("5678",BLEWrite | BLERead);

bool isConnected = false;

void setup() 
{
  Serial.begin(9600);

  pinMode(PIN1, OUTPUT);
  pinMode(PIN2, OUTPUT);
  pinMode(PIN3, OUTPUT);
  pinMode(PIN4, OUTPUT);

  if (!BLE.begin())
  {
    Serial.println("BLE initialisieren fehlgeschlagen");
    while (1);
  }
  
  BLE.setDeviceName("Test");

  controlService.addCharacteristic(controlChar);
  BLE.addService(controlService);

  BLE.advertise();
  Serial.println("Warte auf BLE-Verbindung...");

}

void loop()
{
  BLE.poll();
  
  if (BLE.connected()) 
  {
    if (!isConnected) 
    {
      Serial.println("Gerät ist verbunden!");
      isConnected = true;
    }
  } 
  else 
  {
    isConnected = false;
  }

  if (BLE.connected())
  {
    command = Serial.read();
  
    Serial.print("Empfangene Zahl:");
    Serial.println(command);
  }
      
  switch (command)
  {
    case 1:
      analogWrite(PIN1, 255);
      Serial.println("Pin 1 HIGH");
      delay(DAUER_AUSGABE);
      analogWrite(PIN1, 0);
      Serial.println("Pin 1 LOW");
      break;

    case 2:
      analogWrite(PIN2, 255);
      Serial.println("Pin 2 HIGH");
      delay(DAUER_AUSGABE);
      analogWrite(PIN2, 0);
      Serial.println("Pin 2 LOW");
      break;

    case 3:
      analogWrite(PIN3, 255);
      Serial.println("Pin 3 HIGH");
      delay(DAUER_AUSGABE);
      analogWrite(PIN3, 0);
      Serial.println("Pin 3 LOW");
      break;

    case 4:
      analogWrite(PIN4, 255);
      Serial.println("Pin 4 HIGH");
      delay(DAUER_AUSGABE);
      analogWrite(PIN4, 0);
      Serial.println("Pin 4 LOW");
      break;
  }
} 

The connection with my phone works fine, but as soon as I'm connected, the serial monitor shows that the arduino receives the number "-1" infinite times. If i try to send a number from my phone it doesen't show up on the serial monitor and i also can't measure a current output.

I tried the same using the serial monitor as a control unit and everything works fine. So please help me get this right using BLE.

I can't really help you with the BLE stuff

That will read from the serial port, not from BLE. So you need to send data via the serial monitor which is more than likely not what you want.

Showing my ignorance with BLE, is there something like BLE.read()? You will also have to check if there is indeed data available before reading it.

Look at the LED example that comes with the ArduinoBLE library for how to read/write a characteristic.

Thanks for the heads up about the Serial.read(). How can I check if there's data available before trying to read it?

I've already read alot of examples, but i'm not really getting smart out of it

For Serial, you will need to look at https://docs.arduino.cc/language-reference/en/functions/communication/serial/available/. This will not help you with BLE.

Thanks for your tips, i managed to figure it out. There are still some minor problems, but it's doing what it's soppused to do. Here is the code, so somone who wants to programm a similar application can look at my results.

#include <ArduinoBLE.h>

#define PIN1 3
#define PIN2 5
#define PIN3 6
#define PIN4 9

#define DAUER_AUSGABE 1500

BLEService myService("1234");
BLECharacteristic rxCharacteristic("5678", BLEWrite | BLEWriteWithoutResponse | BLERead, 20);

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!BLE.begin()) {
    Serial.println("Failed to initialize BLE!");
    while (1);
  }

  BLE.setLocalName("ESP32-BLE-Receiver");
  BLE.setDeviceName("Test");
  BLE.setAdvertisedService(myService);
  myService.addCharacteristic(rxCharacteristic);
  BLE.addService(myService);

  BLE.advertise();
  Serial.println("BLE Ready.");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to: ");
    Serial.println(central.address());

    while (central.connected()) {
      if (rxCharacteristic.written()) { 
        int len = rxCharacteristic.valueLength(); 
        char buffer[len + 1]; 
        memcpy(buffer, rxCharacteristic.value(), len);
        buffer[len] = '\0';

        String receivedData = String(buffer);
        Serial.print("Received: ");
        Serial.println(receivedData);

        int command = receivedData.toInt();

        switch (command)
        {
          case 1:
            analogWrite(PIN1, 255);
            Serial.println("Pin 1 HIGH");
            delay(DAUER_AUSGABE);
            analogWrite(PIN1, 0);
            Serial.println("Pin 1 LOW");
            break;

          case 2:
           analogWrite(PIN2, 255);
           Serial.println("Pin 2 HIGH");
           delay(DAUER_AUSGABE);
           analogWrite(PIN2, 0);
           Serial.println("Pin 2 LOW");
           break;

          case 3:
            analogWrite(PIN3, 255);
            Serial.println("Pin 3 HIGH");
            delay(DAUER_AUSGABE);
            analogWrite(PIN3, 0);
            Serial.println("Pin 3 LOW");
            break;

         case 4:
           analogWrite(PIN4, 255);
           Serial.println("Pin 4 HIGH");
           delay(DAUER_AUSGABE);
           analogWrite(PIN4, 0);
           Serial.println("Pin 4 LOW");
           break;

          default:
            Serial.println("Ungültige Zahl empfangen.");
            break;
        }
      }
    }
    Serial.println("Disconnected.");
  }
}

I'm glad you got it working. As a side note, you take a very long way around to get to your switch statement. You receive a string like "3", copy it to buffer, convert it to a String object, convert it to an integer and then switch on that.

You could simplify that by just printing out buffer and then switch on the first element of buffer and change your case statements to

case '1':
...
case '2':
...

since there is no need to convert a single digit to a number as your case values can be a character.

Thank you for this information, I corrected it now.