Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MAX4466.cpp
00001 #include "mbed.h" 00002 #include "MAX4466.h" 00003 00004 MAX4466::MAX4466(PinName pin): _pin(pin), _led1(LED1), _led2(LED2), _led3(LED3), _led4(LED4) { 00005 calibration(); 00006 } 00007 00008 //CALIBRATION FUNCTION: 00009 //Find average sound level at the intialization of the microphone 00010 //Use this in the calculation of the LED indication array values 00011 float MAX4466::calibration() { 00012 00013 _t.start(); 00014 _t1.start(); 00015 float peakToPeak=0, signalMax=0, signalMin=1024; 00016 00017 while (_t1.read()<1) { 00018 while (_t.read_ms()<50) { 00019 _sample=_pin.read(); 00020 00021 if (_sample<1024) { 00022 if (_sample>signalMax) 00023 signalMax=_sample; 00024 00025 else if (_sample<signalMin) 00026 signalMin=_sample; 00027 } 00028 } 00029 _t.reset(); 00030 peakToPeak=signalMax-signalMin; 00031 _value= (peakToPeak*3.3); 00032 _value = floor(_value * 100) / 100; 00033 _sum+=_value; 00034 _count++; 00035 } 00036 _average=_sum/_count; 00037 _t1.reset(); 00038 00039 return _average; 00040 } 00041 00042 //LED ARRAY FUNCTION: 00043 //Setup array of 4 LEDs based on the current value read by the microphone 00044 //And the average value found during calibration 00045 void MAX4466::led_array(float x ) { 00046 00047 if (_value<x+0.05) { 00048 _led1=0; 00049 _led2=0; 00050 _led3=0; 00051 _led4=0; 00052 } 00053 if (_value>x+0.05&&_value<0.5+x) { 00054 _led1=1; 00055 _led2=0; 00056 _led3=0; 00057 _led4=0; 00058 } 00059 if (_value>0.5+x&&_value<1+x) { 00060 _led1=1; 00061 _led2=1; 00062 _led3=0; 00063 _led4=0; 00064 } 00065 if (_value>1+x&&_value<1.2+x) { 00066 _led1=1; 00067 _led2=1; 00068 _led3=1; 00069 _led4=0; 00070 } 00071 if (_value>1.2+x&&_value<2.8+x) { 00072 _led1=1; 00073 _led2=1; 00074 _led3=1; 00075 _led4=1; 00076 } 00077 } 00078 00079 //SOUND LEVEL FUNCTIOM: 00080 //Read in current sound level 00081 float MAX4466::sound_level() { 00082 00083 _t.start(); 00084 float peakToPeak=0, signalMax=0, signalMin=1024; 00085 00086 while (_t.read_ms()<50) { 00087 _sample=_pin.read(); 00088 00089 if (_sample<1024) { 00090 if (_sample>signalMax) 00091 signalMax=_sample; 00092 00093 else if (_sample<signalMin) 00094 signalMin=_sample; 00095 } 00096 } 00097 _t.reset(); 00098 peakToPeak=signalMax-signalMin; 00099 _value= (peakToPeak*3.3); 00100 _value = floor(_value * 100) / 100; 00101 _sum+=_value; 00102 _count++; 00103 00104 return _value; 00105 } 00106 00107 float MAX4466::instantlevel(){ 00108 return _pin.read(); 00109 } 00110 00111 //VOLUME INDICATOR FUNCTION: 00112 //To be called in main function 00113 void MAX4466::volume_indicator() { 00114 led_array(_average); 00115 sound_level(); 00116 }
Generated on Fri Jul 22 2022 09:01:16 by
1.7.2