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