Writing an array to a ble characteristic

Hi there,
I have done some experimenting with sending rgb values of the nano ble sense in a ble characteristic.
That works, but one thing isn't clear to me. When I write 1 color I get a 2 byte input in the nRF connect app. Fine.
when I send 3 colours in an array, each colour is followed by 2 bytes of 0, so I need 12bytes in total. Is that suppposed to happen or can/should I improve something in the code?

/*
  APDS9960 - Color Sensor
  arduino IDE 2.3.3
  bord nano 33 ble sense

  This example reads Color data from the on-board APDS9960 sensor of the
  Nano 33 BLE Sense and prints the color RGB (red, green, blue) values
  to the Serial Monitor once a second.

  The circuit:
  - Arduino Nano 33 BLE Sense

  Dit is uitgebreid met de gelezen kleuren rgb van de sensor, die verpakt worden in 3 characteristics.
  in v3 voegen we descriptors toe.
  Alles werkt, maar het is beter de r, g en b waardes in één karakteristiek te verpakken. 
  Anders is het moeilijker 1 meting van de 3 waardes bij elkaar te houden.Dat gaan we in v4 proberen..
*/
#include <ArduinoBLE.h> 
#include <Arduino_APDS9960.h>
#define debug 1
//4354e243-14c1-45b6-85f7-19ca2b532a8e
BLEService ctService("e5af1698-6c69-46f9-9939-119402b28213");
BLECharacteristic R_Char("2b47f8bd-bb11-4f78-b30c-121ce883a540", BLERead | BLENotify, 2); 
BLECharacteristic G_Char("bbd659aa-1e72-430b-a9e2-705a6c33dd85", BLERead | BLENotify, 2);
BLECharacteristic B_Char("f63b15ba-c3d9-4aa9-a05d-5f8f4bb667ff", BLERead | BLENotify, 2);
BLECharacteristic RGB_Char("06f7c826-9eb6-43c6-863d-32de3795eff4", BLERead | BLENotify, 12);

BLEDescriptor R_Descriptor("4354e243-14c1-45b6-85f7-19ca2b532a8e", "red");
BLEDescriptor G_Descriptor("98938d73-20f8-4a71-83dc-c676e43d7ff5", "green");
BLEDescriptor B_Descriptor("9dcffb4c-4695-4664-8f08-2d6e4fc9c5ef", "blue");
BLEDescriptor RGB_Descriptor("c55f44e9-5e33-43c0-988a-7c5a011795d2", "rgb");

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

  pinMode(LED_BUILTIN, OUTPUT); 
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  BLE.setLocalName("ctMonitor");
  BLE.setAdvertisedService(ctService);
  ctService.addCharacteristic(R_Char);
  ctService.addCharacteristic(G_Char); 
  ctService.addCharacteristic(B_Char);
  ctService.addCharacteristic(RGB_Char);
  
  R_Char.addDescriptor(R_Descriptor);
  G_Char.addDescriptor(G_Descriptor);
  B_Char.addDescriptor(B_Descriptor);
  RGB_Char.addDescriptor(RGB_Descriptor);

  BLE.addService(ctService);
  BLE.advertise();
  Serial.println("Bluetooth® device active, waiting for connections...");

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS9960 sensor.");
  }
}  

void loop() {

  // check if a color reading is available
  while (!APDS.colorAvailable()) {
    delay(20);
  }
  // ctService.addCharacteristic(RGBLuxChar);
   int r, g, b;

  APDS.readColor(r, g, b);
#if debug
  Serial.print("Red light = ");
  Serial.println(r);
  Serial.print("Green light = ");
  Serial.println(g);
  Serial.print("Blue light = ");
  Serial.println(b);
  //Serial.print("Ambient light = ");
  //Serial.println(a);
#endif
  R_Char.writeValue((int16_t) r);
  G_Char.writeValue((int16_t) g);
  B_Char.writeValue((int16_t) b);
  int rgb[3] = {r, g, b};
  RGB_Char.writeValue(rgb, 12);  
  Serial.print("array rgb = ");
  for (int i = 0; i < 3; i++)
  {
    Serial.print(rgb[i]);
  }
    Serial.println();

  BLEDevice central = BLE.central();
  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);
}
else{
  Serial.println("disconnected from cemtral: ");
  digitalWrite(LED_BUILTIN, LOW);
}
}

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