Grove soundsensor lib
Embed:
(wiki syntax)
Show/hide line numbers
soundsensor.cpp
Go to the documentation of this file.
00001 /** 00002 * @file soundsensor.cpp 00003 * @brief this cpp file is where all the logic is handled. 00004 * 00005 * @author Nikolaj M. & Mathias R. 00006 * 00007 * @date 23/1/2019 00008 */ 00009 00010 #include "soundsensor.h" 00011 00012 /** 00013 * Constructor that sets pin to to take analog input form. 00014 * @author Nikolaj M. & Mathias R. 00015 * @param pin The pin that the sound sensor is placed 00016 * @date 23/1/2019 00017 */ 00018 soundsensor::soundsensor(PinName pin){ 00019 soundsensor::setPin(pin); 00020 } 00021 00022 /** 00023 * Set function to set pin where sound sensor is placed. 00024 * @author Nikolaj M. & Mathias R. 00025 * @param pin The pin that the sound sensor is placed 00026 * @date 23/1/2019 00027 */ 00028 void soundsensor::setPin(PinName pin){ 00029 this->_pin=pin; 00030 } 00031 00032 00033 /** 00034 * Get function to get pin where sound sensor is placed. 00035 * @author Nikolaj M. & Mathias R. 00036 * @return pin The pin that the sound sensor is placed 00037 * @date 23/1/2019 00038 */ 00039 PinName soundsensor::getPin(){ 00040 return this->_pin; 00041 } 00042 00043 /** 00044 * Function to convert sound sensor input to DB. 00045 * @author Nikolaj M. & Mathias R. 00046 * @input value Input value from sound sensor. 00047 * @date 23/1/2019 00048 */ 00049 float soundsensor::convertToDb(float value){ 00050 return 16.801 * log(value/65535) + 9.872; 00051 } 00052 00053 00054 /** 00055 * Function to convert sound sensor DB to raw input. 00056 * @author Nikolaj M. & Mathias R. 00057 * @input value DB value from sound sensor. 00058 * @date 23/1/2019 00059 */ 00060 float soundsensor::revertFromDb(float value){ 00061 /** 00062 *CODE NEEDS TO BE IMPLEMENTED 00063 **/ 00064 return 0; 00065 } 00066 00067 00068 /** 00069 * Takes raw input from sound sensor returns Db or raw input depending of input parameter in function. 00070 * @author Nikolaj M. & Mathias R. 00071 * @input toDb value that sets to true or false depending if return should be db og raw input. 00072 * @date 23/1/2019 00073 */ 00074 float soundsensor::listen(bool toDb){ 00075 AnalogIn sensor(this->getPin()); 00076 int values[100 + 1]; 00077 float sum; 00078 float average; 00079 00080 ///Run through for loop to get average of 100 readings to make an more avarage calculation 00081 for(int i=0;i<100;i++){ 00082 ///This calculation of db is not precise and reliable 00083 values[i] = soundSensor.read() * 3.3; 00084 wait(0.0001); 00085 } 00086 00087 for(int j=0;j<100;j++){ 00088 sum += values[j]; 00089 } 00090 average = sum/100; 00091 00092 ///This returns either db or raw input depending of paramater input of function 00093 if(toDb){ 00094 return this->convertToDb(average); 00095 }else{ 00096 return average; 00097 } 00098 }
Generated on Mon Jul 25 2022 22:26:59 by
1.7.2