Final Code for Nucleo Audio

Dependencies:   mbed

Committer:
subhradeep
Date:
Tue Mar 13 01:08:36 2018 +0000
Revision:
0:41c4dd155646
Final code for Nucleo Audio

Who changed what in which revision?

UserRevisionLine numberNew contents of line
subhradeep 0:41c4dd155646 1 /*----------------------------------------------------------------------------
subhradeep 0:41c4dd155646 2 LAB EXERCISE 9 - Analog input and PWM
subhradeep 0:41c4dd155646 3 ----------------------------------------
subhradeep 0:41c4dd155646 4 Use two potentiometers to adjust the volume and pitch of the output sound wave.
subhradeep 0:41c4dd155646 5
subhradeep 0:41c4dd155646 6 Inputs: Virtual potentiometers 1 and 2
subhradeep 0:41c4dd155646 7 Output: Virtual speaker, Real PC
subhradeep 0:41c4dd155646 8
subhradeep 0:41c4dd155646 9 GOOD LUCK!
subhradeep 0:41c4dd155646 10 *----------------------------------------------------------------------------*/
subhradeep 0:41c4dd155646 11
subhradeep 0:41c4dd155646 12 #include "mbed.h"
subhradeep 0:41c4dd155646 13
subhradeep 0:41c4dd155646 14
subhradeep 0:41c4dd155646 15 /*
subhradeep 0:41c4dd155646 16 Define the PWM speaker output
subhradeep 0:41c4dd155646 17 Define analog inputs
subhradeep 0:41c4dd155646 18 Define serial output
subhradeep 0:41c4dd155646 19 */
subhradeep 0:41c4dd155646 20 #define POT1 (PA_0) // Analog input for potentiomenter 1: volume
subhradeep 0:41c4dd155646 21 #define POT2 (PA_1) // Analog input for potentiometer 2: pitch
subhradeep 0:41c4dd155646 22 #define PWM_SPEAKER (PB_10) // output pin connected to speaker
subhradeep 0:41c4dd155646 23
subhradeep 0:41c4dd155646 24 float constant1 = 0.003; // within range 320Hz to 8kHz
subhradeep 0:41c4dd155646 25 float constant2 = 0.000125;
subhradeep 0:41c4dd155646 26
subhradeep 0:41c4dd155646 27 AnalogIn pot1(POT1); //Create an AnalogIn , connected to the specified pin
subhradeep 0:41c4dd155646 28 AnalogIn pot2(POT2);
subhradeep 0:41c4dd155646 29
subhradeep 0:41c4dd155646 30 float pot1_value; // To read values from potentiometers
subhradeep 0:41c4dd155646 31 float pot2_value;
subhradeep 0:41c4dd155646 32
subhradeep 0:41c4dd155646 33 PwmOut speaker(PWM_SPEAKER);
subhradeep 0:41c4dd155646 34 Serial serial_output(USBTX, USBRX);
subhradeep 0:41c4dd155646 35 /*----------------------------------------------------------------------------
subhradeep 0:41c4dd155646 36 MAIN function
subhradeep 0:41c4dd155646 37 *----------------------------------------------------------------------------*/
subhradeep 0:41c4dd155646 38
subhradeep 0:41c4dd155646 39 int main()
subhradeep 0:41c4dd155646 40 {
subhradeep 0:41c4dd155646 41
subhradeep 0:41c4dd155646 42 double i = 0;
subhradeep 0:41c4dd155646 43 while(1)
subhradeep 0:41c4dd155646 44 {
subhradeep 0:41c4dd155646 45 // Read values from the potentiometers
subhradeep 0:41c4dd155646 46 pot1_value = pot1.read();
subhradeep 0:41c4dd155646 47 pot2_value = pot2.read();
subhradeep 0:41c4dd155646 48 printf("\n\r%f %f",pot1_value, pot2_value); // Print potentiometer values
subhradeep 0:41c4dd155646 49 wait(0.5);
subhradeep 0:41c4dd155646 50
subhradeep 0:41c4dd155646 51 for(i=0; i<pot1_value; i+=0.05) // adjusting the PWM steps. s
subhradeep 0:41c4dd155646 52 {
subhradeep 0:41c4dd155646 53 speaker = i;
subhradeep 0:41c4dd155646 54 speaker.period(constant1*pot2_value + constant2); // variations in pitch is given to output
subhradeep 0:41c4dd155646 55 }
subhradeep 0:41c4dd155646 56 speaker.write(pot1_value); // volume output given to speaker
subhradeep 0:41c4dd155646 57
subhradeep 0:41c4dd155646 58 }
subhradeep 0:41c4dd155646 59 }
subhradeep 0:41c4dd155646 60 // *******************************ARM University Program Copyright � ARM Ltd 2014*************************************
subhradeep 0:41c4dd155646 61
subhradeep 0:41c4dd155646 62