part1
Dependencies: mbed
main.cpp@7:89a6d9004710, 2015-02-22 (annotated)
- Committer:
- jaredwil
- Date:
- Sun Feb 22 17:53:02 2015 +0000
- Revision:
- 7:89a6d9004710
- Parent:
- 6:ab02d7bb40cb
- Child:
- 8:8991b0b82440
Commented;
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:89a6d9004710 | 10 | //Initialize Input buffer |
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 | 7:89a6d9004710 | 30 | |
jaredwil | 7:89a6d9004710 | 31 | //wait for return to be pressed |
jaredwil | 4:34df824930f6 | 32 | while(c != '\r') { |
jaredwil | 7:89a6d9004710 | 33 | printf("%c",c); //echo |
jaredwil | 7:89a6d9004710 | 34 | input[i++]=c; //add current char to buffer |
psahay | 3:664e38d7ae34 | 35 | c = pc.getc(); |
psahay | 3:664e38d7ae34 | 36 | } |
jaredwil | 7:89a6d9004710 | 37 | input[i] = '\0'; //end buffer |
jaredwil | 4:34df824930f6 | 38 | //fgets(input, sizeof input, stdin); //input size length 9 followed by enter (xxxx xxxx/r) |
jaredwil | 2:255d7e71c69b | 39 | isint = sscanf(input, "%f %d",&freq,&dur); //assign input variables |
jaredwil | 4:34df824930f6 | 40 | |
jaredwil | 5:cb200e69a544 | 41 | //printf("isint = %d\r",isint); //debug check if input is correct |
jaredwil | 2:255d7e71c69b | 42 | //if input is incorrect this will be displayed |
jaredwil | 1:93eb2c653c86 | 43 | if(isint!= 2) |
jaredwil | 1:93eb2c653c86 | 44 | printf("You did not enter a number.Please enter an argument's number\r"); |
jaredwil | 1:93eb2c653c86 | 45 | |
jaredwil | 1:93eb2c653c86 | 46 | } |
jaredwil | 1:93eb2c653c86 | 47 | |
jaredwil | 7:89a6d9004710 | 48 | |
jaredwil | 7:89a6d9004710 | 49 | //wait for another enter to output the tone |
jaredwil | 1:93eb2c653c86 | 50 | if(pc.readable()) { |
jaredwil | 2:255d7e71c69b | 51 | //if enter is pressed continue to output tone |
jaredwil | 1:93eb2c653c86 | 52 | if(pc.getc() == '\r') |
jaredwil | 1:93eb2c653c86 | 53 | { |
jaredwil | 2:255d7e71c69b | 54 | //show user what value was input |
jaredwil | 1:93eb2c653c86 | 55 | printf("Valid Input Frequency = %3.0f, Duration = %d\r", freq, dur); |
jaredwil | 1:93eb2c653c86 | 56 | isint = 0; //reset input |
jaredwil | 1:93eb2c653c86 | 57 | |
jaredwil | 2:255d7e71c69b | 58 | //Play tone with correct freq and durration |
jaredwil | 2:255d7e71c69b | 59 | //Timer used for duration |
jaredwil | 1:93eb2c653c86 | 60 | t1.reset(); |
jaredwil | 1:93eb2c653c86 | 61 | t1.start(); |
jaredwil | 6:ab02d7bb40cb | 62 | PWM1.period(1/freq); // set PWM period to user specified |
jaredwil | 1:93eb2c653c86 | 63 | PWM1=0.5; // set duty cycle to 50% |
jaredwil | 1:93eb2c653c86 | 64 | while(t1.read_ms() < dur){ |
jaredwil | 1:93eb2c653c86 | 65 | //WAIT |
jaredwil | 0:2c98a4766d9f | 66 | } |
jaredwil | 1:93eb2c653c86 | 67 | PWM1=0.0; //turn off |
jaredwil | 1:93eb2c653c86 | 68 | t1.stop(); |
jaredwil | 1:93eb2c653c86 | 69 | |
jaredwil | 1:93eb2c653c86 | 70 | } |
jaredwil | 7:89a6d9004710 | 71 | |
jaredwil | 7:89a6d9004710 | 72 | //if another button is pressed besides enter abort and start over |
jaredwil | 1:93eb2c653c86 | 73 | else{ |
jaredwil | 1:93eb2c653c86 | 74 | printf("You didn't press enter you idiot\r"); |
jaredwil | 1:93eb2c653c86 | 75 | isint = 0; |
jaredwil | 1:93eb2c653c86 | 76 | } |
jaredwil | 1:93eb2c653c86 | 77 | } |
jaredwil | 1:93eb2c653c86 | 78 | } |
jaredwil | 1:93eb2c653c86 | 79 | |
jaredwil | 1:93eb2c653c86 | 80 | } |