Can't include "Arduino_LSM9DS1.h" and "ArduinoBLE.h"

I see posts that include “Arduino_LSM9DS1.h” and “ArduinoBLE.h” in their ino’s.

LightBlue won’t recognize the Nano 33 unless my code is compiled with the following restrictions:

  • There is an analogRead() function called in the program
  • Globals are not used. A while(1) within Loop let’s one use the locals since globals can’t be used.
  • When trying to use two characteristics, a third characteristic must be defined (csEnabled in this case). Only two characteristics are added to the service.
  • Don’t include “Arduino_LSM9DS1.h”.
#include <ArduinoBLE.h>
//?? #include <Arduino_LSM9DS1.h>

// create service:
BLEService countService("10000000");
// create service characteristics.
BLEUnsignedLongCharacteristic csSeconds("10000001", BLERead);
BLEUnsignedLongCharacteristic csCount("10000002", BLERead | BLENotify);
  // This one is declared but not added to the service.
BLEIntCharacteristic csEnabled("19B10010-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite );

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  // begin initialization
  while (!BLE.begin()) {
    Serial.println("Waiting for BLE to start");
    delay(1);
  }

  // set the local name that the peripheral advertises:
  BLE.setLocalName("Count Service");
  // set the UUID for the service:
  BLE.setAdvertisedService(countService);

  // add the characteristics to the service
  countService.addCharacteristic(csSeconds);
  countService.addCharacteristic(csCount);
  // add the service
  BLE.addService(countService);

  // start advertising the service:
  BLE.advertise();
}

void loop() {
  unsigned long lastSec=millis()/1000; // Coded such that lastSec will go from 0..2**32-1
  unsigned long loopCount=0;
  uint16_t enabled=0;

  while(1) {  // Use while to get around global varialbe usage when using arduinoBLE.h
    if (lastSec != millis()/1000) lastSec++;

    BLEDevice central = BLE.central();
    // if a central is connected to the peripheral:
    if (central) {
      // print the central's BT address:
      Serial.print("Connected to central: ");
      Serial.println(central.address());
      // turn on LED to indicate connection:
      digitalWrite(LED_BUILTIN, HIGH);
    
      // while the central remains connected:
      while (central.connected()) {
        loopCount = loopCount + 1;
        //************** DON'T remove this analogRead. Device will not be recognized otherwise.
        int xReading = analogRead(A0);
        //**************
        // write values to service characteristics:
        if (lastSec != millis()/1000) lastSec++;
        csSeconds.writeValue(lastSec);
        csCount.writeValue(loopCount);
      }
      loopCount +=1000000;
      Serial.println("Disconnected from central.");
    } else {
      // turn off the LED
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Welcome to the forum

ardprb:
I see posts that include "Arduino_LSM9DS1.h" and "ArduinoBLE.h" in their ino's.

I probably posted something here in the forum and tested it before. There are a few issues in your code. Let us see whether we can fix them. I will keep it brief, please let me know when something is unclear.

ardprb:
unsigned long lastSec=millis()/1000; // Coded such that lastSec will go from 0..2**32-1
...
if (lastSec != millis()/1000) lastSec++;

I do not believe this works as you expect it. millis() returns a unsigned long which you divide by 1000 and then compare to your unsigned long. Once millis() overflows your logic breaks down.

The reason you need the analogRead is that you use it as a delay function. Without it you write your characteristics more often than they can be read by anyone. You need slow down the update of your characteristics. You can use the millis() function for that.

Your UUIDs are wrong. You can ether use 16-bit UUIDs defined by the Bluetooth SiG or 128-bit random UUIDs for characteristics and services you define yourself.

I am not sure why you believe you cannot use global variables. You did not show this in your example. I am certain you can use global variables.

You do not need while if you write your code properly. The while is used because the central is declared locally (and therefore destroyed every time loop finishes). With a global or static variable for central you can rewrite your code without while.

I got around this problem by coping some working code and modified it to suit my needs.