BLE XIAO nrf52840 in sleep mode help me

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
diltech
 
Posts: 1
Joined: Fri May 13, 2022 6:40 pm

BLE XIAO nrf52840 in sleep mode help me

Post by diltech »

Hi,

I use this code for sending always "Enter" to my phone for a little app. I use button many time on days and when i don't use the button, i need to go in sleep mode my module to save energy. I want use with a Cell battery 2032.
I want to keep blutooth connection and just wakeup with a button action.

Somebody help for this ?

Code: Select all

#include <Arduino.h>
#include <Adafruit_TinyUSB.h> // for Serial
/*********************************************************************
Programmation d'un bouton qui envoie une ligne de texte sur la tablette
à l'aide d'un lien Blutooth. Attention au délai: 1 sec programmable
avec que la touche ne se répète. Dans le cadre d'un projet de production.


*********************************************************************/
#include <bluefruit.h>
const int buttonPin = D1;
const int ledPin = D10;
#define SLEEPING_DELAY 30000
BLEDis bledis;
BLEHidAdafruit blehid;
int buttonState = 0;

bool hasKeyPressed = false;

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();
  Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);

  // Include BLE HID service
  Bluefruit.Advertising.addService(blehid);

  // There is enough room for the dev name in the advertising packet
  Bluefruit.Advertising.addName();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   *
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);   // number of seconds in fast mode
  Bluefruit.Advertising.start(0);             // 0 = Don't stop advertising after n seconds
}

void send_string()
{

  int i = 0;

  String send_data = "\n";

  int number_of_data = send_data.length();

  while (i <= number_of_data)
  {

    char one_alphabet = send_data[i];

    blehid.keyPress(one_alphabet);

    // Serial.print(one_alphabet);

    i++;
  }

  delay(1000);
}

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

  // Code pour attendre de démarrer le terminal série
  //  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Bluefruit.begin();
  Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
  Bluefruit.setName("BT_IDS_001");

  // Configure and Start Device Information Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather 52");
  bledis.begin();

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  /* Start BLE HID
   * Note: Apple requires BLE device must have min connection interval >= 20m
   * ( The smaller the connection interval the faster we could send data).
   * However for HID and MIDI device, Apple could accept min connection interval
   * up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max
   * connection interval to 11.25  ms and 15 ms respectively for best performance.
   */
  blehid.begin();

  // Set callback for set LED from central
  //  blehid.setKeyboardLedCallback(set_keyboard_led);

  /* Set connection interval (min, max) to your perferred value.
   * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms
   * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms
   */
  /* Bluefruit.Periph.setConnInterval(9, 12); */

  // Set up and start advertising
  startAdv();
}

void loop()
{

  // read the state of the pushbutton value:
  // Only send KeyRelease if previously pressed to avoid sending
  // multiple keyRelease reports (that consume memory and bandwidth)

  while (1)

  {
    buttonState = digitalRead(buttonPin);

    if (hasKeyPressed)
    {
      hasKeyPressed = false;
      blehid.keyRelease();
      // Delay a bit after a report
      delay(5);
    }
    if (buttonState == LOW)
    {
      digitalWrite(ledPin, HIGH);
      delay(35);
      digitalWrite(ledPin, LOW);
      send_string();
      hasKeyPressed = true;
      // Delay a bit after a report
      delay(10);
    }
  }
}



Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”