       {
         buffer[start++] = '0';
       } 
+
       buffer[start++] = '.';
     }
   }

    return start+index;
}

void writeToSerial(char text[], int length, float value)
{
    char buffer [length + 10];
    buffer[0] = *"#";
    copyOver(text, buffer, length, 1);
    int index = dToBuffer(value, buffer, length+1);
    buffer[index] = *"#";
    buffer[index+1] = '\0';
    
//    if (Serial.available())
//    {
//        Serial.write(buffer);
//        Serial.flush();  
//        Serial.println();
//    }
    
//    if (Serial1.available())
//    {
        Serial1.write(buffer);
        Serial1.flush();  
//    }
}

void bleTransmitSensorData() 
{
    char text[] = { 'A', 'X', ':' };
    writeToSerial(text, 3, Ax);
    delay(5);
    
    text[1] = 'Y';
    writeToSerial(text, 3, Ay);
    delay(5);
    
    text[1] = 'Z';
    writeToSerial(text, 3, Az);
    delay(5);
    
    text[0] = 'G';
    text[1] = 'X';
    writeToSerial(text, 3, Gx);
    delay(5);
    
    text[1] = 'Y';
    writeToSerial(text, 3, Gy);
    delay(5);
    
    text[1] = 'Z';
    writeToSerial(text, 3, Gz);
    delay(5);
    
    text[0] = 'L';
    text[1] = 'O';
    writeToSerial(text, 3, loudness);
    delay(5);
}

void setup() {

}

void loop() {
  while (true) {
    if (Serial1.available())
    {
      measurementMode = Serial1.read();
    }
    
    //If greater than 0 => take measurement
    if (measurementMode > 48)
    {
      updateAccelerationAndGyro();
      updateLoudness();
      //normalise()
      bleTransmitSensorData();
    }
    
    // if measurement mode is 1 => one measurement only
    if (measurementMode < 50)
    {
      measurementMode = 0;
    }
   
    delay(100);
  }
