Grove soundsensor lib
soundsensor.cpp@9:c38836e5d1f5, 2019-01-23 (annotated)
- Committer:
- math991e
- Date:
- Wed Jan 23 11:48:11 2019 +0000
- Revision:
- 9:c38836e5d1f5
- Parent:
- 8:5b4b258eb1da
array fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
math991e | 8:5b4b258eb1da | 1 | /** |
math991e | 8:5b4b258eb1da | 2 | * @file soundsensor.cpp |
math991e | 8:5b4b258eb1da | 3 | * @brief this cpp file is where all the logic is handled. |
math991e | 8:5b4b258eb1da | 4 | * |
math991e | 8:5b4b258eb1da | 5 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 6 | * |
math991e | 8:5b4b258eb1da | 7 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 8 | */ |
math991e | 8:5b4b258eb1da | 9 | |
math991e | 0:18d442efc99a | 10 | #include "soundsensor.h" |
math991e | 0:18d442efc99a | 11 | |
math991e | 8:5b4b258eb1da | 12 | /** |
math991e | 8:5b4b258eb1da | 13 | * Constructor that sets pin to to take analog input form. |
math991e | 8:5b4b258eb1da | 14 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 15 | * @param pin The pin that the sound sensor is placed |
math991e | 8:5b4b258eb1da | 16 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 17 | */ |
math991e | 0:18d442efc99a | 18 | soundsensor::soundsensor(PinName pin){ |
math991e | 1:29daa2bdd0c6 | 19 | soundsensor::setPin(pin); |
math991e | 0:18d442efc99a | 20 | } |
math991e | 0:18d442efc99a | 21 | |
math991e | 8:5b4b258eb1da | 22 | /** |
math991e | 8:5b4b258eb1da | 23 | * Set function to set pin where sound sensor is placed. |
math991e | 8:5b4b258eb1da | 24 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 25 | * @param pin The pin that the sound sensor is placed |
math991e | 8:5b4b258eb1da | 26 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 27 | */ |
math991e | 0:18d442efc99a | 28 | void soundsensor::setPin(PinName pin){ |
math991e | 0:18d442efc99a | 29 | this->_pin=pin; |
math991e | 0:18d442efc99a | 30 | } |
math991e | 0:18d442efc99a | 31 | |
math991e | 8:5b4b258eb1da | 32 | |
math991e | 8:5b4b258eb1da | 33 | /** |
math991e | 8:5b4b258eb1da | 34 | * Get function to get pin where sound sensor is placed. |
math991e | 8:5b4b258eb1da | 35 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 36 | * @return pin The pin that the sound sensor is placed |
math991e | 8:5b4b258eb1da | 37 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 38 | */ |
math991e | 0:18d442efc99a | 39 | PinName soundsensor::getPin(){ |
math991e | 0:18d442efc99a | 40 | return this->_pin; |
math991e | 0:18d442efc99a | 41 | } |
math991e | 0:18d442efc99a | 42 | |
math991e | 8:5b4b258eb1da | 43 | /** |
math991e | 8:5b4b258eb1da | 44 | * Function to convert sound sensor input to DB. |
math991e | 8:5b4b258eb1da | 45 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 46 | * @input value Input value from sound sensor. |
math991e | 8:5b4b258eb1da | 47 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 48 | */ |
math991e | 0:18d442efc99a | 49 | float soundsensor::convertToDb(float value){ |
math991e | 6:fb51a4b71646 | 50 | return 16.801 * log(value/65535) + 9.872; |
math991e | 0:18d442efc99a | 51 | } |
math991e | 0:18d442efc99a | 52 | |
math991e | 8:5b4b258eb1da | 53 | |
math991e | 8:5b4b258eb1da | 54 | /** |
math991e | 8:5b4b258eb1da | 55 | * Function to convert sound sensor DB to raw input. |
math991e | 8:5b4b258eb1da | 56 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 57 | * @input value DB value from sound sensor. |
math991e | 8:5b4b258eb1da | 58 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 59 | */ |
math991e | 0:18d442efc99a | 60 | float soundsensor::revertFromDb(float value){ |
math991e | 0:18d442efc99a | 61 | /** |
math991e | 0:18d442efc99a | 62 | *CODE NEEDS TO BE IMPLEMENTED |
math991e | 0:18d442efc99a | 63 | **/ |
math991e | 1:29daa2bdd0c6 | 64 | return 0; |
math991e | 0:18d442efc99a | 65 | } |
math991e | 0:18d442efc99a | 66 | |
math991e | 8:5b4b258eb1da | 67 | |
math991e | 8:5b4b258eb1da | 68 | /** |
math991e | 8:5b4b258eb1da | 69 | * Takes raw input from sound sensor returns Db or raw input depending of input parameter in function. |
math991e | 8:5b4b258eb1da | 70 | * @author Nikolaj M. & Mathias R. |
math991e | 8:5b4b258eb1da | 71 | * @input toDb value that sets to true or false depending if return should be db og raw input. |
math991e | 8:5b4b258eb1da | 72 | * @date 23/1/2019 |
math991e | 8:5b4b258eb1da | 73 | */ |
math991e | 0:18d442efc99a | 74 | float soundsensor::listen(bool toDb){ |
math991e | 1:29daa2bdd0c6 | 75 | AnalogIn sensor(this->getPin()); |
math991e | 9:c38836e5d1f5 | 76 | int values[100 + 1]; |
math991e | 6:fb51a4b71646 | 77 | float sum; |
math991e | 6:fb51a4b71646 | 78 | float average; |
math991e | 3:372e67c123c3 | 79 | |
math991e | 8:5b4b258eb1da | 80 | ///Run through for loop to get average of 100 readings to make an more avarage calculation |
math991e | 3:372e67c123c3 | 81 | for(int i=0;i<100;i++){ |
math991e | 8:5b4b258eb1da | 82 | ///This calculation of db is not precise and reliable |
math991e | 8:5b4b258eb1da | 83 | values[i] = soundSensor.read() * 3.3; |
math991e | 3:372e67c123c3 | 84 | wait(0.0001); |
math991e | 3:372e67c123c3 | 85 | } |
math991e | 3:372e67c123c3 | 86 | |
math991e | 3:372e67c123c3 | 87 | for(int j=0;j<100;j++){ |
math991e | 3:372e67c123c3 | 88 | sum += values[j]; |
math991e | 3:372e67c123c3 | 89 | } |
math991e | 3:372e67c123c3 | 90 | average = sum/100; |
math991e | 8:5b4b258eb1da | 91 | |
math991e | 8:5b4b258eb1da | 92 | ///This returns either db or raw input depending of paramater input of function |
math991e | 0:18d442efc99a | 93 | if(toDb){ |
math991e | 3:372e67c123c3 | 94 | return this->convertToDb(average); |
math991e | 0:18d442efc99a | 95 | }else{ |
math991e | 3:372e67c123c3 | 96 | return average; |
math991e | 0:18d442efc99a | 97 | } |
math991e | 0:18d442efc99a | 98 | } |