Detect specific ble device around my Arduino nano 33 BLE

Hello,

I'm beginner in Arduino dev, but I'm a developer.

For a project, I'm trying to write some code for Arduino nano 33 BLE. I'd like to turn on some lights (LEDs) when a specific device is in range of the arduino. I'm trying to catch a device named w6-1 in my example.

Here is the code:

#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 60
#define DATA_PIN 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);

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

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

  BLE.setLocalName("Nano33BLE");
  BLE.advertise();
  Serial.println("Bluetooth activated, waiting connection...");
}

void loop() {
  // Seach for w6-1 device
  BLE.scanForName("w6-1"); 

  // Check if a device is found
  BLEDevice device = BLE.available();
  if (device) {
    Serial.print("Device w6-1 detected at ");
    Serial.print(millis());
    Serial.println(" ms, turning on LEDs");
    lightLEDs(5); // Allumer les LEDs pendant 5 secondes
  }

  // Redémarrez le scan après avoir traité la détection
  BLE.stopScan();
}

void lightLEDs(int duration) {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0)); // Rouge
  }
  strip.show();
  delay(duration * 1000); // wait 'duration' seconds

  strip.clear();
  strip.show();
}

My problem is that it does not seems to be acurate. For example, my w6-1 device is on my desk beside the arduino. So I guess I'd be able to see the lights turn it on very often. But it is not the case. There are on at some times, but the time between 2 is not the same.

Here are some logs with time when the lights are on:

Device w6-1 detected at 14939 ms, turning on LEDs
Device w6-1 detected at 45939 ms, turning on LEDs
Device w6-1 detected at 93936 ms, turning on LEDs
Device w6-1 detected at 160937 ms, turning on LEDs
Device w6-1 detected at 186947 ms, turning on LEDs
Device w6-1 detected at 213951 ms, turning on LEDs
Device w6-1 detected at 281949 ms, turning on LEDs
Device w6-1 detected at 343958 ms, turning on LEDs
Device w6-1 detected at 559966 ms, turning on LEDs
Device w6-1 detected at 679973 ms, turning on LEDs
Device w6-1 detected at 699976 ms, turning on LEDs
Device w6-1 detected at 885982 ms, turning on LEDs
Device w6-1 detected at 981993 ms, turning on LEDs
Device w6-1 detected at 996991 ms, turning on LEDs
Device w6-1 detected at 1004991 ms, turning on LEDs
Device w6-1 detected at 1091000 ms, turning on LEDs
Device w6-1 detected at 1107991 ms, turning on LEDs
Device w6-1 detected at 1116993 ms, turning on LEDs
Device w6-1 detected at 1132999 ms, turning on LEDs
Device w6-1 detected at 1189995 ms, turning on LEDs
Device w6-1 detected at 1212997 ms, turning on LEDs
Device w6-1 detected at 1247002 ms, turning on LEDs
Device w6-1 detected at 1292002 ms, turning on LEDs

Do you have any ideas about what could be wrong with my code?

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