DFRobot sound sensor problem

I've just bought an arduino 101 board and started playing around with it but I have a problem with sound sensor. Firstly - when I clap or do pretty much anything noisy, it stops serial monitor and computer make that sound it makes when you plug something into an usb. But it worked, at least until today. I made a sketch that lights up an led when I clap and it was working fine. But when I tried to make it work with Blynk(where you could turn on and off clapping), it started to freeze after few claps.

//WEIRD STUFF
#define BLYNK_PRINT Serial

#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>

char auth[] = "dab0e3c24dd84171b66bf3d718be985e";

BLEPeripheral  blePeripheral;

//REGULAR PINS

int led = 13;
int Sensor = A0;

//VARIABLES

int volume;
unsigned long long int firstClap;
unsigned long long int secondClap;
int clapVolume;
int MIN;
int avarage;

int clapping;
int lights = 0;

//VIRTUAL PINS

int button;
int slider;

//APP OPERATION

BLYNK_WRITE(V1)
{
  button = param.asInt();
}
BLYNK_WRITE(V0)
{
  slider = param.asInt();
}
BLYNK_WRITE(V2)
{
  clapping = param.asInt();
}

//CLAPPING FUNCTION

void clap(){
  MIN = slider+100;
  volume = analogRead(Sensor);
  if(volume > MIN){
    if(firstClap == 0){
      firstClap = millis();
      delay(100);
    }else{
      secondClap = millis();
      clapVolume = volume; //new
    }
  }
  
  if(secondClap - firstClap < 1200 && secondClap - firstClap > 250 && firstClap != 0 && secondClap != 0){
    delay(500); //new
    avarage = analogRead(Sensor); //new
    if(clapVolume - avarage > MIN){ //new
    if(lights == 0){
      lights = 1;
    }else if(lights == 1){
      lights = 0;
    }
    firstClap = 0;
    secondClap = 0;
  }
  } //new
  if(millis() - firstClap > 1200 || (secondClap - firstClap < 400 && firstClap != 0 && secondClap != 0)){
    firstClap = 0;
    secondClap = 0;
  }
}

void setup()
{ 
  delay(1000);

  blePeripheral.setLocalName("room ");
  blePeripheral.setDeviceName("room");
  blePeripheral.setAppearance(384);

  Blynk.begin(blePeripheral, auth);

  blePeripheral.begin();
  
  pinMode(led, OUTPUT);
  pinMode(Sensor, INPUT);
}

void loop(){
  if(clapping == 0){
    lights = button;
  }else{
    clap();
  }
  if(lights == 1){
    digitalWrite(led, HIGH);
  }else{
    digitalWrite(led, LOW);
  }
  blePeripheral.poll();
  Blynk.run();
}

smart_room.ino (1.79 KB)

Hi there!

Could you also post a picture or attachment of a schematic and any wiring and components that you have connected?

The sketch may also be useful but PLEASE use code tags ( </> ) when you post it.

Well, it's not very complicated with that gravity shield. I've also noticed that sound sensor doesn't have on-board potentiometer eventhough according to the picture on the website it should.

Okay, I found out that the problem is arduino disconects from Blynk application after calling "clap()" function. But why does it happen and how to fix it?