part1

Dependencies:   mbed

Committer:
jaredwil
Date:
Thu Feb 12 21:11:49 2015 +0000
Revision:
1:93eb2c653c86
Parent:
0:2c98a4766d9f
Child:
2:255d7e71c69b
Part 2 Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaredwil 0:2c98a4766d9f 1 #include "mbed.h"
jaredwil 1:93eb2c653c86 2
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 1:93eb2c653c86 11 char input[8];
jaredwil 1:93eb2c653c86 12
jaredwil 1:93eb2c653c86 13
jaredwil 1:93eb2c653c86 14 int main()
jaredwil 0:2c98a4766d9f 15 {
jaredwil 1:93eb2c653c86 16 float freq;
jaredwil 1:93eb2c653c86 17 int dur;
jaredwil 1:93eb2c653c86 18 int isint;
jaredwil 0:2c98a4766d9f 19 while(1){
jaredwil 1:93eb2c653c86 20
jaredwil 1:93eb2c653c86 21 //wait for a valid input
jaredwil 1:93eb2c653c86 22 while(isint != 2)
jaredwil 1:93eb2c653c86 23 {
jaredwil 1:93eb2c653c86 24 //Input Desired Tone
jaredwil 1:93eb2c653c86 25 printf("Input Desired Tone: Frequency (Hz) Duration (ms) followed by enter\r");
jaredwil 1:93eb2c653c86 26 fgets(input, sizeof input, stdin);
jaredwil 1:93eb2c653c86 27 isint = sscanf(input, "%3f %3d",&freq,&dur);
jaredwil 1:93eb2c653c86 28 printf("isint = %d\r",isint);
jaredwil 1:93eb2c653c86 29 if(isint!= 2)
jaredwil 1:93eb2c653c86 30 printf("You did not enter a number.Please enter an argument's number\r");
jaredwil 1:93eb2c653c86 31
jaredwil 1:93eb2c653c86 32 }
jaredwil 1:93eb2c653c86 33
jaredwil 1:93eb2c653c86 34 if(pc.readable()) {
jaredwil 1:93eb2c653c86 35 //if enter is pressed continue
jaredwil 1:93eb2c653c86 36 if(pc.getc() == '\r')
jaredwil 1:93eb2c653c86 37 {
jaredwil 1:93eb2c653c86 38 printf("Valid Input Frequency = %3.0f, Duration = %d\r", freq, dur);
jaredwil 1:93eb2c653c86 39 isint = 0; //reset input
jaredwil 1:93eb2c653c86 40
jaredwil 1:93eb2c653c86 41 t1.reset();
jaredwil 1:93eb2c653c86 42 t1.start();
jaredwil 1:93eb2c653c86 43 PWM1.period(1/freq); // set PWM period to 10 ms
jaredwil 1:93eb2c653c86 44 PWM1=0.5; // set duty cycle to 50%
jaredwil 1:93eb2c653c86 45 while(t1.read_ms() < dur){
jaredwil 1:93eb2c653c86 46 //WAIT
jaredwil 0:2c98a4766d9f 47 }
jaredwil 1:93eb2c653c86 48 PWM1=0.0; //turn off
jaredwil 1:93eb2c653c86 49 t1.stop();
jaredwil 1:93eb2c653c86 50
jaredwil 1:93eb2c653c86 51 }
jaredwil 1:93eb2c653c86 52 //if anyother button is pressed abort and start over
jaredwil 1:93eb2c653c86 53 else{
jaredwil 1:93eb2c653c86 54 printf("You didn't press enter you idiot\r");
jaredwil 1:93eb2c653c86 55 isint = 0;
jaredwil 1:93eb2c653c86 56 }
jaredwil 1:93eb2c653c86 57 }
jaredwil 1:93eb2c653c86 58 }
jaredwil 1:93eb2c653c86 59
jaredwil 1:93eb2c653c86 60 }