Stack OverFlow error

I am receiving an error message every time I try and use the app on my phone. When I hit a button on my phone it immediately sends me the java.lang.StackOverflow error and then the app stops responding. Can someone help me figure out why?

My guess is, you created an endless loop. .

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

Let me know if you need more information

Hi everyone,
For my senior project I am creating an app and utilizing the Arduino Nano 33 BLE. The idea of the project is to utilize the gyroscope within the Arduino and the app created will allow me to preset a value and when the Arduino has met that value by going up and down the red LED will then turn on. I have created the code for the most part but can not seem to get the app to work. Can anyone give me some tips and pointers on how to make the app actually function

At what point during the operation of your app does the stack overflow error occur?

I get the app on my phone and when I go to connect a device to pair which is my Arduino it kicks me out and gives me that error.

Post the code from both sides here.

What version of Android are you running?

I have a samsung galaxy s21. Also, I had to install something called lightblue which is how I can connect to the bluetooth

#include <Arduino_LSM9DS1.h> //This is the library for the gyroscope

#include <ArduinoBLE.h> //The bluetooth library

float X,Y,Z;//Float variable to be used in the gyroscope

#define LED 13//defines LED as pin 13

///Establishes GUUID along wiith the three characteristics

const  char * UUID_RepTrackservice =  "84582cd0-3df0-4e73-9496-29010d7445dd" ;

const  char * UUID_repCharacteristic =  "84582cd1-3df0-4e73-9496-29010d7445dd" ;

const  char * UUID_targetCharacteristic =  "84582cd2-3df0-4e73-9496-29010d7445dd" ;

const  char * UUID_resetCharacteristic =  "84582cd3-3df0-4e73-9496-29010d7445dd" ;

//Establishes the service along with defining the characteristics as a float

BLEService RepTrackservice ( UUID_RepTrackservice );

BLEIntCharacteristic repCharacteristic( UUID_repCharacteristic, BLERead | BLENotify );

BLEIntCharacteristic targetCharacteristic ( UUID_targetCharacteristic, BLEWrite | BLENotify );

BLEIntCharacteristic resetCharacteristic( UUID_resetCharacteristic, BLERead | BLENotify );

int presetValue = 5;// max value for z axis to detect rep count

int currentValue = 0;//Current z axis value

int repAmount=0;//variable used to hold rep amount sent to app commented out for troubleshooting

int repTarget=0;//variable used to hold repetitions wanted sent from app commented out for troubleshooting

int reset=0;//Variable to reset the count sent from app commented out for troubleshooting

//Setting up the arduinos outputs

void setup ()


  Serial.begin(115200);//begin the bluetooth

  uint32_t t=millis();

  while (!Serial) // delay 5sec connection


    if ((millis()-t) > 5000) break;


  bool err=false;

  if (!IMU.begin())


    Serial.println("IMU: failed");



  Serial.println("IMU: ok");

  // init BLE

  if (!BLE.begin())


    Serial.println("BLE: failed");



  Serial.println("BLE: ok");

  // error: flash led forever

  if (err)


    Serial.println("Init error. System halted");


  BLE.setLocalName("RepTrack2.0");//sets the local name


  BLE.setAdvertisedService (RepTrackservice);//setting the service to be advertised

  //Adds services to their own charcteristics

  RepTrackservice.addCharacteristic ( repCharacteristic ) ;

  RepTrackservice.addCharacteristic ( targetCharacteristic ) ;

  RepTrackservice.addCharacteristic ( resetCharacteristic ) ;

  BLE.addService ( RepTrackservice);//service is added to he BLE

  //Sets values so that they do not transmit random values

  repCharacteristic.writeValue ( 0 );

  targetCharacteristic.writeValue ( 0 );

  resetCharacteristic.writeValue ( 0 );

  BLE.advertise();//Publishes the service to start


  pinMode (LED, OUTPUT);//sets LED as an output

  digitalWrite(LED, LOW);//turns off LED in case it was on



void loop()


  static long preMillis = 0;

  int command=0;

  // looks for BLE central devices

  BLEDevice central = BLE.central();

  // Checks for connection

  if (central)


    Serial.print("Connected to central: ");

    Serial.println(central.address()); // prints address


    // Loop while connected

    while (central.connected())



      if (targetCharacteristic.written())


        command = targetCharacteristic.value(); // retrieve value from app


        Serial.print(F("commmand value:  "));




      long curMillis = millis();

      if (preMillis>curMillis) preMillis=0;

      if (curMillis - preMillis >= 10) // check values every 10mS


        preMillis = curMillis;

        updateApp(); // Calls function to send values to app



    // To disconnect

    Serial.print(F("Disconnected from central: "));




//Updating the app function

void updateApp()


  //Starting the scan to monitor the gyroscope

  //Checking to see if the Gyro is online and then Reads all 3 axis

   if (IMU.gyroscopeAvailable())


    IMU.readGyroscope(X, Y, Z);

  currentValue =Z;//sets current value to the value of Z from the gryo


   //Comparison that is the current value of z is greater than or equal to the preset, then you increase the rep amount. This means a rep was done

  if (currentValue >= presetValue)



    repCharacteristic.writeValue(repAmount);//updates the live value


  //If it was not greater then the repamount stays the same






  //displays values for visual purpose



  Serial.print("Current Value:");

 // Serial.println(currentValue);

 // Serial.println(repAmount);//Sends repamout to app



  //Checks to see if the repAmount is greater than or equal to the rep target, if yes then the LED will light up

   if (repAmount >= repTarget)


    digitalWrite(LED, HIGH);


  //If not then LED stays off



    digitalWrite(LED, LOW);


  delay(1000);// Delay to keep the system from readding two reps within a second

  //Reset for the rep amount, recieved from the app

  if (reset==1)









This is the code and the start of the app. I am not sure what to do because it does not run and I am sensing some issues. I do not mind it being readjusted or redesigned if need be

I am moving this thread to the other thread you started for the same app.

Ok. I was not sure how to ask this question and worded it a couple different ways. While I am getting an error I also would not mind some tips to help create something fully functional. I am not a computer science major, I am in engineering for the quality side of things and this is really hard for me to grasp

I'd suggest disabling the Disconnect block in the ErrorOccurred event. My guess is that you're on Android 12 or later, and because of that the BLE extension is throwing an error when it tries to disconnect (due to permission changes related to Bluetooth in Android 12) and that this triggers ErrorOccurred, which in turn calls Disconnect, etc. until the stack overflows.

I am now receiving an error saying Screen ErrorError 9101 in extension BluetoothLE: Expected device index between 1 and 0. Any idea what that could mean?

This error is thrown by the Connect method when the supplied index is out of bounds. Given that the latter value is 0, this means that internally the BLE extension hasn't discovered any devices. I don't see any when BluetoothLE1.DeviceFound blocks in your code, so it's possible you haven't confirmed that the device has been discovered before trying to connect.

So I should discover the bluetooth device by using the BluetoothLE1.DeviceFound block and then use the bluetoothLE1. connected block?

Not necessarily, but the BLE extension manages some internal state about what devices it knows about. You may want to try out this how-to about setting up an app to connect to a BLE device: