part1

Dependencies:   mbed

Committer:
psahay
Date:
Tue Feb 17 18:55:46 2015 +0000
Revision:
3:664e38d7ae34
Parent:
2:255d7e71c69b
Child:
4:34df824930f6
testing putty echo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaredwil 0:2c98a4766d9f 1 #include "mbed.h"
jaredwil 2:255d7e71c69b 2 //Right now this is only working for inputs length 3
jaredwil 0:2c98a4766d9f 3
jaredwil 0:2c98a4766d9f 4 //Initialize PWM
jaredwil 0:2c98a4766d9f 5 PwmOut PWM1(p21);
jaredwil 0:2c98a4766d9f 6 Serial pc(USBTX, USBRX);
jaredwil 1:93eb2c653c86 7 Timer t1;
jaredwil 0:2c98a4766d9f 8
jaredwil 0:2c98a4766d9f 9
jaredwil 1:93eb2c653c86 10 //Initialize Input that is 8 character
jaredwil 2:255d7e71c69b 11 char input[10];
jaredwil 1:93eb2c653c86 12
jaredwil 1:93eb2c653c86 13
jaredwil 1:93eb2c653c86 14 int main()
jaredwil 0:2c98a4766d9f 15 {
jaredwil 2:255d7e71c69b 16 //define input variables
jaredwil 1:93eb2c653c86 17 float freq;
jaredwil 1:93eb2c653c86 18 int dur;
jaredwil 2:255d7e71c69b 19 //Check if input is valid
jaredwil 1:93eb2c653c86 20 int isint;
jaredwil 0:2c98a4766d9f 21 while(1){
jaredwil 1:93eb2c653c86 22
jaredwil 1:93eb2c653c86 23 //wait for a valid input
jaredwil 1:93eb2c653c86 24 while(isint != 2)
jaredwil 1:93eb2c653c86 25 {
jaredwil 1:93eb2c653c86 26 //Input Desired Tone
jaredwil 1:93eb2c653c86 27 printf("Input Desired Tone: Frequency (Hz) Duration (ms) followed by enter\r");
psahay 3:664e38d7ae34 28 char c;
psahay 3:664e38d7ae34 29 while((c = pc.getc()) != '\r') {
psahay 3:664e38d7ae34 30 printf("%c",c);
psahay 3:664e38d7ae34 31 c = pc.getc();
psahay 3:664e38d7ae34 32 }
jaredwil 2:255d7e71c69b 33 fgets(input, sizeof input, stdin); //input size length 9 followed by enter (xxxx xxxx/r)
jaredwil 2:255d7e71c69b 34 isint = sscanf(input, "%f %d",&freq,&dur); //assign input variables
jaredwil 2:255d7e71c69b 35 printf("isint = %d\r",isint); //debug check if input is correct
jaredwil 2:255d7e71c69b 36 //if input is incorrect this will be displayed
jaredwil 1:93eb2c653c86 37 if(isint!= 2)
jaredwil 1:93eb2c653c86 38 printf("You did not enter a number.Please enter an argument's number\r");
jaredwil 1:93eb2c653c86 39
jaredwil 1:93eb2c653c86 40 }
jaredwil 1:93eb2c653c86 41
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 1:93eb2c653c86 54 PWM1.period(1/freq); // set PWM period to 10 ms
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
jaredwil 1:93eb2c653c86 60 t1.stop();
jaredwil 1:93eb2c653c86 61
jaredwil 1:93eb2c653c86 62 }
jaredwil 1:93eb2c653c86 63 //if anyother button is pressed abort and start over
jaredwil 1:93eb2c653c86 64 else{
jaredwil 1:93eb2c653c86 65 printf("You didn't press enter you idiot\r");
jaredwil 1:93eb2c653c86 66 isint = 0;
jaredwil 1:93eb2c653c86 67 }
jaredwil 1:93eb2c653c86 68 }
jaredwil 1:93eb2c653c86 69 }
jaredwil 1:93eb2c653c86 70
jaredwil 1:93eb2c653c86 71 }