Problème prog Arduino, capteur de son Iduino 1485297

une proposition de code qui maintient les données dans un buffer

const byte pinCapteur = A0;
const uint32_t dureeEcoute = 5000; // en ms
const int seuil = 300;
const uint16_t tailleBuffer = 125;

uint8_t echantillons[tailleBuffer]; // initialisé à 0 par le compilateur car variable globale

const uint32_t nombreEchantillons = tailleBuffer * 8;
const uint32_t periodeEchantillonage = dureeEcoute / nombreEchantillons;

uint16_t numeroEchantillon = 0;
uint16_t sommeEchantillons = 0;

// **** acquisition ****
//   rajoute un échantillon dans le buffer si c'est le moment.
//   retourne vrai si l'échantillon a été rajouté
//   met à jour la variable sommeEchantillons qui est le nombre de fois où l'échantillon était > seuil sur la durée d'écoute (après initialisation)
//**********************

bool acquisition() {
  static uint32_t derniereAcquisition = -periodeEchantillonage; // valeur initale pour que le premier échantillon soit pris immédiatement en compte
  if (millis() - derniereAcquisition >= periodeEchantillonage) {
    uint16_t indexOctet = numeroEchantillon >> 3;   //
    uint8_t  indexBit = numeroEchantillon & 0b111;  // 3 bits de poids faible (de 0 à 7)
    sommeEchantillons -= bitRead(echantillons[indexOctet], indexBit);
    if (analogRead(pinCapteur) > seuil) {
      sommeEchantillons++;
      bitSet(echantillons[indexOctet], indexBit);
    } else {
      bitClear(echantillons[indexOctet], indexBit);
    }
    if (++numeroEchantillon >= nombreEchantillons) numeroEchantillon = 0;
    derniereAcquisition = millis();
    return true;
  }
  return false;
}

void setup() {
  Serial.begin(115200);
  Serial.print(F("\nPréparation .")); // on remplit le buffer
  while (true) {
    if (acquisition()) {
      if ((numeroEchantillon % 100) == 0) Serial.write('.');
      if (numeroEchantillon == 0) break; // on remplit le buffer
    }
  }
  Serial.print(F("\nSystème Prêt.\nSon Fort à ")); Serial.print((100ul * sommeEchantillons) / nombreEchantillons);
  Serial.println(F("%."));
}

void loop() {
  if (acquisition()) {
    Serial.print(F("Son Fort à ")); Serial.print((100ul * sommeEchantillons) / nombreEchantillons);
    Serial.println(F("%."));
  }
}