lora's drivers taken out of examples repo to make a smaller binary
Dependencies: libxDot-mbed5
src/main.cpp@0:973a5bbb2a17, 2019-11-21 (annotated)
- Committer:
- guoxsharon
- Date:
- Thu Nov 21 08:46:20 2019 +0000
- Revision:
- 0:973a5bbb2a17
- Child:
- 1:80bc4501abc5
Sharon_microphone1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guoxsharon | 0:973a5bbb2a17 | 1 | |
guoxsharon | 0:973a5bbb2a17 | 2 | #include "mbed.h" |
guoxsharon | 0:973a5bbb2a17 | 3 | #define u16_max 65526 |
guoxsharon | 0:973a5bbb2a17 | 4 | AnalogIn mic(PB_0); |
guoxsharon | 0:973a5bbb2a17 | 5 | DigitalOut vcc(GPIO0); |
guoxsharon | 0:973a5bbb2a17 | 6 | Serial pc(UART_TX, UART_RX, 115200); |
guoxsharon | 0:973a5bbb2a17 | 7 | const int sampleTime = 50; |
guoxsharon | 0:973a5bbb2a17 | 8 | int micOut; |
guoxsharon | 0:973a5bbb2a17 | 9 | // Find the Peak-to-Peak Amplitude Function |
guoxsharon | 0:973a5bbb2a17 | 10 | int findPTPAmp(float* sampleArr){ |
guoxsharon | 0:973a5bbb2a17 | 11 | // Time variables to find the peak-to-peak amplitude |
guoxsharon | 0:973a5bbb2a17 | 12 | |
guoxsharon | 0:973a5bbb2a17 | 13 | unsigned int PTPAmp = 0; |
guoxsharon | 0:973a5bbb2a17 | 14 | |
guoxsharon | 0:973a5bbb2a17 | 15 | // Signal variables to find the peak-to-peak amplitude |
guoxsharon | 0:973a5bbb2a17 | 16 | unsigned int maxAmp = 0; |
guoxsharon | 0:973a5bbb2a17 | 17 | unsigned int minAmp = 1023; |
guoxsharon | 0:973a5bbb2a17 | 18 | |
guoxsharon | 0:973a5bbb2a17 | 19 | // Find the max and min of the mic output within the 50 ms timeframe |
guoxsharon | 0:973a5bbb2a17 | 20 | for(int i=0; i<8000; i++) |
guoxsharon | 0:973a5bbb2a17 | 21 | { |
guoxsharon | 0:973a5bbb2a17 | 22 | if( sampleArr[i] < 1023) //prevent erroneous readings |
guoxsharon | 0:973a5bbb2a17 | 23 | { |
guoxsharon | 0:973a5bbb2a17 | 24 | if (sampleArr[i] > maxAmp) |
guoxsharon | 0:973a5bbb2a17 | 25 | { |
guoxsharon | 0:973a5bbb2a17 | 26 | maxAmp = sampleArr[i]; //save only the max reading |
guoxsharon | 0:973a5bbb2a17 | 27 | } |
guoxsharon | 0:973a5bbb2a17 | 28 | else if (sampleArr[i] < minAmp) |
guoxsharon | 0:973a5bbb2a17 | 29 | { |
guoxsharon | 0:973a5bbb2a17 | 30 | minAmp = sampleArr[i]; //save only the min reading |
guoxsharon | 0:973a5bbb2a17 | 31 | } |
guoxsharon | 0:973a5bbb2a17 | 32 | } |
guoxsharon | 0:973a5bbb2a17 | 33 | } |
guoxsharon | 0:973a5bbb2a17 | 34 | |
guoxsharon | 0:973a5bbb2a17 | 35 | PTPAmp = maxAmp - minAmp; // (max amp) - (min amp) = peak-to-peak amplitude |
guoxsharon | 0:973a5bbb2a17 | 36 | double micOut_Volts = (PTPAmp * 3.3) / 1024; // Convert ADC into voltage |
guoxsharon | 0:973a5bbb2a17 | 37 | |
guoxsharon | 0:973a5bbb2a17 | 38 | //Uncomment this line for help debugging (be sure to also comment out the VUMeter function) |
guoxsharon | 0:973a5bbb2a17 | 39 | //Serial.println(PTPAmp); |
guoxsharon | 0:973a5bbb2a17 | 40 | |
guoxsharon | 0:973a5bbb2a17 | 41 | //Return the PTP amplitude to use in the soundLevel function. |
guoxsharon | 0:973a5bbb2a17 | 42 | // You can also return the micOut_Volts if you prefer to use the voltage level. |
guoxsharon | 0:973a5bbb2a17 | 43 | return PTPAmp; |
guoxsharon | 0:973a5bbb2a17 | 44 | } |
guoxsharon | 0:973a5bbb2a17 | 45 | |
guoxsharon | 0:973a5bbb2a17 | 46 | void record(float* sampleArr) { |
guoxsharon | 0:973a5bbb2a17 | 47 | //record sound for 1 second |
guoxsharon | 0:973a5bbb2a17 | 48 | for(int i=0; i<8000; i++) { |
guoxsharon | 0:973a5bbb2a17 | 49 | sampleArr[i] = mic.read(); //put samples in array |
guoxsharon | 0:973a5bbb2a17 | 50 | wait(0.000125f); //sample rate of 8000 Hz |
guoxsharon | 0:973a5bbb2a17 | 51 | } |
guoxsharon | 0:973a5bbb2a17 | 52 | } |
guoxsharon | 0:973a5bbb2a17 | 53 | |
guoxsharon | 0:973a5bbb2a17 | 54 | int main(){ |
guoxsharon | 0:973a5bbb2a17 | 55 | float sampleArr[8000]; //used to store sound |
guoxsharon | 0:973a5bbb2a17 | 56 | pc.printf("\r\n Sparkfun MEM Microphone Test\n"); |
guoxsharon | 0:973a5bbb2a17 | 57 | pc.printf("******************\n"); |
guoxsharon | 0:973a5bbb2a17 | 58 | vcc = 1; |
guoxsharon | 0:973a5bbb2a17 | 59 | unsigned int val; |
guoxsharon | 0:973a5bbb2a17 | 60 | int ptpAmp; |
guoxsharon | 0:973a5bbb2a17 | 61 | while(1){ |
guoxsharon | 0:973a5bbb2a17 | 62 | wait(0.2); |
guoxsharon | 0:973a5bbb2a17 | 63 | record(sampleArr); |
guoxsharon | 0:973a5bbb2a17 | 64 | ptpAmp=findPTPAmp(sampleArr); |
guoxsharon | 0:973a5bbb2a17 | 65 | } |
guoxsharon | 0:973a5bbb2a17 | 66 | } |
guoxsharon | 0:973a5bbb2a17 | 67 | |
guoxsharon | 0:973a5bbb2a17 | 68 | |
guoxsharon | 0:973a5bbb2a17 | 69 |