XIAO Bluetooth not connecting when on battery power

Hey everyone, I have my XIAO set up with a battery and with my first tests having the board disconnected from my computer it has stopped working. It is on battery power and the code runs fine, however, for some reason I can’t connect to the bluetooth unless I first plug the board into my computer. If I start with it connected to my computer and connect to my phone then unplug the board from my computer my phone remains Connected over bluetooth. I have a feeling that there must be something related to Serial communication that is stopping the board from advertising its service. Here is the code, please let me know if there is something I am missing or should change so I can connect to the seeduino when it is not plugged into my computer, thank you in advance.

#include <MPU6050_light.h>
#include <Wire.h>
#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
 
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
 
const int motorPin = 1, valvePin = 2, ledPin = LED_BUILTIN, threshold = -50, interventionDelay = 3000;
bool ON = false, timerON = false;
MPU6050 mpu(Wire);
unsigned long time1 = 0;

//this function will turn off the pump and open the valve to deflate actuators
void Shutdown(){
 // Serial.println(F("Motor off"));
  digitalWrite(motorPin, LOW);   
  digitalWrite(ledPin, HIGH);
 // Serial.println("Valve open/close");
  digitalWrite(valvePin, HIGH);
  delay(1000);
  digitalWrite(valvePin, LOW);  
}

void setup() {
  Serial.begin(115200);
  digitalWrite(LEDG, LOW);
  while (!Serial);
    Wire.begin();
  mpu.begin();      
 // Serial.print("Hold still for calibration");    //only need calibration if I use z angles
 // delay(500);
 // mpu.calcGyroOffsets();                          // This does the calibration
  if (!BLE.begin()) {
   // Serial.println("starting Bluetooth® Low Energy module failed!");
    while (1);
  }
  
  pinMode(motorPin, OUTPUT);
  pinMode(valvePin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  /// BLE setup begins
  BLE.setLocalName("Jordan's Seeduino");              // set advertised local name and service UUID:
  BLE.setAdvertisedService(ledService);               //declare what your service is called
  ledService.addCharacteristic(switchCharacteristic); //declare the on/off switch charachteristic in service
  BLE.addService(ledService);                         // add service
  switchCharacteristic.writeValue(0);                 // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);                                                                           //try deleting this line and see if it still works
  BLE.advertise();                                    // start advertising service
  //Serial.println("BLE set to Peripheral");
}
 
void loop() {
  int angle = 0;
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();
 
  if (central) {                                      // if a central is connected to peripheral
  //  Serial.print("Connected to central: ");
   // Serial.println(central.address());                // print the central's MAC address
    time1 = millis();                                 //set the baseline for the timer
    digitalWrite(LEDB, LOW);
    // while the central is still connected to peripheral:
  while (central.connected()) {
    // Serial.print("State: "); Serial.println(switchCharacteristic.value());
        if (switchCharacteristic.written() || switchCharacteristic.value() == 1) {
          if (switchCharacteristic.value() == 1) {  //if the value sent is 1 then run the program
                //gather current angle data in Y
                mpu.update();                                   
                angle = mpu.getAngleY();
            //    Serial.print("Angle: "); Serial.println(angle);
                if(angle > threshold && !timerON){                         //if measurement is above threshold then start a timer so we can say they have been in the incorrect position for X amount of time so you must move
                  time1 = millis();
             //     Serial.println("Timer reset");
                  timerON = true;
                }
               if(angle < threshold){  //reset the timer so it doesnt keep counting while you are out of the ON state.
                time1 = millis();
                if(timerON){
              //    Serial.println("Valve open/close");
                  digitalWrite(valvePin, HIGH);
                  delay(1000);
                  digitalWrite(valvePin, LOW);  
                }
                if(ON){                                 //if you straighten your back then shutdown and wait a few seconds before detecting angles again.
                  Shutdown();
                  ON = false;
                } 
                timerON = false;
                }
                      
                //if the angle of your back is too steep for more than interventionDelay milliseconds then turn on the pumps if they aren't already on.
                if(angle > threshold && !ON && timerON && (millis() - time1) > interventionDelay){   
               //   Serial.println("Motor on");
               //   Serial.print("Time delay: "); 
               //   Serial.println(millis() - time1);
                  digitalWrite(motorPin, HIGH); 
                  digitalWrite(ledPin, LOW);
                  ON = true;
                  timerON = false;
                }
                if((millis()-time1) > 15000){                    //in the event that the shirt was activated but you still havent moved out of the position then turn off the pump.
                  digitalWrite(motorPin, LOW); 
                  digitalWrite(ledPin, HIGH);
                }
                mpu.update(); 
                delay(100);
          }
          else { //if the value is set to 0 then turn off the system
                   Shutdown();
                   ON = false;
                   timerON = false;
                   time1 = millis();  
               }
         // Serial.println(millis() - time1);
          delay(100);
          }
      }
    // when the central disconnects, print it out:
   // Serial.print(F("Disconnected from central: "));
   // Serial.println(central.address());
    Shutdown();
    ON = false;
    digitalWrite(LEDG, LOW);
}
}

“while(!Serial);” waits for the serial monitor of PC.

1 Like