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.
GROVE_SOUND.cpp@0:fa75a7cf49b6, 2017-07-02 (annotated)
- Committer:
- ngomez
- Date:
- Sun Jul 02 11:33:55 2017 +0000
- Revision:
- 0:fa75a7cf49b6
Library for grove sound sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ngomez | 0:fa75a7cf49b6 | 1 | #include "GROVE_SOUND.h" |
ngomez | 0:fa75a7cf49b6 | 2 | #include "mbed.h" |
ngomez | 0:fa75a7cf49b6 | 3 | |
ngomez | 0:fa75a7cf49b6 | 4 | GROVE_SOUND::GROVE_SOUND(PinName pin) : _pin(pin){ |
ngomez | 0:fa75a7cf49b6 | 5 | sum = 0; |
ngomez | 0:fa75a7cf49b6 | 6 | average = 0; |
ngomez | 0:fa75a7cf49b6 | 7 | } |
ngomez | 0:fa75a7cf49b6 | 8 | |
ngomez | 0:fa75a7cf49b6 | 9 | float GROVE_SOUND::get_decibels(){ |
ngomez | 0:fa75a7cf49b6 | 10 | |
ngomez | 0:fa75a7cf49b6 | 11 | float values[1000]; |
ngomez | 0:fa75a7cf49b6 | 12 | for(int i=0;i<1000;i++){ |
ngomez | 0:fa75a7cf49b6 | 13 | values[i] = _pin.read()* 3.3; |
ngomez | 0:fa75a7cf49b6 | 14 | wait(0.0001); |
ngomez | 0:fa75a7cf49b6 | 15 | } |
ngomez | 0:fa75a7cf49b6 | 16 | |
ngomez | 0:fa75a7cf49b6 | 17 | for(int j=0;j<1000;j++){ |
ngomez | 0:fa75a7cf49b6 | 18 | sum += values[j]; |
ngomez | 0:fa75a7cf49b6 | 19 | } |
ngomez | 0:fa75a7cf49b6 | 20 | average = sum/1000; |
ngomez | 0:fa75a7cf49b6 | 21 | decibels = average * 29; |
ngomez | 0:fa75a7cf49b6 | 22 | return decibels; |
ngomez | 0:fa75a7cf49b6 | 23 | } |