BLE working in a time

Hello, I want to make the BLE state work for a certain period of time. BLE is activated when I throw a certain char. I wanted to add a period to the code with the millis() command. However, it does not enter the Period. Could you please provide a comment or advice?

#include <Arduino.h>
#include <bluefruit.h>
#include <Wire.h>

// // Forward declarations for functions
// void connect_callback(uint16_t conn_handle);
// void disconnect_callback(uint16_t conn_handle, uint8_t reason);

/**
   @brief  BLE DFU service
   @note   Used for DFU OTA upgrade
*/
BLEDfu bledfu;

/**
   @brief Arduino setup function. Called once after power on or reset

*/
void setup()
{
    // Initialize built in green LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);

    // Initialize Serial for debug output
    Serial.begin(115200);

    

    // Wait for USB Serial to be ready or terminal to be connected
    time_t timeout = millis(); // Timeout in case the system runs on its own
    // Waiting for Serial
    while (!Serial)
    {
        if ((millis() - timeout) < 5000)
        {
            // Blink the LED to show that we are alive
            delay(100);
            digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
        }
        else
        {
            // Timeout while waiting for USB Serial
            break;
        }
    }


}




void loop()
{
  unsigned long previousTime = 0;
  
  while (Serial.available() > 0) 
  {

      if (Serial.read() == '1') {


          if ((millis()-previousTime) < 50000UL) 
          { 
              
              Serial.printf("system is works");
     
              Serial.println("================================");
              Serial.println("RAK4631 BLE OTA example");
              Serial.println("================================");

              // Config the peripheral connection with maximum bandwidth more SRAM required by SoftDevice Note: All config***() function must be called before begin()
              Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
              Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);

              Bluefruit.begin(1, 0);
              // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
              Bluefruit.setTxPower(4);
              // Set the BLE device name
              Bluefruit.setName("RAK4631_OTA");
              // Bluefruit.Periph.setConnectCallback(connect_callback);
              // Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
              bledfu.begin();
              
             
 Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
              Bluefruit.Advertising.addTxPower();
              Bluefruit.Advertising.addName();
              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
          


          } 
  
      }
  
  }

    

}

Welcome to the forum @yohyaVv

I run your code and it works as expected.
During the first 50 seconds I can send “1” over USB and BLE starts advertising.
After 50 seconds the “1” is ignored. Nothing wrong with your code.

Just

if ((millis()-previousTime) < 50000UL)

is useless, as you set previousTime to zero everytime the loop runs. You could just do

if (millis() < 50000)

thank you very much.

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