Grove soundsensor lib
Diff: soundsensor.cpp
- Revision:
- 3:372e67c123c3
- Parent:
- 1:29daa2bdd0c6
- Child:
- 4:89a9c205446c
--- a/soundsensor.cpp Thu Jan 17 10:41:12 2019 +0000 +++ b/soundsensor.cpp Fri Jan 18 10:28:28 2019 +0000 @@ -13,7 +13,8 @@ } float soundsensor::convertToDb(float value){ - return 16.801 * log (value/65535) + 9.872; + //return 16.801 * log (value/65535) + 9.872; + return 1000000*20*(log(value)/log(10)))/1000000; } float soundsensor::revertFromDb(float value){ @@ -26,9 +27,20 @@ float soundsensor::listen(bool toDb){ AnalogIn sensor(this->getPin()); float value = sensor.read_u16(); + + for(int i=0;i<100;i++){ + values[i] = soundSensor.read_u16(); + wait(0.0001); + } + + for(int j=0;j<100;j++){ + sum += values[j]; + } + average = sum/100; + if(toDb){ - return this->convertToDb(value); + return this->convertToDb(average); }else{ - return value; + return average; } } \ No newline at end of file