part1

Dependencies:   mbed

Committer:
jaredwil
Date:
Tue Feb 17 19:22:51 2015 +0000
Revision:
4:34df824930f6
Parent:
3:664e38d7ae34
Child:
5:cb200e69a544
putty echo and non-fixed buffer

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");
jaredwil 4:34df824930f6 28 char c = pc.getc();
jaredwil 4:34df824930f6 29 int i =0;
jaredwil 4:34df824930f6 30 while(c != '\r') {
psahay 3:664e38d7ae34 31 printf("%c",c);
jaredwil 4:34df824930f6 32 input[i++]=c;
psahay 3:664e38d7ae34 33 c = pc.getc();
psahay 3:664e38d7ae34 34 }
jaredwil 4:34df824930f6 35 input[i] = '\0';
jaredwil 4:34df824930f6 36 //fgets(input, sizeof input, stdin); //input size length 9 followed by enter (xxxx xxxx/r)
jaredwil 2:255d7e71c69b 37 isint = sscanf(input, "%f %d",&freq,&dur); //assign input variables
jaredwil 4:34df824930f6 38
jaredwil 2:255d7e71c69b 39 printf("isint = %d\r",isint); //debug check if input is correct
jaredwil 2:255d7e71c69b 40 //if input is incorrect this will be displayed
jaredwil 1:93eb2c653c86 41 if(isint!= 2)
jaredwil 1:93eb2c653c86 42 printf("You did not enter a number.Please enter an argument's number\r");
jaredwil 1:93eb2c653c86 43
jaredwil 1:93eb2c653c86 44 }
jaredwil 1:93eb2c653c86 45
jaredwil 1:93eb2c653c86 46 if(pc.readable()) {
jaredwil 2:255d7e71c69b 47 //if enter is pressed continue to output tone
jaredwil 1:93eb2c653c86 48 if(pc.getc() == '\r')
jaredwil 1:93eb2c653c86 49 {
jaredwil 2:255d7e71c69b 50 //show user what value was input
jaredwil 1:93eb2c653c86 51 printf("Valid Input Frequency = %3.0f, Duration = %d\r", freq, dur);
jaredwil 1:93eb2c653c86 52 isint = 0; //reset input
jaredwil 1:93eb2c653c86 53
jaredwil 2:255d7e71c69b 54 //Play tone with correct freq and durration
jaredwil 2:255d7e71c69b 55 //Timer used for duration
jaredwil 1:93eb2c653c86 56 t1.reset();
jaredwil 1:93eb2c653c86 57 t1.start();
jaredwil 1:93eb2c653c86 58 PWM1.period(1/freq); // set PWM period to 10 ms
jaredwil 1:93eb2c653c86 59 PWM1=0.5; // set duty cycle to 50%
jaredwil 1:93eb2c653c86 60 while(t1.read_ms() < dur){
jaredwil 1:93eb2c653c86 61 //WAIT
jaredwil 0:2c98a4766d9f 62 }
jaredwil 1:93eb2c653c86 63 PWM1=0.0; //turn off
jaredwil 1:93eb2c653c86 64 t1.stop();
jaredwil 1:93eb2c653c86 65
jaredwil 1:93eb2c653c86 66 }
jaredwil 1:93eb2c653c86 67 //if anyother button is pressed abort and start over
jaredwil 1:93eb2c653c86 68 else{
jaredwil 1:93eb2c653c86 69 printf("You didn't press enter you idiot\r");
jaredwil 1:93eb2c653c86 70 isint = 0;
jaredwil 1:93eb2c653c86 71 }
jaredwil 1:93eb2c653c86 72 }
jaredwil 1:93eb2c653c86 73 }
jaredwil 1:93eb2c653c86 74
jaredwil 1:93eb2c653c86 75 }