Charles Andre
/
serialaudio
Diff: src/main.cpp
- Revision:
- 1:a6835f6c84eb
- Parent:
- 0:973a5bbb2a17
--- a/src/main.cpp Thu Nov 21 08:46:20 2019 +0000 +++ b/src/main.cpp Sun Dec 08 05:18:34 2019 +0000 @@ -1,69 +1,40 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ +#include <stdio.h> +// Application helper #include "mbed.h" -#define u16_max 65526 -AnalogIn mic(PB_0); -DigitalOut vcc(GPIO0); -Serial pc(UART_TX, UART_RX, 115200); -const int sampleTime = 50; -int micOut; -// Find the Peak-to-Peak Amplitude Function -int findPTPAmp(float* sampleArr){ -// Time variables to find the peak-to-peak amplitude - - unsigned int PTPAmp = 0; +#include <AnalogIn.h> +#include <DigitalOut.h> + -// Signal variables to find the peak-to-peak amplitude - unsigned int maxAmp = 0; - unsigned int minAmp = 1023; +// PIN DEFINITIONS +DigitalOut vcc(GPIO0); +AnalogIn mic(PB_0); + +// To sleep, 'wait' should be replaced by 'ThisThread::sleep_for' (C++) or 'thread_sleep_for' (C). If you wish to wait (without sleeping), call 'wait_us'. 'wait_us' is safe to call from ISR context. [since mbed-os-5.14] [-Wdeprecated-declarations] in "main.cpp", Line: 59, Col: 9 -// Find the max and min of the mic output within the 50 ms timeframe - for(int i=0; i<8000; i++) - { - if( sampleArr[i] < 1023) //prevent erroneous readings - { - if (sampleArr[i] > maxAmp) - { - maxAmp = sampleArr[i]; //save only the max reading - } - else if (sampleArr[i] < minAmp) - { - minAmp = sampleArr[i]; //save only the min reading +int main() { + int ARR_SIZE = 8000; + vcc = 1; + uint16_t raw_vals[ARR_SIZE]; + int i = 0; + printf("Loading values...\n"); + while(1) { + memset(raw_vals, 0, ARR_SIZE * sizeof(uint16_t)); + i = 0; + for(i; i < ARR_SIZE; i++) { + raw_vals[i] = mic.read_u16(); } - } - } - - PTPAmp = maxAmp - minAmp; // (max amp) - (min amp) = peak-to-peak amplitude - double micOut_Volts = (PTPAmp * 3.3) / 1024; // Convert ADC into voltage - - //Uncomment this line for help debugging (be sure to also comment out the VUMeter function) - //Serial.println(PTPAmp); - - //Return the PTP amplitude to use in the soundLevel function. - // You can also return the micOut_Volts if you prefer to use the voltage level. - return PTPAmp; -} - -void record(float* sampleArr) { - //record sound for 1 second - for(int i=0; i<8000; i++) { - sampleArr[i] = mic.read(); //put samples in array - wait(0.000125f); //sample rate of 8000 Hz - } -} - -int main(){ - float sampleArr[8000]; //used to store sound - pc.printf("\r\n Sparkfun MEM Microphone Test\n"); - pc.printf("******************\n"); - vcc = 1; - unsigned int val; - int ptpAmp; - while(1){ - wait(0.2); - record(sampleArr); - ptpAmp=findPTPAmp(sampleArr); + //printf("Done reading in values\n"); + wait(1); + i = 0; + for(i; i < ARR_SIZE; i++) { + printf("%u,", raw_vals[i]); + } + + while(1); } } - - -