part1

Dependencies:   mbed

Committer:
psahay
Date:
Sun Feb 22 20:30:58 2015 +0000
Revision:
9:58c1acf33117
Parent:
8:8991b0b82440
minor changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaredwil 0:2c98a4766d9f 1 #include "mbed.h"
jaredwil 0:2c98a4766d9f 2
jaredwil 0:2c98a4766d9f 3 //Initialize PWM
jaredwil 0:2c98a4766d9f 4 PwmOut PWM1(p21);
jaredwil 0:2c98a4766d9f 5 Serial pc(USBTX, USBRX);
jaredwil 1:93eb2c653c86 6 Timer t1;
jaredwil 0:2c98a4766d9f 7
jaredwil 0:2c98a4766d9f 8
jaredwil 7:89a6d9004710 9 //Initialize Input buffer
jaredwil 2:255d7e71c69b 10 char input[10];
jaredwil 1:93eb2c653c86 11
jaredwil 1:93eb2c653c86 12
jaredwil 1:93eb2c653c86 13 int main()
jaredwil 0:2c98a4766d9f 14 {
jaredwil 2:255d7e71c69b 15 //define input variables
jaredwil 1:93eb2c653c86 16 float freq;
jaredwil 1:93eb2c653c86 17 int dur;
jaredwil 2:255d7e71c69b 18 //Check if input is valid
jaredwil 1:93eb2c653c86 19 int isint;
jaredwil 0:2c98a4766d9f 20 while(1){
jaredwil 1:93eb2c653c86 21 //wait for a valid input
psahay 9:58c1acf33117 22 while(isint != 2) {
jaredwil 1:93eb2c653c86 23 //Input Desired Tone
jaredwil 1:93eb2c653c86 24 printf("Input Desired Tone: Frequency (Hz) Duration (ms) followed by enter\r");
jaredwil 4:34df824930f6 25 char c = pc.getc();
jaredwil 4:34df824930f6 26 int i =0;
jaredwil 7:89a6d9004710 27
jaredwil 7:89a6d9004710 28 //wait for return to be pressed
jaredwil 4:34df824930f6 29 while(c != '\r') {
jaredwil 7:89a6d9004710 30 printf("%c",c); //echo
jaredwil 7:89a6d9004710 31 input[i++]=c; //add current char to buffer
psahay 3:664e38d7ae34 32 c = pc.getc();
psahay 3:664e38d7ae34 33 }
jaredwil 7:89a6d9004710 34 input[i] = '\0'; //end buffer
jaredwil 2:255d7e71c69b 35 isint = sscanf(input, "%f %d",&freq,&dur); //assign input variables
jaredwil 5:cb200e69a544 36 //printf("isint = %d\r",isint); //debug check if input is correct
jaredwil 2:255d7e71c69b 37 //if input is incorrect this will be displayed
jaredwil 1:93eb2c653c86 38 if(isint!= 2)
jaredwil 1:93eb2c653c86 39 printf("You did not enter a number.Please enter an argument's number\r");
jaredwil 1:93eb2c653c86 40 }
jaredwil 7:89a6d9004710 41 //wait for another enter to output the tone
jaredwil 1:93eb2c653c86 42 if(pc.readable()) {
jaredwil 2:255d7e71c69b 43 //if enter is pressed continue to output tone
jaredwil 1:93eb2c653c86 44 if(pc.getc() == '\r')
jaredwil 1:93eb2c653c86 45 {
jaredwil 2:255d7e71c69b 46 //show user what value was input
jaredwil 1:93eb2c653c86 47 printf("Valid Input Frequency = %3.0f, Duration = %d\r", freq, dur);
jaredwil 1:93eb2c653c86 48 isint = 0; //reset input
jaredwil 1:93eb2c653c86 49
jaredwil 2:255d7e71c69b 50 //Play tone with correct freq and durration
jaredwil 2:255d7e71c69b 51 //Timer used for duration
jaredwil 1:93eb2c653c86 52 t1.reset();
jaredwil 1:93eb2c653c86 53 t1.start();
jaredwil 6:ab02d7bb40cb 54 PWM1.period(1/freq); // set PWM period to user specified
jaredwil 1:93eb2c653c86 55 PWM1=0.5; // set duty cycle to 50%
jaredwil 1:93eb2c653c86 56 while(t1.read_ms() < dur){
jaredwil 1:93eb2c653c86 57 //WAIT
jaredwil 0:2c98a4766d9f 58 }
jaredwil 1:93eb2c653c86 59 PWM1=0.0; //turn off
psahay 9:58c1acf33117 60 t1.stop();
jaredwil 1:93eb2c653c86 61 }
jaredwil 7:89a6d9004710 62 //if another button is pressed besides enter abort and start over
jaredwil 1:93eb2c653c86 63 else{
psahay 9:58c1acf33117 64 printf("You didn't press enter\r");
jaredwil 1:93eb2c653c86 65 isint = 0;
jaredwil 1:93eb2c653c86 66 }
jaredwil 1:93eb2c653c86 67 }
jaredwil 1:93eb2c653c86 68 }
jaredwil 1:93eb2c653c86 69
jaredwil 1:93eb2c653c86 70 }