Emine Acar / Mbed 2 deprecated Sound_Level_Meter1

Dependencies:   mbed PeripheralNames

main.cpp

Committer:
emineacar
Date:
2019-12-07
Revision:
0:1f53ece35c14

File content as of revision 0:1f53ece35c14:

#include "mbed.h"
#include"PeripheralNames.h"
#include"PeripheralPins.h"
#include"PinNames.h"

    Serial serial(PA_2, PA_3,9600); 
    AnalogIn Sound(PB_0); 
    Timer tim;
    // Clear the screen // 
    void clrscr()
    {    
    char clrscr[] = {0x1B, '[', '2' , 'J',0};    
    serial.printf(clrscr);

     }
    // // Goto specified line and column // 
    void gotoscr(int line, int column) 
    {    char scr[] = {0x1B, '[', 0x00, ';' ,0x00, 'H', 0};    
    scr[2] = line;    
    scr[4] = column;   
     serial.printf(scr); 
    }

    int main() {   
    double mV;   
    float SMax = 0,Peak,SMin = 4096;    
    // // Read the peak-topeak output voltage of the audio amplifier, 
    // then display the voltage on the PC screen //   
    while(1)                                         // Do forever
    {   
        tim.reset();                                 // Reset Timer       
        tim.start();                                // Start Timer       
        SMax = 0.0;      
        SMin = 3000.0;
        while(tim.read_ms() < 50)                    // Do for 50ms      
        {    
            mV = 3000.0f * Sound.read();            // In mV            
            if(mV > SMax)                           // Find Max               
             SMax = mV;            
             else if(mV < SMin)                      // Find Min               
                SMin = mV;        
        }              
                 Peak = SMax - SMin;                          // Peak-to-peak      
                 tim.stop();                                  // Stop Timer      
                clrscr();                                    // Clear screen      
                gotoscr('4', '0');                           // Line 4, col 0     
                serial.printf("Sound Level = %5.2f", Peak);   // Display      
                                                   // Wait 1 second  

    } 
    
  }