Is it a programming error? Port is not recognized when uploading code.

Hello, everyone.

I’m dealing with BLE and IMU sensors through the nano33ble board.
There are three functions that I want to make.

Obtain the slope value through the IMU sensor.

  1. Left LED illuminates for 1 second when board tilts to left.
  2. Right LED illuminated for 1 second when board tilts to right.
  3. Center LEDs keep blinking regardless of sensor

The central LED can be turned on or off via the app.
When I upload this code, the port connection is not working and the Bluetooth connection is also not working.

Below is my entire code.

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

float ax, ay, az; //가속도 변수
float gx, gy, gz; //자이로 변수

int left_led = 12;
int right_led = 11;
int center_led = 10;

//millis()함수를 사용하여 blink에 이용
unsigned long previousMillis_1 = 0; //center led
unsigned long previousMillis_2 = 0; //left led
unsigned long previousMillis_3 = 0; //right led

//===================================================================================================

void setup() {
  Serial.begin(9600);
  //while (!Serial); 

  // set LED pin to output mode
  pinMode(center_led, OUTPUT); 
  pinMode(left_led, OUTPUT);
  pinMode(right_led, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("Blink_Toggle");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");

  Center_led();
}

//===================================================================================================
//led동작에 관한 함수 정의
boolean blink_state1 = true;
boolean left_state, right_state = false;;
boolean led_state = HIGH;


void Center_led()
{
  unsigned long currentMillis_1 = millis();
  if(blink_state1 == true)
  {
    if(currentMillis_1 - previousMillis_1 > 500) //0.5초 블링크
    {
      previousMillis_1 = currentMillis_1;
      digitalWrite(center_led, led_state);
      led_state = !led_state;
    }
  }
  else if(blink_state1 == false)
  {
    digitalWrite(center_led, LOW);
  }
}

void Left_led()
{
  Serial.println("왼쪽");
  unsigned long currentMillis_2 = millis();
  if(currentMillis_2 - previousMillis_2 > 1000) //1초 동안 ON
  {
    previousMillis_2 = currentMillis_2;
    digitalWrite(left_led, HIGH);    
  }
  digitalWrite(left_led, LOW);
}

void Right_led()
{
  Serial.println("오른쪽");
  unsigned long currentMillis_3 = millis();
  if(currentMillis_3 - previousMillis_3 > 1000) //1초 동안 ON
  {
    previousMillis_3 = currentMillis_3;
    digitalWrite(right_led, HIGH);    
  }
  digitalWrite(right_led, LOW);
}


void IMU_start()
{
  //엑셀, 자이로값
  if(IMU.accelerationAvailable()) 
    IMU.readAcceleration(ax, ay, az);
  if(IMU.gyroscopeAvailable())
    IMU.readGyroscope(gx,gy,gz);

    //각도 계산 x축, y축
    float RADIAN_TO_DEGREES = 180/3.14139;
    float val_y = atan(ax/sqrt(pow(ay,2) + pow(az,2))) * RADIAN_TO_DEGREES;
    float val_x = atan(ay/sqrt(pow(ax,2) + pow(az,2))) * RADIAN_TO_DEGREES;

  //왼쪽, 오른쪽 기울기 판별 및 실행
  if(val_x > 30) //x축 기울기    
  {
    right_state = true;
    left_state = false;
    if(right_state == true)
    {
      Right_led(); //오른쪽 LED 실행
    } 
  }
     
  else if(val_x < -30) //x축 기울기
  {
      right_state = false;
      left_state = true;
      if(left_state == true)
      {
        Left_led(); //왼쪽 LED 실행
      }
   }
}
  
//===================================================================================================

void loop() {
  BLEDevice central = BLE.central();
  
  IMU_start();


    while (central.connected()) { 
      Center_led(); //central led blink
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) { 
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          //digitalWrite(center_led, HIGH);         // will turn the LED on 
          blink_state1 = true; //led blink ON
        } 
        else {                              // a 0 value
          Serial.println(F("LED off"));
          //digitalWrite(center_led, LOW);          // will turn the LED off 
          blink_state1 = false; //led blink OFF
        }
      }
    }
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
}

Can I know the problem with this?

Thank you in advance!

I don't have time to investigate it thoroughly, but I'm suspicious of these lines:

yscheol:

    float val_y = atan(ax/sqrt(pow(ay,2) + pow(az,2))) * RADIAN_TO_DEGREES;

float val_x = atan(ay/sqrt(pow(ax,2) + pow(az,2))) * RADIAN_TO_DEGREES;

Dividing by zero will cause the symptoms you describe. Here's a minimal sketch if you want to try it out:

void setup() {
  volatile byte foo = 1 / 0;  // The variable must be volatile to prevent it from being optimized out
}
void loop() {}

Are you seeing a 4X long, 4X short blink pattern? If so, this indicates Mbed OS has crashed. This causes the USB code that creates the CDC serial port to stop running, thus no port.

You can recover from this by putting the board in bootloader mode:

  • Press and release the reset button on your board quickly twice. You should now see the LED on the board pulsing, which means the bootloader is running. The double press causes the bootloader to run indefinitely (until the board is reset, powered off, or an upload is done), which means you don't need to get the timing of the reset just right.
  • Select the port of your board from the Tools > Port menu. The port will be different when the bootloader is running so don't assume you already have the correct port selected.
  • Sketch > Upload

The upload should now complete successfully. However, if you upload the same sketch you were running when you had the problem, it will probably just occur again. You will need to figure out what part of the sketch is crashing MbedOS. Now that you know how to recover the board from a crash, you can start working on that.

One thing that will crash Mbed OS is doing analogWrite on too many pins at the same time.


Debug information is printed on Serial1 when Mbed OS crashes. You can view the debug output by following these instructions (no guarantee it will be helpful though):

Unplug your Nano 33 BLE board from the computer or power supply.

Make the following connections between a USB to TTL serial adapter (AKA “FTDI”) and your Nano 33 BLE:

| Adapter | | Nano 33 BLE |
| - | - |
| ----------- | |------- |
| RX | | TX1 |
| ----------- | |------- |
| GND | | GND |

You will also need to power the Nano 33 BLE. You can do this via the USB socket, or power it from the USB to TTL serial adapter’s VCC pin. If the serial adapter’s VCC pin is outputting 5 V, connect it to the Nano 33 BLE’s VIN pin. If the serial adapter’s VCC pin is outputting 3.3 V, connect it to the Nano 33 BLE’s 3.3 V pin.

Connect the serial adapter to your computer with a USB cable.

Select the port of the serial adapter from the Arduino IDE’s Tools > Port menu.

Tools > Serial Monitor

Set the baud rate menu at the bottom right corner of Serial Monitor to 115200.

Press and release the reset button on the Nano 33 BLE. You should now see the “MbedOS Error Info” output in the Serial Monitor output field.