App crashes when sending a command to the Arduino via bluetooth

Hi everyone! I am working on a project "Coin Sorting Machine with Withdrawal System" for my bachelor's thesis. This is a machine that will be used to sort coins (like a piggy bank but it sorts coins based on denominations). This machine is controlled wirelessly using an android app. The android app will be used to track the coins inside the machine and it can also be used to withdraw those coins in the specified number by the users.

The hardware is now complete, and the app's design and blocks is almost complete (around 90%) but I ran into some problem. Here's the complete breakdown of the withdrawal function before I state the problen.

Above is the withdrawing interface of the application. (Pardon for the design I just made this on the fly).The three buttons are segmented buttons. This is where the user will select the coin type he's going to withdraw.
Here is the code for the segmented buttons:
2024-10-14 17_31_12-Window

After clicking a button, the selected coin type will be saved to its corresponding variable. This will also allow the user to now enter a quantity since he has already selected a coin.

This is the code for the "Enter Quantity" section along with the stepper buttons:

2024-10-14 17_28_56-Window

After the user has successfully selected a coin and entered a quantity, he can now click the "Confirm Withdrawal" button to start the process. Here is the block:


Depending on what coin is selected, the quantity entered inside the quantity box will be joined with the "H1, H2, and H3" commands with a period as delimiter. What does these mean?
H1/H2/H3 corresponds to the relay that will be turned on when the data is sent. If H1, then the Relay 1 will turn on. You get the idea.
To give you a grasp here is the part of my arduino code that processes this data:

// Check for Bluetooth input
if (Serial1.available() > 0) {
String command = Serial1.readStringUntil('\n');
command.trim(); // Remove any whitespace

if (command.startsWith("H")) {
  int dotIndex = command.indexOf('.');
  if (dotIndex != -1) {
    String relayIdentifier = command.substring(0, dotIndex);  // Get "H1", "H2", etc.
    String numberPart = command.substring(dotIndex + 1);      // Get n (number of objects)
    int detectionLimit = numberPart.toInt();                  // Convert n to integer

    if (relayIdentifier == "H1") {
      detectionLimit1 = detectionLimit;
      monitoringEnabled1 = true;
      digitalWrite(relayPin1, LOW);  // Turn relay 1 ON
      objectCount1 = 0;              // Reset object count for sensor 1
      Serial.println("Monitoring started for Relay 1. Detection limit: " + String(detectionLimit1));
    } else if (relayIdentifier == "H2") {
      detectionLimit2 = detectionLimit;
      monitoringEnabled2 = true;
      digitalWrite(relayPin2, LOW);  // Turn relay 2 ON
      objectCount2 = 0;              // Reset object count for sensor 2
      Serial.println("Monitoring started for Relay 2. Detection limit: " + String(detectionLimit2));
    } else if (relayIdentifier == "H3") {
      detectionLimit3 = detectionLimit;
      monitoringEnabled3 = true;
      digitalWrite(relayPin3, LOW);  // Turn relay 3 ON
      objectCount3 = 0;              // Reset object count for sensor 3
      Serial.println("Monitoring started for Relay 3. Detection limit: " + String(detectionLimit3));
    }
  }
}

}

Now that the whole function is explained, here is the main problem:

When clicking the "Confirm Withdraw" button, the command will be sent successfully and the relay will turn on (and also starting the withdrawal process). Then, the app will FREEZE for a few seconds and then exits (basically a crash). This is a random event. Sometimes, it happens in the first time, and sometimes it is very random. The maximum straight times I was able to withdraw is 7 times before the app FREEZES and exits.

I literally have no idea how to fix this. I've tried asking AI for this but it gives very bad advice. Hopefully someone could help me with my problem! I will show the other blocks if you require to see it to help you in assisting me.

You might start by adding a '\n' at the end of each message you send from the app to the hardware.

Your sketch needs those.


Is this correct?

It couldn't hurt.

You have not mentioned the data flow from the device to the app.

That's more likely to freeze the app.

So far, I haven't implemented that. I was focusing first on sending the data from the app to the device.

I've tried this solution and so far I got to 15 straight withdrawals without a freeze. I'll keep testing and testing if the problem resides.

which version of mit app inventor are you using because mine doesn't have the bluetooth client and give me the exe

Canned Response ABG - Project Properties

Project Properties
Project Properties2

Set your Blocks Toolkit to default

MIT App Inventor is a web application, not something you would install on your system.