Errore bluetooth

Salve a tutti. Ho un arduino UNO con una bluetooth shield e ho scritto un programma per accendere un led con un’applicazione per android fatta da me. Il tutto funziona sia per accendere un led in PWM che in ON/OFF. Solo che se provo a collegare un sensore di temperatura (DS18B20) l’applicazione a volte si blocca o i dati vengono mandati lentamente.
Questo è il programma:

#include <SoftwareSerial.h> //Software Serial Port
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
float c=0;
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

#define uint8 unsigned char 
#define uint16 unsigned int
#define uint32 unsigned long int

#define RxD 6 // This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD)
#define TxD 7 // This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD)
 
#define DEBUG_ENABLED 1


int Clkpin = 9; //RGB LED Clock Pin (Digital 9)
int Datapin = 8; //RGB LED Data Pin (Digital 8)
int ledPin = 3;      // LED connected to digital pin 9
int val = 0;   
int off=0,on=1;
char Lettura[11];
SoftwareSerial blueToothSerial(RxD,TxD);

/*----------------------SETUP----------------------------*/ 
void setup() { 
 Serial.begin(9600); // Allow Serial communication via USB cable to computer (if required)
 pinMode(RxD, INPUT); // Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6
 pinMode(TxD, OUTPUT); // Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7
 pinMode(13,OUTPUT); // Use onboard LED if required.
 setupBlueToothConnection(); //Used to initialise the Bluetooth shield
 pinMode(ledPin, OUTPUT);   // sets the pin as output
 pinMode(Datapin, OUTPUT); // Setup the RGB LED Data Pin
 pinMode(Clkpin, OUTPUT); // Setup the RGB LED Clock pin
 sensors.begin();
 
} 

/*----------------------LOOP----------------------------*/ 
void loop() { 
 
 
 
 digitalWrite(13,LOW); //Turn off the onboard Arduino LED
 char recvChar;
 while(1){
 
 if(blueToothSerial.available())
   {
   //check if there's any data sent from the remote bluetooth shield
   recvChar= blueToothSerial.read();
   Serial.print(recvChar); // Print the character received to the Serial Monitor (if required)
 
   //If the character received = 'r' , then change the RGB led to display a RED colour
   if(recvChar=='H'){
 
      digitalWrite(13,HIGH);
      blueToothSerial.write(on);
      
      
      }
   else if(recvChar=='L')
      {
 
      digitalWrite(13,LOW);
      blueToothSerial.write(off);
      
      }
  /* else if(recvChar=='T')
      {
        sensors.requestTemperatures(); // Send the command to get temperatures
        c=sensors.getTempCByIndex(0);
        c=c-1;
        blueToothSerial.write(c);
        delay(200);
      }*/
   else
     {
     val=recvChar;
     analogWrite(ledPin, val);
     }
   }
    
  }
c=0;

}


//The following code is necessary to setup the bluetooth shield ------copy and paste----------------
void setupBlueToothConnection()
{
 blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
 blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
 blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
 blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
 delay(2000); // This delay is required.
 blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
 Serial.println("The slave bluetooth is inquirable!");
 delay(2000); // This delay is required.
 blueToothSerial.flush();
}

Il problema è di arduino perchè commentando la parte della temperatura il programma funziona senza problemi.
Accetto tutti i consigli grazie in anticipo! :slight_smile:

stai usando il metodo classico per leggere la temperatura, il che include quasi un secondo di delay per la lettura del dato dal 18B20, questo intendi per lentezza ? (sono 750ms a risoluzione 12bit) Usa l'opzione Async della libreria dallas e risolvi