Subhradeep Dutta / Mbed 2 deprecated Nucleo_Audio

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*----------------------------------------------------------------------------
00002 LAB EXERCISE 9 - Analog input and PWM
00003  ----------------------------------------
00004     Use two potentiometers to adjust the volume and pitch of the output sound wave.
00005     
00006     Inputs: Virtual potentiometers 1 and 2
00007     Output: Virtual speaker,  Real PC
00008 
00009     GOOD LUCK!
00010  *----------------------------------------------------------------------------*/
00011 
00012 #include "mbed.h"
00013 
00014 
00015 /*
00016 Define the PWM speaker output
00017 Define analog inputs
00018 Define serial output
00019 */
00020 #define POT1 (PA_0)         // Analog input for potentiomenter 1: volume 
00021 #define POT2 (PA_1)         // Analog input for potentiometer 2: pitch
00022 #define PWM_SPEAKER (PB_10)     // output pin connected to speaker
00023 
00024 float constant1 = 0.003;        // within range 320Hz to 8kHz
00025 float constant2 = 0.000125;
00026 
00027 AnalogIn pot1(POT1);            //Create an AnalogIn , connected to the specified pin
00028 AnalogIn pot2(POT2);
00029 
00030 float pot1_value;           // To read values from potentiometers
00031 float pot2_value;
00032 
00033 PwmOut speaker(PWM_SPEAKER);
00034 Serial serial_output(USBTX, USBRX);
00035 /*----------------------------------------------------------------------------
00036  MAIN function
00037  *----------------------------------------------------------------------------*/
00038     
00039 int main() 
00040 {
00041     
00042     double i = 0;
00043     while(1)
00044     {
00045     // Read values from the potentiometers
00046         pot1_value = pot1.read();
00047         pot2_value = pot2.read();
00048         printf("\n\r%f %f",pot1_value, pot2_value);         // Print potentiometer values
00049         wait(0.5);
00050         
00051         for(i=0; i<pot1_value; i+=0.05)                 // adjusting the PWM steps. s
00052         {
00053             speaker = i;
00054             speaker.period(constant1*pot2_value + constant2);       // variations in pitch is given to output
00055         }
00056         speaker.write(pot1_value);                  // volume output given to speaker
00057         
00058     }
00059 }
00060 // *******************************ARM University Program Copyright � ARM Ltd 2014*************************************
00061 
00062