Arduino crashes when I upload my sketch, than the port is not available anymore

Hi, I have a problem with my arduino nano rp2040 connect. When I upload my code to it, then the built-in led starts flashing 4 times fast and 4 times slow and the board doesn't respond anymore, I can't even see the port. I've read on internet that this could be the indicator that the Mbed OS that runs underneath my sketch has crashed. It's a not a very long and complicated code (connected to my board I only have a thermal printer, a tft lcd ili9341 which they display/print things according to the datas that come through ble from another arduino nano rp2040 connect ) and it neither occupies a lot of space in the arduino memory.

What should I do to solve this problem? thnx

Hard to begin to diagnose the problem without seeing your code, posted in code tags.

this is the code that fails to upload:


#include <ArduinoBLE.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_Thermal.h"


#include "1.1.h"
#include "1.2.h"  // Assicurati di avere l'immagine bitmap "cuori.h" inclusa nel tuo progetto
#include "1.3.h"
#include "2.1.h"
#include "2.2.h"
#include "2.3.h"
#include "3.1.h"
#include "3.2.h"
#include "3.3.h"

//tempo utilizzo
int counter = 0; //counter che se arriva poi ai 10 sec determina la fine
unsigned long startTime = 0;  //tempo effettivo di utilizzo

// SCHERMINO
#define TFT_CS   10
#define TFT_DC   9
#define TFT_RST  -1

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

//COLORI SCHERMINO
#define TFT_BIANCO 0xFFFF
#define TFT_ROSA 0xFE59
#define TFT_ROSSO 0xD0A4

Adafruit_Thermal printer(&Serial1);

void setup() {
  pinMode(7, OUTPUT); digitalWrite(7, LOW);
  Serial.begin(19200);
  printer.begin();
  while (!Serial); // L'istruzione while (!Serial) attende finché la comunicazione seriale non è stabilita.
  
  BLE.begin();
  Serial.println("BLE Central - FSR control");
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");

  // SCHERMINO
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(TFT_BIANCO);

  // scritta iniziale
  // tft.setFont(&Chiswick_Grotesque_Semibold7pt7b);
  // drawAnimatedText("Hey sweety", 10, 70, 3);
  // delay(3000);
  // tft.fillScreen(TFT_BIANCO);
}

void loop() {
  BLEDevice peripheral = BLE.available();
  if (peripheral) {
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();
    if (peripheral.localName().indexOf("FSR Sensor") < 0) {
      Serial.println("No 'FSR Sensor' in name");
      return;
    }
    BLE.stopScan();
    controlLed(peripheral);
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

void controlLed(BLEDevice peripheral) {
  Serial.println("Connecting ...");
  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  BLECharacteristic FSRCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
  if (!FSRCharacteristic) {
    Serial.println("Peripheral does not have FSR characteristic!");
    peripheral.disconnect();
    return;
  }

  BLECharacteristic LightCharacteristic = peripheral.characteristic("19B10002-E8F2-537E-4F6C-D104768A1214");
  if (!LightCharacteristic) {
    Serial.println("Peripheral does not have Light characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    byte value = FSRCharacteristic.read();
    if (FSRCharacteristic.canRead()) {
      FSRCharacteristic.readValue(value);
      Serial.println(value);
        // Logica per determinare l'output in base al valore più alto tra i due sensori
        //SLOT 0
      if (value < 30) {
       int randomPhrase = random(3);
        switch (randomPhrase) {
        case 0:
          tft.fillScreen(TFT_BIANCO);
         tft.drawRGBBitmap(0, 0, (uint16_t *)unounoBitmap, UNOUNO_WIDTH, UNOUNO_HEIGHT);
          delay(1000);
        break;
        case 1:
          tft.fillScreen(TFT_BIANCO);
          tft.drawRGBBitmap(0, 0, (uint16_t *)unodueBitmap, UNODUE_WIDTH, UNODUE_HEIGHT);
          delay(1000);
        break;
        case 2:
          tft.fillScreen(TFT_BIANCO);
          tft.drawRGBBitmap(0, 0, (uint16_t *)unotreBitmap, UNOTRE_WIDTH, UNOTRE_HEIGHT);
          delay(1000);   
        break;
      }
    
      //SLOT 1
      } else if (value < 70) {
        int randomPhrase = random(3);
        switch (randomPhrase) {
        case 0:
        tft.fillScreen(TFT_ROSA);
        tft.drawRGBBitmap(0, 0, (uint16_t *)dueunoBitmap, DUEUNO_WIDTH, DUEUNO_HEIGHT);
        delay(1000);
        break;
        case 1:
        tft.fillScreen(TFT_ROSA);
        tft.drawRGBBitmap(0, 0, (uint16_t *)duedueBitmap, DUEDUE_WIDTH, DUEDUE_HEIGHT);
        delay(1000);
        break;
        case 2:
        tft.fillScreen(TFT_ROSA);
        tft.drawRGBBitmap(0, 0, (uint16_t *)duetreBitmap, DUETRE_WIDTH, DUETRE_HEIGHT);
        delay(1000);   
        break;
      }

      //SLOT 2
         } else if (value < 200) {
          int randomPhrase = random(3);
          switch (randomPhrase) {
          case 0:
            tft.fillScreen(TFT_ROSSO);
            tft.drawRGBBitmap(0, 0, (uint16_t *)treunoBitmap, TREUNO_WIDTH, TREUNO_HEIGHT);
            delay(1000);
          break;
          case 1:
            tft.fillScreen(TFT_ROSSO);
            tft.drawRGBBitmap(0, 0, (uint16_t *)tredueBitmap, TREDUE_WIDTH, TREDUE_HEIGHT);
            delay(1000);
          break;
          case 2:
            tft.fillScreen(TFT_ROSSO);
            tft.drawRGBBitmap(0, 0, (uint16_t *)tretreBitmap, TRETRE_WIDTH, TRETRE_HEIGHT);
            delay(1000);   
          break;
      }
    }

      //photoresistor e FINE
      byte luce = LightCharacteristic.read();
      if (LightCharacteristic.canRead()) {
        LightCharacteristic.readValue(luce);
        Serial.println(luce);
      }
      if (value < 5 && luce < 80) { //se non rileva più pressione e la luce è bassa
        if (counter == 0) {
          startTime = millis();
        }
        counter++;
      } else {
        counter = 0;
      }

      if (counter >= 10) {
      if (startTime <= 15000) { 
      printer.setFont('A');
      printer.setSize('S');
      printer.justify('R');
      Serial.println("hai sgravato");
      } else if (startTime > 15000 && startTime <= 40000) {
      printer.setFont('A');
      printer.setSize('S');
      printer.justify('R');
      Serial.println("ciao");
      } else {
      printer.setFont('A');
      printer.setSize('S');
      printer.justify('R');
      Serial.println("hello");
      }
        
        printer.sleep();      // Tell printer to sleep
        delay(3000L);         // Sleep for 3 seconds
        printer.wake();       // MUST wake() before printing again, even if reset
        printer.setDefault(); 

        while (true) {
          // Loop infinito per mantenere lo stato di uscita
        }
        //photoresistor e FINE
        
      }
    }
    
  Serial.println("Peripheral disconnected");
}
}

this is the code of the other Arduino, which uploads correctly:


int fsrPin = 0;
int fsrPin2 = 1;
int light;

BLEService FSRService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEByteCharacteristic FSRCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
BLEByteCharacteristic LightCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);

void setup() {
  Serial.begin(9600);
  pinMode(fsrPin, INPUT);
  pinMode(fsrPin2, INPUT);

  if (!BLE.begin()) {
    Serial.println("Starting Bluetooth®️ Low Energy failed!");
    while (1);
  }

  BLE.setLocalName("FSR Sensor");
  BLE.setAdvertisedService(FSRService);

  FSRService.addCharacteristic(FSRCharacteristic);   // Aggiungi la nuova caratteristica per il sensore di forza
  FSRService.addCharacteristic(LightCharacteristic); // Aggiungi la nuova caratteristica per la luce
  BLE.addService(FSRService);

  BLE.advertise();
  Serial.println("BLE FSR Sensor Peripheral, waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();
  if (central) {
    Serial.print("Connesso al nano centrale: ");
    Serial.println(central.address());

    while (central.connected()) {
      int sensorState = analogRead(fsrPin);
      Serial.print("Force sensor 1 reading = ");
      Serial.print(sensorState);

      int sensorState2 = analogRead(fsrPin2);
      Serial.print(", Force sensor 2 reading = ");
      Serial.print(sensorState2);

      int maxReading = max(sensorState, sensorState2);
      Serial.print(", Maxreading = ");
      Serial.println(maxReading);

      light = analogRead(A2);
      if (light > 100) {
        digitalWrite(13, HIGH); //accendi e sepgni il led integrato nell'arduino
      } else {
        digitalWrite(13, LOW);
      }
      Serial.println("Light reading = " + String(light));

      delay(500);

      FSRCharacteristic.writeValue(maxReading);
      LightCharacteristic.writeValue(light); // Aggiungi il valore di light alla caratteristica BLE
    }

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

That code is not the reason for the trouble. Please post a block diagram showing the system build.
Schematics might be the next request.

I am actually uploading the code without all the components connected so I think that there’s a problem in the code.
It used to work but a few weeks ago it started to crash. Now the Arduino is not even working after the bootloader reset.

P.S.: I’m new to this world so I’m sorry if I’m ignorant about a lot of things.

If the code was working, why do You go for an upload? Code doesn't crack or break. The fault is out in the surrounding circuitry.

Please provided engineering documentation. Words, so far, only confuse.

I had to do Uploads to trigger BLE connection between the two Arduinos since there's no other way the peripheral starts advertising.

If it could be helpful I can send the pictures of the circuit.

No thank You. Use common engineering ways like schematics, datasheet links etc.

I didn't find a schematic online simulator for our board, these are the main links we've followed to create our project.

TFT LCD connection to Nano Rp2040

Adafruit thermal printer
In order to connect the printer to the Nano rp2040 I've connected the GND of the printer to the GND of the Nano, RX of the printer to TX of the Nano and TX of the printer to RX of the Nano. GND, RX and TX of the printer are not directly connected to the Nano but trough a Level Shifter (5v to 3.3v).

@mtld01, are you @ulnnn?

we’re working on the same university project together. Since nobody was texting back to her I decided to share the same problem on my account😅

Your definition of "nobody" is flawed.

@mtld01, do not cross-post again. Other thread locked.

Hi @mtld01 and @ulnnn. My advice is to create a demonstration program that contains only the absolute minimum of code required to produce the fault. Doing this will make the situation easier to understand by removing all irrelevant complexity from the system.

I find that often by the time I have completed this work the cause of the fault has become clear to me. Even if it doesn't, you can then share this minimal, reproducible, verifiable example (MCVE) here on the forum. It will be of value to the helpers here.

Thank you, we're going to follow this path and try to solve the problem. :slightly_smiling_face:

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