part1

Dependencies:   mbed

main.cpp

Committer:
jaredwil
Date:
2015-02-13
Revision:
2:255d7e71c69b
Parent:
1:93eb2c653c86
Child:
3:664e38d7ae34

File content as of revision 2:255d7e71c69b:

#include "mbed.h"
//Right now this is only working for inputs length 3

//Initialize PWM
PwmOut PWM1(p21);
Serial pc(USBTX, USBRX);
Timer t1;


//Initialize Input that is 8 character
char input[10];

  
 int main()
{
    //define input variables
    float freq;
    int dur;
    //Check if input is valid
    int isint;
    while(1){
         
            //wait for a valid input
            while(isint != 2) 
            {
                //Input Desired Tone
                printf("Input Desired Tone: Frequency (Hz) Duration (ms) followed by enter\r");
                fgets(input, sizeof input, stdin);  //input size length 9 followed by enter (xxxx xxxx/r)
                isint = sscanf(input, "%f %d",&freq,&dur);      //assign input variables
                printf("isint = %d\r",isint); //debug check if input is correct
                //if input is incorrect this will be displayed
                if(isint!= 2)
                printf("You did not enter a number.Please enter an argument's number\r");
            
            }
        
          if(pc.readable()) {
              //if enter is pressed continue to output tone
              if(pc.getc() == '\r')
             {
                //show user what value was input
                printf("Valid Input Frequency = %3.0f, Duration  = %d\r", freq, dur);
                isint = 0;  //reset input
                
                //Play tone with correct freq and durration
                //Timer used for duration
                t1.reset();
                t1.start();
                PWM1.period(1/freq);               // set PWM period to 10 ms   
                PWM1=0.5;                         // set duty cycle to 50% 
                while(t1.read_ms() < dur){
                    //WAIT
                    }
                PWM1=0.0;                         //turn off
                t1.stop();
                
             }
            //if anyother button is pressed abort and start over
             else{
                printf("You didn't press enter you idiot\r");
                isint = 0;  
                 }
            }    
      }
      
}