XIAO BLE nRF BLE functionality question

I was tinkering with the BLE led example. I extended it for 3 channels for build in rgb led and now i want to introduce blinking as an option.

/*
LED

This example creates a Bluetooth® Low Energy peripheral with service that contains a
characteristic to control an LED.

The circuit:

  • Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
nRF Connect (Android), to interact with the services and characteristics
created in this sketch.

This example code is in the public domain.
*/

#include <ArduinoBLE.h>

BLEService ledService(“19B10000-E8F2-537E-4F6C-D104768A1214”); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic(“19B10001-E8F2-537E-4F6C-D104768A1214”, BLERead | BLEWrite);

const int ledBlue = 13; // pin to use for the LED
const int ledGreen = 12; // pin to use for the LED
const int ledRed = 11; // pin to use for the LED

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

// set LED pin to output mode
pinMode(ledRed, OUTPUT);
digitalWrite(ledRed, LOW); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(ledRed, HIGH); // turn the LED off by making the voltage LOW
delay(250);
pinMode(ledGreen, OUTPUT);
digitalWrite(ledGreen, LOW); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(ledGreen, HIGH); // turn the LED off by making the voltage LOW
delay(250);
pinMode(ledBlue, OUTPUT);
digitalWrite(ledBlue, LOW); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(ledBlue, HIGH); // turn the LED off by making the voltage LOW
delay(250);

// begin initialization
if (!BLE.begin()) {
Serial.println(“starting Bluetooth® Low Energy module failed!”);

while (1);

}

// set advertised local name and service UUID:
BLE.setLocalName(“Builtin LEDs”);
BLE.setAdvertisedService(ledService);

// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);

// add service
BLE.addService(ledService);

// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);

// start advertising
BLE.advertise();

Serial.println(“BLE LEDs Peripheral”);
}

void loop() {

// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central’s MAC address:
Serial.println(central.address());

// while the central is still connected to peripheral:
while (central.connected()) {
  // if the remote device wrote to the characteristic,
  // use the value to control the LEDs:
  if (switchCharacteristic.written()) {
    byte state = switchCharacteristic.value();
    byte blueBlink = state & 32;
    byte greenBlink = state & 16;
    byte redBlink = state & 8;
    byte blueState = state & 4;
    byte greenState = state & 2;
    byte redState = state & 1;
    
    if (blueState) {   
      Serial.println("Blue LED on");
      digitalWrite(ledBlue, LOW);         // will turn the LED on
    } else {                              // a 0 value
      Serial.println(F("Blue LED off"));
      digitalWrite(ledBlue, HIGH);          // will turn the LED off
    }        
    if (greenState) {   
      Serial.println("Green LED on");
      digitalWrite(ledGreen, LOW);         // will turn the LED on
    } else {                              // a 0 value
      Serial.println(F("Green LED off"));
      digitalWrite(ledGreen, HIGH);          // will turn the LED off
    }        
    if (redState) {   
      Serial.println("Red LED on");
      digitalWrite(ledRed, LOW);         // will turn the LED on
    } else {                              // a 0 value
      Serial.println(F("Red LED off"));
      digitalWrite(ledRed, HIGH);          // will turn the LED off
    }
  }
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());

}
}

How can I make a while loop that is interrupted on entering new value?
Also is there a way to secure BLE conection with pin or password?

I would put the Blinking in a Function and call it.
You may want to use the NOTIFY on the BLE side.
If i may , This tutorial is one of the best around. Bluetooth perfect sketch.
my .02
HTH
GL :wink:

Thanks, but that sketch is not that helpful for me. Library is not applicable on my board.

I’ll look about NOTIFY.